表tb
id,year,month,xun(每个月分 上旬,中旬,下旬), pronumber(产品数量)
现在的问题是:
如何根据所选择的年份,查询出每个月上,中和下旬的产品数量统计表
比如2011 1 上旬 20000
2011 1 中旬 10000
2011 1 下旬 13000
2011 2 上旬 10000
2011 2 中旬 12000
2011 2 下旬 30000
。
。
。
。
2011 12 上旬 30000
2011 12 中旬 20000
2011 12 下旬 13000
id,year,month,xun(每个月分 上旬,中旬,下旬), pronumber(产品数量)
现在的问题是:
如何根据所选择的年份,查询出每个月上,中和下旬的产品数量统计表
比如2011 1 上旬 20000
2011 1 中旬 10000
2011 1 下旬 13000
2011 2 上旬 10000
2011 2 中旬 12000
2011 2 下旬 30000
。
。
。
。
2011 12 上旬 30000
2011 12 中旬 20000
2011 12 下旬 13000
where year=2011
group by year,month,xun
where year=2011
group by month,xun
where year=2011
group by year,month,xun
order by xun2
order by period2
where year=2011
group by year,month,xun
order by xun2
where year=@year
group by year,month,xun
where year=2011
group by year,month,xun
order by xun2
order by period2顶
where year=2011
group by year,month,xun
order by xun2
where year=@year
group by year,month,xun
order by case when xun ='' then 1 when xun ='' then 2 when xun ='' then 3 end
month,
xun,
sum(pronumber) as procount,
(case when xun='上旬' then 1 when xun='上旬' then 2 when xun='上旬' then 3 else 0 end) as xun2 from tb
where year=2011
group by year,month,xun
order by xun2
max(case xun when '上' then money end) as '上旬',
max(case xun when '中' then money end) as '中旬',
max(case xun when '下' then money end) as '下旬'
from month group by year
max(case xun when '上' then money end) as '上旬',
max(case xun when '中' then money end) as '中旬',
max(case xun when '下' then money end) as '下旬'
from TB group by year刚才建了个表叫month 没改过来!!!
where year=2011
group by year,month,xun
where year=2011
group by year,month,xun
declare @表tb table (id int,[year] int,[month] int,xun varchar(4),procount int)
insert into @表tb
select 1,2001,1,'上旬',5 union all
select 2,2001,1,'中旬',5 union all
select 3,2001,1,'下旬',5 union all
select 4,2001,2,'上旬',4 union all
select 5,2001,2,'中旬',7 union all
select 6,2001,2,'下旬',11 union all
select 7,2001,3,'上旬',4 union all
select 8,2001,3,'中旬',9 union all
select 9,2001,3,'下旬',5select [year],[month],
数字统计='上旬:'+cast(sum(case xun when '上旬' then procount else 0 end) as varchar(10)),
数字统计='中旬:'+cast(sum(case xun when '中旬' then procount else 0 end) as varchar(10)),
数字统计='下旬:'+cast(sum(case xun when '下旬' then procount else 0 end) as varchar(10))
from @表tb
group by [year],[month]/*
year month 数字统计 数字统计 数字统计
----------- ----------- --------------- --------------- ---------------
2001 1 上旬:5 中旬:5 下旬:5
2001 2 上旬:4 中旬:7 下旬:11
2001 3 上旬:4 中旬:9 下旬:5
*/
procount又该如何统计出来
insert into @表tb
select 1,2001,1,'上旬',5 union all
select 2,2001,1,'中旬',5 union all
select 3,2001,1,'下旬',5 union all
select 4,2001,2,'上旬',4 union all
select 5,2001,2,'中旬',7 union all
select 6,2001,2,'下旬',11 union all
select 7,2001,3,'上旬',4 union all
select 8,2001,3,'中旬',9 union all
select 9,2001,3,'下旬',5 union all
select 10,2001,1,'上旬',7 union all
select 11,2001,1,'上旬',8 select [year],[month],
数字统计='上旬:'+cast(sum(case xun when '上旬' then procount else 0 end) as varchar(10)),
数字统计='中旬:'+cast(sum(case xun when '中旬' then procount else 0 end) as varchar(10)),
数字统计='下旬:'+cast(sum(case xun when '下旬' then procount else 0 end) as varchar(10))
from @表tb
group by [year],[month]
/*
year month 数字统计 数字统计 数字统计
----------- ----------- --------------- --------------- ---------------
2001 1 上旬:20 中旬:5 下旬:5
2001 2 上旬:4 中旬:7 下旬:11
2001 3 上旬:4 中旬:9 下旬:5
*/我新加了2条1月上旬的数据,直接总汇,没有问题呀。