我有一张表:
category    brand       Plan_Date               M_Sales
----------- ----------- ----------------------- -----------
1292        1294        2011-05-01 00:00:00.000 6452.0000
1292        1294        2011-06-01 00:00:00.000 8526.8000
1292        1294        2011-07-01 00:00:00.000 10890.4000
1292        1294        2011-08-01 00:00:00.000 13542.8000
1292        1294        2011-09-01 00:00:00.000 16484.0000
1292        1294        2011-10-01 00:00:00.000 19714.0000
1292        1296        2011-05-01 00:00:00.000 3328.2000
1292        1296        2011-06-01 00:00:00.000 4380.8000
1292        1296        2011-07-01 00:00:00.000 5577.8000
1292        1296        2011-08-01 00:00:00.000 6919.2000
1292        1296        2011-09-01 00:00:00.000 8405.0000
1292        1296        2011-10-01 00:00:00.000 10035.2000现在想把六月和七月的M_Sales加总,根据category 和 brandSQL语句如何写

解决方案 »

  1.   

    select category, brand,
    sum(M_Sales) as M_Sales
    from t1
    where Plan_Date  between '2001-06-01' and '2001-07-01'
    group by category ,brand
      

  2.   

    select category,brand,sum(M_Sales)
    from tb where month(Plan_Date) in(6,7) group by category,brand
      

  3.   

    select category,brand,sum(M_Sales)M
    from tb
    where convert(varchar(7),Plan_date,120) between '2011-06' and '2011-07'
    group by category,brand
      

  4.   

    select
     category, brand,
     sum(M_Sales) as M_Sales
    from
     t1
    where
     convert(varchar(10),Plan_Date,120)  >= '2001-06-01' and convert(varchar(10),Plan_Date,120)<='2001-07-01'
    group by
     category ,brand
      

  5.   

    or
    select category,brand,sum(M_Sales)M
    from tb
    where Plan_date>='2011-06-01' and Plan_date<'2011-08-01'
    group by category,brand