需求是:乙個月有多個發布額,現在要求按月統計發布額,例如:1月發布額--35900,2月發布額--2300
sql 語句如下:
--按月分組,求和
select dmonth,codename,sum(iamount) from
(--求出全部日期,發布額
select convert(varchar(7),a.dregdate,120) as dmonth,a.coper_code+'-'+w.work_name as codename
,iamount as iamount
from viw_executeall a
inner join tbl_worksub s on left(a.coper_code, 2) = s.work_group
inner join tbl_dealer d on a.ccorp_code = d.cdealer_code
inner join tbl_work w on w.work_code = a.coper_code
where a.iamount >0 and s.cmemo =1 and bbig =0 and (dfrom_date <= '2007-07-30') and
(dto_date >= '2007-06-01') and s.ccity_name ='北京' and d.cyyzzcode ='1'
) aa
group by dmonth,codename
分組求和SQL示例
1 rollup和cube函式,自動彙總資料 select from test tbl的資料這樣的 col a col b col c 1 b1 12 1 b1 2 1 b2 31 2 b2 7 2 b3 42 2 b3 1 2 b3 3 如果按a b列進行彙總c列,用一般的方法是這樣 select...
SQL技巧之分組求和
這是csdn問答裡面有人提出的一道問題,題目如下。如下 得出結果如下 求精簡的sql語句。sql查詢語句 with a as select rank over partition by 商店 order by 商店,sum 價錢 desc as rowid,商店,sum 價錢 as 價錢和,備註 f...
SQL技巧之分組求和
這是csdn問答裡面有人提出的一道問題,題目如下。如下 得出結果如下 求精簡的sql語句。sql查詢語句 with a as select rank over partition by 商店 order by 商店,sum 價錢 desc as rowid,商店,sum 價錢 as 價錢和,備註 f...