表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    

解决方案 »

  1.   

    select year,month,xun,sum(pronumber) from tb group by year,month,xun
      

  2.   

    select year,month,xun,pronumber from tb where year=@year
      

  3.   

    select year,month,xun,sum(pronumber) from tb 
    where year=2011
    group by year,month,xun
      

  4.   

    select year,month,xun,sum(pronumber) from tb  
    where year=2011
    group by month,xun
      

  5.   

    select year,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
    order by period2
      

  6.   

    上面的个when写成一样的“上旬"了,你改一下 上,中,下
      

  7.   

    select year,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
      

  8.   

    order by month,xun2
      

  9.   

    select year,month,(case when xun is 1 then '' ..end) as xun,sum(pronumber) from tb  
    where year=@year
    group by year,month,xun
      

  10.   

    SQL codeselect year,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
    order by period2顶
      

  11.   

    select year,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
      

  12.   

    select year,month,(case when xun is 1 then '' ..end) as xun,sum(pronumber) from tb   
    where year=@year
    group by year,month,xun
    order by case when xun ='' then 1 when xun ='' then 2 when xun ='' then 3 end
      

  13.   

    select year,
           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
      

  14.   

    select year as '年份',month as '月份',
    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
      

  15.   

    select year as '年份',month as '月份',
    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  没改过来!!!
      

  16.   

    select year,month,xun,sum(pronumber) from tb 
    where year=2011
    group by year,month,xun
      

  17.   

    select year,month,xun,sum(pronumber) from tb  
    where year=2011
    group by year,month,xun
      

  18.   


    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
    */
      

  19.   

    楼上的,那如果有2条,或者多条2001,1,'上旬',4  或者是中旬,或者是下旬的数据呢?这个 
    procount又该如何统计出来
      

  20.   

    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,'下旬',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月上旬的数据,直接总汇,没有问题呀。