線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1212
推到 Plurk!
推到 Facebook!

sql 語法如何填補

尚未結案
jt25
一般會員


發表:14
回覆:30
積分:8
註冊:2003-08-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-23 13:54:46 IP:61.59.xxx.xxx 未訂閱
請問 SQL 語法如何填補 原SQL 語法: SELECT M_PERIOD AS 月, COUNT (REC_MA) AS 筆數, SUM (REC_MA) AS 金額 FROM SSMM WHERE YYMM = '9211' GROUP BY M_PERIOD ORDER BY M_PERIOD 原結果: 月 筆數 金額 1 386 200.0 2 90 100.0 3 239 400.0 5 1 2400.0 8 580 18300.0 10 1 700.0 12 8 400.0 填補成為: 月 筆數 金額 1 386 200 2 90 100 3 239 400 4 0 0 5 1 2400 6 0 0 7 0 0 8 580 18300 9 0 0 10 1 700 11 0 0 12 8 400
Fishman
尊榮會員


發表:120
回覆:1949
積分:2163
註冊:2006-10-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-12-23 14:52:17 IP:210.65.xxx.xxx 未訂閱
Hi:    Sorry,因為平常都用 Oracle,MS SQL 語法我不太熟,你先試試以下 SQL,若有錯誤,請不吝指教
SELECT  月,
        SUM(筆數) 筆數,
        SUM(金額) 金額
FROM   (SELECT  M_PERIOD AS 月, COUNT (REC_MA) AS 筆數, SUM (REC_MA) AS 金額
        FROM    SSMM
        WHERE   YYMM = '9211' 
        GROUP BY
                M_PERIOD
        UNION
        SELECT 1 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 2 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 3 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 4 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 5 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 6 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 7 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 8 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 9 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 10 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 11 AS 月,0 AS 筆數,0 AS 金額
        UNION
        SELECT 12 AS 月,0 AS 筆數,0 AS 金額)
GROUP BY
        月
ORDER BY
        月
-------------------------------- 小弟才疏學淺,若有謬誤請不吝指教 --------------------------------
------
Fishman
blanka
一般會員


發表:7
回覆:7
積分:2
註冊:2003-01-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-12-24 12:36:59 IP:211.73.xxx.xxx 未訂閱
Fishman前輩的語法應該可以達到你的需求, 不過我個人盡量避免使用子查詢, 因為相對而言子查詢比較慢, 我比要傾向先把需要的月份先做成一個暫存的Table或View, (前端跑回圈產生sql語法), 然後再以這個暫存的Table或View為主left join你的SSMM, 應該也可以達成你的需求...... ------------------------------ 我是個菜鳥,若有錯誤請勿見怪 吼..吼...吼....
------
吼..吼...吼....
jt25
一般會員


發表:14
回覆:30
積分:8
註冊:2003-08-19

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-12-25 14:24:41 IP:61.59.xxx.xxx 未訂閱
Fishman 前輩你好: 你提供語法小弟已經測試過但是會出現錯誤 關鍵字 'GROUP' 附近的語法不正確。 單獨執行子查詢是OK ? 請問要如何排除
Fishman
尊榮會員


發表:120
回覆:1949
積分:2163
註冊:2006-10-28

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-12-26 09:00:55 IP:210.65.xxx.xxx 未訂閱
Hi:    我在 SQL Server 上試了一下,原則上使可以執行的,但是它把 Command 改為
SELECT  月,
        SUM(筆數) AS 筆數,
        SUM(金額) AS 金額
FROM   (SELECT  M_PERIOD AS 月,
                COUNT(REC_MA) AS 筆數,
                SUM(REC_MA) AS 金額
        FROM    SSMM
        WHERE   YYMM = '9211'
        GROUP BY
                M_PERIOD
        UNION
        SELECT         1 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         2 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         3 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         4 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         5 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         6 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         7 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         8 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         9 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         10 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         11 AS 月, 0 AS 筆數, 0 AS 金額
        UNION
        SELECT         12 AS 月, 0 AS 筆數, 0 AS 金額) DERIVEDTBL
GROUP BY  月
ORDER BY  月
你試試看是否可行 -------------------------------- 小弟才疏學淺,若有謬誤請不吝指教 --------------------------------
------
Fishman
jt25
一般會員


發表:14
回覆:30
積分:8
註冊:2003-08-19

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-12-26 09:43:57 IP:61.59.xxx.xxx 未訂閱
Fishman 前輩你好: 加上 DERIVEDTBL 就可以了 非常感謝!
系統時間:2024-11-22 22:30:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!