我尝试进行查询,在那里我可以看到该网站的销售净额 2007年和2008年美国和世界其他地区的数据库以及所有小计以及总数。(值得一提的是,美国和世界是已经在数据库中编码的区域)
我的查询到目前为止:
SELECT country regionname, sum(custamot)Totsales
FROM( select o.customerid, sum(netamount) custamot, c.country
FROM orders o join customers c
on o.customerid = c.customerid
where country like 'US'
group by c.customerid ) iv;
这个查询告诉我美国的totsales(没有一年你可以看到)。 我尝试了没有部分的查询:
where country like 'US'
但结果是一样的。我无法找到一个查询,其中显示了特定年份的美国和世界......
任何想法都会有所帮助
感激, JOP
答案 0 :(得分:0)
您需要在外部添加group by
子句:
SELECT country as regionname, sum(custamot) as Totsales
FROM (select o.customerid, sum(netamount) custamot, c.country
FROM orders o join
customers c
on o.customerid = c.customerid
group by c.customerid
) iv
group by country;
实际上,您不需要子查询:
select country as regionname, sum(netamount) as Totsales
FROM orders o join
customers c
on o.customerid = c.customerid
group by country;
要在同一查询中获取总数,请使用with rollup
:
select country as regionname, sum(netamount) as Totsales
FROM orders o join
customers c
on o.customerid = c.customerid
group by country with rollup;
最后,要添加年份,请使用where
子句或尝试此操作:
select year(orderdate) as yr, country as regionname, sum(netamount) as Totsales
FROM orders o join
customers c
on o.customerid = c.customerid
group by year(orderdate), country with rollup;
分开美国和ROW:
select year(orderdate) as yr,
(case when country = 'US' then 'US' else 'ROW' end) as region,
sum(netamount) as TotSales
FROM orders o join
customers c
on o.customerid = c.customerid
group by year(orderdate), (case when country = 'US' then 'US' else 'ROW' end) with rollup;
答案 1 :(得分:0)
这样的事情:
SELECT country as regionname, sum(custamot)Totsales
FROM( select o.customerid, sum(netamount) custamot, c.country
FROM orders o join customers c
on o.customerid = c.customerid
where left(my_date,4) between 2007 and 2008
group by c.customerid ) iv
group by country
Saludos;)
答案 2 :(得分:0)
要获得特定年份的结果,假设您有一个日期字段,
where YourDateField >= {d '2007-01-01'}
and YourDateField < {d '2008-01-01'}