日期:2014-05-18  浏览次数:20419 次

SQL数据统计
我想在查询到的数据下面加一个统计数据,就比如这下面的数据
我想在这个查询结果的19-22行加上分类统计,如:
A A当年总的销售量
B B当年总的销售量
C C当年总的销售量
D D当年总的销售量


请问可以做出来吗?

------解决方案--------------------
SQL code
select * from tb
union all
select null,null,sum(销售量),店铺 from tb group by 店铺

------解决方案--------------------
SQL code
 SELECT 销售量from tb where 店铺名='A' and 日期=datepart (y,getdate())
SELECT 销售量from tb where 店铺名='B' and 日期=datepart (y,getdate())
SELECT 销售量from tb where 店铺名='C' and 日期=datepart (y,getdate())
SELECT 销售量from tb where 店铺名='D' and 日期=datepart (y,getdate())

------解决方案--------------------
SQL code
  SELECT SUM(销售量) from tb where 店铺名='A' and 日期=datepart (y,getdate())
SELECT SUM(销售量) from tb where 店铺名='B' and 日期=datepart (y,getdate())
SELECT SUM(销售量) from tb where 店铺名='C' and 日期=datepart (y,getdate())
SELECT SUM(销售量) from tb where 店铺名='D' and 日期=datepart (y,getdate())

------解决方案--------------------
SQL code
select * from tb
union all
select null,null,sum(销售量),店铺 from tb group by 店铺