数据如下,第一行是列名。
以第一条记录解释数据,在GD省GZ市的DuYun销售员一月份(Jan)品牌FA销售的销量是1200,在二月份(Feb)品牌FA的销售量是1800。
Province city brand contact sale sale_month
GD GZ FA DuYun 1200 Jan
ZJ HZ FB DuanT 1300 Jan
FJ FZ FC DeMa 1500 Jan
GD GZ FA DuYun 1800 Feb
ZJ HZ FB DuanT 1900 Feb
FJ FZ FC DeMa 2000 Feb
现在要做出如下的统计表格,分别统计每个销售人员的销售情况。
Province city brand contact Jan Feb
GD GZ FA DuYun 1200 1800
ZJ HZ FB DuanT 1300 1900
FJ FZ FC DeMa 1500 2000
不知道这条语句该怎样写?
请诸位达人指教~~~~~小分送上~~~
------解决方案--------------------------------------------------------
SELECT Province,city,brand,SUM(case when sale_month
='jan' then sale else 0) as jsn ,SUM(case when sale_month
='feb' then sale else 0) as feb
FROM TT GROUP BY Province,city,brand
动态用SP生成SQL语句
------解决方案--------------------------------------------------------
select Province,city,brand,contact,
sum(case sale_month when 'Jan' then sale end) as Jan,
sum(case sale_month when 'Feb' then sale end) as Feb
from 数据如下
group by Province,city,brand,contact