如何根据 “YearMonth”是“2009”开头 和 “RegionName”不同的记录,求“SumQty”的和?
------解决方案--------------------
- SQL code
select RegionName,sum(SumQty)as SumQty from table_name where left(yearMonth,4)=2009group by RegionName
------解决方案--------------------
select RegionName,sum(SumQty) as SumQty,YearMonth from tbl
where left(YearMonth ,4)='2009'
group by RegionName and left(YearMonth ,4)
------解决方案--------------------
- SQL code
GOIF OBJECT_ID('TBL')IS NOT NULLDROP TABLE TBLGOCREATE TABLE TBL(RName varchar(20),SumQty decimal(18,2),YearMonth varchar(6))GOINSERT TBLSELECT '沪北小区',25452.00,'200901' UNION ALLSELECT '沪北小区',35452.00,'200901' UNION ALLSELECT '嘉定校区',26452.00,'200901' UNION ALLSELECT '南校区', 25452.00,'200901' UNION ALLSELECT '四平校区',25422.00,'200901' UNION ALLSELECT '彭五小区',52452.00,'200901' UNION ALLSELECT '沪北小区',21252.00,'200902' union allSELECT '沪西小区',20452.00,'200902'select RName,SUM(SumQty) as SumQty,left(YearMonth,4) as [Year]from tbl where left(YearMonth,4)='2009'group by RName,left(YearMonth,4)/*RName SumQty Year沪北小区 82156.00 2009沪西小区 20452.00 2009嘉定校区 26452.00 2009南校区 25452.00 2009彭五小区 52452.00 2009四平校区 25422.00 2009*/
------解决方案--------------------
------解决方案--------------------
select left(yearmonth,4) yearmonth , RegionName , sum(SumQty) SumQty from tb group by left(yearmonth,4) , RegionName
select substring(yearmonth,1,4) yearmonth , RegionName , sum(SumQty) SumQty from tb group by substring(yearmonth,1,4) , RegionName
------解决方案--------------------
------解决方案--------------------
select sum(sumqty),regionname from a where left(yearMonth,4) = '2009' group by regionName;
------解决方案--------------------
3楼正解