我有一张表:
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 和 brand
SQL语句如何写
------解决方案--------------------
- SQL code
select category, brand,sum(M_Sales) as M_Salesfrom t1where Plan_Date between '2001-06-01' and '2001-07-01'group by category ,brand
------解决方案--------------------
- SQL code
select category,brand,sum(M_Sales)from tb where month(Plan_Date) in(6,7) group by category,brand
------解决方案--------------------
- SQL code
select category,brand,sum(M_Sales)Mfrom tbwhere convert(varchar(7),Plan_date,120) between '2011-06' and '2011-07'group by category,brand
------解决方案--------------------
- SQL code
select category, brand, sum(M_Sales) as M_Salesfrom t1where convert(varchar(10),Plan_Date,120) >= '2001-06-01' and convert(varchar(10),Plan_Date,120)<='2001-07-01'group by category ,brand