全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2005
推到 Plurk!
推到 Facebook!

sql語法如何累進加總?

尚未結案
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-14 10:07:00 IP:210.61.xxx.xxx 未訂閱
請教各位前輩,我要產生一份月銷售報表如下 日 銷售額  累進額 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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-14 11:06:41 IP:211.78.xxx.xxx 未訂閱
可不可以這樣 SELECT dd,m1Vol,(SELECT Sum(m1Vol) FROM billa B WHERE B.dd <= A.dd) as ShiftTot FROM billa A ORDER BY dd 發表人 - 懷舊的人 於 2005/06/14 11:33:20
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-14 12:00:13 IP:210.61.xxx.xxx 未訂閱
懷舊的人您好,謝謝您 經修改後
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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-14 12:35:13 IP:211.78.xxx.xxx 未訂閱
引言: 懷舊的人您好,謝謝您 經修改後
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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-06-14 13:01:06 IP:210.65.xxx.xxx 未訂閱
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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-06-14 14:50:05 IP:202.154.xxx.xxx 未訂閱
修改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)
系統時間:2024-06-27 0:20:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!