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

sybase 查詢語句(急!)

尚未結案
zhuna
一般會員


發表:26
回覆:39
積分:13
註冊:2003-05-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-05-22 20:59:52 IP:211.148.xxx.xxx 未訂閱
在sybase數據庫中有這樣三個table1(year,month,vendor,amt) table2(vendor,name,venclass) table3(venclass,description) 現在我想得到的結果為: year, venclass,description,1月amt,2月amt......12月amt 這些結果要以year,venclass分組合計得來的. 互相幫助,共同進步
------
互相幫助,共同進步
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-05-23 00:17:14 IP:61.221.xxx.xxx 未訂閱
可以這樣下:
select year, t2.venclass, description,
  sum(case when month=1 then amt else 0 end) as "1月amt",
  sum(case when month=2 then amt else 0 end) as "1月amt",
  sum(case when month=3 then amt else 0 end) as "1月amt",
  ...
  sum(case when month=12 then amt else 0 end) as "12月amt",
from table1 t1 
  inner join table2 t2 on t1.vendor=t2.vendor
  inner join table3 t3 on t2.venclass=t3.venclass
group by year, t2.venclass, description
order by year, t2.venclass
zhuna
一般會員


發表:26
回覆:39
積分:13
註冊:2003-05-19

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-05-23 08:20:51 IP:211.148.xxx.xxx 未訂閱
引言: 可以這樣下:
select year, t2.venclass, description,
  sum(case when month=1 then amt else 0 end) as "1月amt",
  sum(case when month=2 then amt else 0 end) as "1月amt",
  sum(case when month=3 then amt else 0 end) as "1月amt",
  ...
  sum(case when month=12 then amt else 0 end) as "12月amt",
from table1 t1 
  inner join table2 t2 on t1.vendor=t2.vendor
  inner join table3 t3 on t2.venclass=t3.venclass
group by year, t2.venclass, description
order by year, t2.venclass
但是會出現語法錯誤,inner附近的語法錯,是不是在sybase中不能這樣用? 錯誤提示:Incorrect syntax near 'inner'. 互相幫助,共同進步
------
互相幫助,共同進步
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-05-23 09:31:43 IP:211.76.xxx.xxx 未訂閱
[quote] 可以這樣下:
select year, t2.venclass, description,
  sum(case when month=1 then amt else 0 end) as "1月amt",
  sum(case when month=2 then amt else 0 end) as "1月amt",
  sum(case when month=3 then amt else 0 end) as "1月amt",
  ...
  sum(case when month=12 then amt else 0 end) as "12月amt", -- 最後一個逗號要拿掉
from table1 t1 
  inner join table2 t2 on t1.vendor=t2.vendor
  inner join table3 t3 on t2.venclass=t3.venclass
group by year, t2.venclass, description
order by year, t2.venclass
若還是不行的話就將 from 的後面改為:
from table1 t1, table2 t2, table3 t3 
where t1.vendor=t2.vendor and t2.venclass=t3.venclass
group by year, t2.venclass, description
order by year, t2.venclass
系統時間:2024-06-27 0:59:26
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!