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

如何一次查詢不同時間條件之總計資料

答題得分者是:folkchen
kuanll
一般會員


發表:5
回覆:6
積分:2
註冊:2002-09-25

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-10 12:06:25 IP:210.243.xxx.xxx 未訂閱
我有一個銷售資料表,記錄每一個月份的銷售資料 需要統計:"當月","上個月","去年同月","累計至當月","本年累計",五個統計值 目前的作法是寫成五個查詢,分別使用不同的時間條件下去查詢,得到累計值.. 想請問各位,是否有什麼方法可以使用一次SQL查詢得到這五個值(在一個ROW中) 希望能用SQL查詢,而不是使用view或是預存程序... 謝謝各位!!
folkchen
高階會員


發表:9
回覆:232
積分:173
註冊:2003-10-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-10 12:22:35 IP:211.20.xxx.xxx 未訂閱
Select sum(t1.Amt),sum(t2.Amt),sum(t3.Amt),sum(t4.Amt),sum(t5.Amt) From Table as t1, Table as t2, Table as t3, Table as t4, Table as t5 Where (t1~t5 的 Join Key) And (t1 的資料篩選) And (t2 的資料篩選) And (t3 的資料篩選) And (t4 的資料篩選) And (t5 的資料篩選) 限制:Join Key 不能為資料篩選的欄位 若你的資料庫有支援 SubQuery 可以使用以下方法,沒有限制 Select From (Select (JoinKey...),sum(Amt) as totAmt From Table Where (t1 的資料篩選)) as t1, And (Select (JoinKey...),sum(Amt) as totAmt From Table Where (t2 的資料篩選)) as t2, And (Select (JoinKey...),sum(Amt) as totAmt From Table Where (t3 的資料篩選)) as t3, And (Select (JoinKey...),sum(Amt) as totAmt From Table Where (t4 的資料篩選)) as t4, And (Select (JoinKey...),sum(Amt) as totAmt From Table Where (t5 的資料篩選)) as t5 Where (t1~t5 的 Join Key) 若以上都不行,可能只有建 View 或 Work Table 的方式了
系統時間:2024-05-03 12:09:18
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!