sql語法如何累進加總? |
尚未結案
|
pedro
尊榮會員 發表:152 回覆:1187 積分:892 註冊:2002-06-12 發送簡訊給我 |
請教各位前輩,我要產生一份月銷售報表如下
日 銷售額 累進額
01 100 100
02 150 250
...
我用這樣語法
select Right(yymmdd,2) as dd, Sum(case when left(yymmdd,6)='200506' then Volume else 0 end) as m1Vol, '' as ShiftTot from ( SELECT Left(a.OrdNo,8) as yymmdd,b.PRICE * (b.Quity-b.ReturnQty) as Volume FROM bil1a a right JOIN bil1b b ON a.ordno=b.ordno where a.OrdNo like '200506%' ) as tmp group by Right(yymmdd,2) order by Right(yymmdd,2)請問上面ShiftTot欄位要怎麼下,才能達到累進的效果? 謝謝您 ................... .楛耕傷稼,楛耘失歲. ................... |
懷舊的人
高階會員 發表:28 回覆:152 積分:141 註冊:2003-01-08 發送簡訊給我 |
|
pedro
尊榮會員 發表:152 回覆:1187 積分:892 註冊:2002-06-12 發送簡訊給我 |
懷舊的人您好,謝謝您
經修改後
select Right(yymmdd,2) as dd, Sum(case when left(yymmdd,6)='200506' then Volume else 0 end) as m1Vol, Sum( select Price*Quity from bil1b where Left(OrdNo,8) between '20050601' and '200506' Right(yymmdd,2) ) as ShiftTot from ( SELECT Left(a.OrdNo,8) as yymmdd,b.PRICE * (b.Quity-b.ReturnQty) as Volume FROM bil1a a right JOIN bil1b b ON a.ordno=b.ordno where a.OrdNo like '200506%' ) as tmp group by Right(yymmdd,2) order by Right(yymmdd,2)出現錯誤 Server: Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'select'. Server: Msg 170, Level 15, State 1, Line 5 Line 5: Incorrect syntax near ')'. Server: Msg 156, Level 15, State 1, Line 11 Incorrect syntax near the keyword 'as'. ................... .楛耕傷稼,楛耘失歲. ................... |
懷舊的人
高階會員 發表:28 回覆:152 積分:141 註冊:2003-01-08 發送簡訊給我 |
引言: 懷舊的人您好,謝謝您 經修改後select Right(yymmdd,2) as dd, Sum(case when left(yymmdd,6)='200506' then Volume else 0 end) as m1Vol, Sum( select Price*Quity from bil1b where Left(OrdNo,8) between '20050601' and '200506' Right(yymmdd,2) ) as ShiftTot from ( SELECT Left(a.OrdNo,8) as yymmdd,b.PRICE * (b.Quity-b.ReturnQty) as Volume FROM bil1a a right JOIN bil1b b ON a.ordno=b.ordno where a.OrdNo like '200506%' ) as tmp group by Right(yymmdd,2) order by Right(yymmdd,2)出現錯誤 Server: Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'select'. Server: Msg 170, Level 15, State 1, Line 5 Line 5: Incorrect syntax near ')'. Server: Msg 156, Level 15, State 1, Line 11 Incorrect syntax near the keyword 'as'. ................... .楛耕傷稼,楛耘失歲. ................... select Right(OrdNo,2) as dd,Sum(IsNull(Volume,0)) as m1Vol,Sum(select Price*Quity from bil1b B where Left(B.OrdNo,8) between '20050601' and '200506' Right(A.OrdNo,2)) as ShiftTot from billa A where A.OrdNo like '200506%' group by Right(OrdNo,2) order by dd如果不正確 請告知 table 有哪些欄位 |
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi Pedro, 不知你使用的是何種資料庫? 我在 MS SQL 2000 上測試過以下語法可行,你可以試試
SELECT RIGHT(YMD,2) DD, SUM(AMT) AMT, (SELECT SUM(AMT) FROM TABLE2 T2 WHERE T2.YMD LIKE '0506%' AND RIGHT(T2.YMD,2)<= RIGHT(T1.YMD,2)) AS TOTAMT FROM TABLE2 T1 WHERE YMD LIKE '0506%' GROUP BY RIGHT(YMD,2)---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman |
BOSS
中階會員 發表:70 回覆:79 積分:64 註冊:2006-11-01 發送簡訊給我 |
修改fishman大大的 select right(t.date_,2) date_, sum(t.amt_) amt_,
sum(t.amt_) isnull((select sum(amt_) from aa x where x.date_ like '200506%' and right(x.date_,2) < right(t.date_,2)),0)
from aa t
where t.date_ like '200506%'
group by right(t.date_,2)
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |