sql 語法如何填補 |
尚未結案
|
jt25
一般會員 發表:14 回覆:30 積分:8 註冊:2003-08-19 發送簡訊給我 |
請問 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 發送簡訊給我 |
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 發送簡訊給我 |
|
jt25
一般會員 發表:14 回覆:30 積分:8 註冊:2003-08-19 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
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 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |