該select 語句怎樣寫
將t1中的數據由縱向變為橫向顯示,t1中包含bh、xu、desp,t1中,在t1中bh相同的記錄最多為3條,有時可能為2條,有時可能為1條;
要查詢出來的顯示的形式為bh 、desp1、desp2 、desp3,t2中不包含t1中的xu,當t1為如下表時,
t1中的記錄
bh xu desp
M001 1 aaa
M001 2 a
M001 3 b
M002 1 c
M002 2 a
M003 1 2
M004 1 dd
M004 2 ff
查詢後應顯示的記錄為
bh desp1 desp2 desp3
M001 aaa a b
M002 c a
M003 2
M004 dd ff
請問該查詢語句怎樣寫?
问题点数:20、回复次数:5Top
1 楼yelook(香槟酒)回复于 2004-11-03 21:12:36 得分 10
select bh,
max(case when xu=1 then desp else '' end) as desp1,
max(case when xu=2 then desp else '' end) as desp2,
max(case when xu=3 then desp else '' end) as desp3
from t1 group by bhTop
2 楼biliky()回复于 2004-11-03 21:29:58 得分 0
select distinct bh, (select nvl(desp,null) from t1 where xu=1 and bh=t.bh) desp1,
(select nvl(desp,null) from t1 where xu=2 and bh=t.bh) desp2,
(select nvl(desp,null) from t1 where xu=3and bh=t.bh) desp3
from t1 t;Top
3 楼MCHorse(mchorse)回复于 2004-11-03 21:41:23 得分 0
nvl是函數嗎?Top
4 楼biliky()回复于 2004-11-03 22:11:10 得分 0
搞错了,我以为这是oracle版。nvl是oracle的库函数,判断第一次参数如果不存在则取第二个参数的值。相当于sql server的函数isnull()。Top
5 楼biliky()回复于 2004-11-03 22:16:33 得分 10
select distinct bh,(select desp from t1 where xu=1 and bh=t.bh) desp1,
(select desp from t1 where xu=2 and bh=t.bh) desp2,
(select desp from t1 where xu=3 and bh=t.bh) desp3 from t1 t;Top




