国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

Oracle里的交叉SQL語句寫法

2024-08-29 13:29:13
字體:
來源:轉載
供稿:網友

實例說明

資料表結構(紅色為主鍵)==>
主表:test_part_cost_tab(料號資料表)
part_no  varchar2(20)   
part_name  varchar2(50) 

從表:test_part_cost_dt_tab(料號成本資料表)
part_no varchar2(10) 
cost_id varchar2(5) 
cost number 

數據==>
主表資料:
 part_no  part_name
1 1000            name1000 
2 1001            name1001 
從表資料:
part_no cost_id  cost
1 1000        100              1.1 
2 1000        200              1.2 
3 1000        300              1.3
4 1000        321              1.321 
5 1001        100              2.1 

交叉資料==>
sql語句產生的結果
part_no  part_name cost_100 cost_200 cost_300 cost_321
1 1000          name1000       1.1                   1.2                 1.3        1.321
2 1001          name1001       2.1                   0                    0                0

具體的交叉sql語句寫法:
select a.part_no,a.part_name,
--sum(b.cost)
sum(case when  b.cost_id = '100' then b.cost else 0 end) as cost_100,
sum(case when  b.cost_id = '200' then b.cost else 0 end) as cost_200,
sum(case when  b.cost_id = '300' then b.cost else 0 end) as cost_300,
sum(case when  b.cost_id = '321' then b.cost else 0 end) as cost_321

from test_part_cost_tab a,test_part_cost_dt_tab b
where a.part_no = b.part_no
group by a.part_no,a.part_name

ps: 若主表有資料,從表沒有資料時,交叉后會沒有相應的資料
解決辦法是在where條件里用外連接
where a.part_no = b.part_no(+)

參考資料:
http://www.cnblogs.com/iouniuniu/archive/2004/04/05/5238.aspx  ==>此文的語法與本文的語法不同
http://it.icxo.com/htmlnews/2004/11/11/454293.htm  
http://www.itpub.net/176727.html
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华阴市| 霍城县| 雅江县| 尚志市| 冀州市| 桐柏县| 册亨县| 阿坝| 年辖:市辖区| 奉化市| 镇江市| 聂拉木县| 镇平县| 博湖县| 榆林市| 石台县| 岚皋县| 陵水| 长汀县| 锦屏县| 萨嘎县| 昌平区| 长兴县| 寿光市| 攀枝花市| 桦南县| 乐清市| 盐山县| 买车| 和田县| 格尔木市| 左云县| 冀州市| 昂仁县| 花垣县| 肥西县| 锦屏县| 墨竹工卡县| 麻江县| 承德县| 进贤县|