小弟遇到了一个棘手的问题:
我的表结构是这样:
xh element
2009001 Ag
2009001 Al
2009001 Au
2009001 As
2009002 Ba
2009002 Be
现在我想在查询是得到这样的结果:比如有10种元素Ag Al Au As Ba Be Bi Br C Ca,在我表中出现的元素,我就显示为一,否则为0。
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
这该怎么做啊,郁闷中。。。请大哥就我!!!
15 个解决方案
#1
更正一下
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
#2
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
#3
create table test(xh int,element varchar2(10));
insert into test values(2009001,'Ag');
insert into test values(2009001,'Al');
insert into test values(2009001,'Au');
insert into test values(2009001,'As');
insert into test values(2009002,'Ba');
insert into test values(2009002,'Be');
commit;
select * from test t;
select t.xh,
max(decode(t.element, 'Ag', 1, 0)) Ag,
max(decode(t.element, 'Al', 1, 0)) Al,
max(decode(t.element, 'Au', 1, 0)) Au,
max(decode(t.element, 'As', 0, 0)) "As",
max(decode(t.element, 'Ba', 1, 0)) Ba,
max(decode(t.element, 'Be', 1, 0)) Be,
max(decode(t.element, 'Bi', 1, 0)) Bi,
max(decode(t.element, 'Br', 1, 0)) Br,
max(decode(t.element, 'C', 1, 0)) C,
max(decode(t.element, 'Ca', 1, 0)) Ca
from test t
group by t.xh
order by t.xh;
insert into test values(2009001,'Ag');
insert into test values(2009001,'Al');
insert into test values(2009001,'Au');
insert into test values(2009001,'As');
insert into test values(2009002,'Ba');
insert into test values(2009002,'Be');
commit;
select * from test t;
select t.xh,
max(decode(t.element, 'Ag', 1, 0)) Ag,
max(decode(t.element, 'Al', 1, 0)) Al,
max(decode(t.element, 'Au', 1, 0)) Au,
max(decode(t.element, 'As', 0, 0)) "As",
max(decode(t.element, 'Ba', 1, 0)) Ba,
max(decode(t.element, 'Be', 1, 0)) Be,
max(decode(t.element, 'Bi', 1, 0)) Bi,
max(decode(t.element, 'Br', 1, 0)) Br,
max(decode(t.element, 'C', 1, 0)) C,
max(decode(t.element, 'Ca', 1, 0)) Ca
from test t
group by t.xh
order by t.xh;
#4
顶
#5
dicarte
#6
我告诉你该怎么写吧:
SELECT DISTINCT
TB.XH
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ag' ), 0 ) AS Ag
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Al' ), 0 ) AS Al
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Au' ), 0 ) AS Au
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'As' ), 0 ) AS As
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ba' ), 0 ) AS Ba
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Be' ), 0 ) AS Be
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Bi' ), 0 ) AS Bi
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Br' ), 0 ) AS Br
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'C' ), 0 ) AS C
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ca' ), 0 ) AS Ca
FROM YOURTABLE
;
SELECT DISTINCT
TB.XH
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ag' ), 0 ) AS Ag
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Al' ), 0 ) AS Al
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Au' ), 0 ) AS Au
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'As' ), 0 ) AS As
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ba' ), 0 ) AS Ba
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Be' ), 0 ) AS Be
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Bi' ), 0 ) AS Bi
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Br' ), 0 ) AS Br
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'C' ), 0 ) AS C
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ca' ), 0 ) AS Ca
FROM YOURTABLE
;
#7
SELECT P.XH,
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
#8
SELECT P.XH,
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
#9
嗯。ren32408的想法应该性能更好些,建议使用。
稍微修改:
SELECT DISTINCT P.XH,
CASE WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END AG,
.....
FROM TAB1 P
就可以了。
稍微修改:
SELECT DISTINCT P.XH,
CASE WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END AG,
.....
FROM TAB1 P
就可以了。
#10
顶这个
select * from test t;
select t.xh,
max(decode(t.element, 'Ag', 1, 0)) Ag,
max(decode(t.element, 'Al', 1, 0)) Al,
max(decode(t.element, 'Au', 1, 0)) Au,
max(decode(t.element, 'As', 0, 0)) "As",
max(decode(t.element, 'Ba', 1, 0)) Ba,
max(decode(t.element, 'Be', 1, 0)) Be,
max(decode(t.element, 'Bi', 1, 0)) Bi,
max(decode(t.element, 'Br', 1, 0)) Br,
max(decode(t.element, 'C', 1, 0)) C,
max(decode(t.element, 'Ca', 1, 0)) Ca
from test t
group by t.xh
order by t.xh;
做这样的功能,就是用decode这个思路。
#11
以上各位都给出的是元素个数固定的情况下
如果元素个数很多或是不确定,就需要用动态sql写了。
如果元素个数很多或是不确定,就需要用动态sql写了。
#12
我很想知道oracle动态sql怎么写。早上的csdn不能搜索。
#13
三楼正解。
#14
http://www.oracle.com/technology/oramag/oracle/06-jul/o46sql.html
看看这个,能用吗
看看这个,能用吗
#15
谢谢啦,给分
#1
更正一下
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
#2
xh Ag Al Au As Ba Be Bi Br C Ca
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
2009001 1 1 1 0 0 0 0 0 0 0
2009002 0 0 0 0 1 1 0 0 0 0
#3
create table test(xh int,element varchar2(10));
insert into test values(2009001,'Ag');
insert into test values(2009001,'Al');
insert into test values(2009001,'Au');
insert into test values(2009001,'As');
insert into test values(2009002,'Ba');
insert into test values(2009002,'Be');
commit;
select * from test t;
select t.xh,
max(decode(t.element, 'Ag', 1, 0)) Ag,
max(decode(t.element, 'Al', 1, 0)) Al,
max(decode(t.element, 'Au', 1, 0)) Au,
max(decode(t.element, 'As', 0, 0)) "As",
max(decode(t.element, 'Ba', 1, 0)) Ba,
max(decode(t.element, 'Be', 1, 0)) Be,
max(decode(t.element, 'Bi', 1, 0)) Bi,
max(decode(t.element, 'Br', 1, 0)) Br,
max(decode(t.element, 'C', 1, 0)) C,
max(decode(t.element, 'Ca', 1, 0)) Ca
from test t
group by t.xh
order by t.xh;
insert into test values(2009001,'Ag');
insert into test values(2009001,'Al');
insert into test values(2009001,'Au');
insert into test values(2009001,'As');
insert into test values(2009002,'Ba');
insert into test values(2009002,'Be');
commit;
select * from test t;
select t.xh,
max(decode(t.element, 'Ag', 1, 0)) Ag,
max(decode(t.element, 'Al', 1, 0)) Al,
max(decode(t.element, 'Au', 1, 0)) Au,
max(decode(t.element, 'As', 0, 0)) "As",
max(decode(t.element, 'Ba', 1, 0)) Ba,
max(decode(t.element, 'Be', 1, 0)) Be,
max(decode(t.element, 'Bi', 1, 0)) Bi,
max(decode(t.element, 'Br', 1, 0)) Br,
max(decode(t.element, 'C', 1, 0)) C,
max(decode(t.element, 'Ca', 1, 0)) Ca
from test t
group by t.xh
order by t.xh;
#4
顶
#5
dicarte
#6
我告诉你该怎么写吧:
SELECT DISTINCT
TB.XH
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ag' ), 0 ) AS Ag
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Al' ), 0 ) AS Al
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Au' ), 0 ) AS Au
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'As' ), 0 ) AS As
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ba' ), 0 ) AS Ba
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Be' ), 0 ) AS Be
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Bi' ), 0 ) AS Bi
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Br' ), 0 ) AS Br
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'C' ), 0 ) AS C
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ca' ), 0 ) AS Ca
FROM YOURTABLE
;
SELECT DISTINCT
TB.XH
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ag' ), 0 ) AS Ag
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Al' ), 0 ) AS Al
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Au' ), 0 ) AS Au
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'As' ), 0 ) AS As
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ba' ), 0 ) AS Ba
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Be' ), 0 ) AS Be
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Bi' ), 0 ) AS Bi
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Br' ), 0 ) AS Br
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'C' ), 0 ) AS C
, COALESCE ( ( SELECT MAX(1) FROM YOURTABLE WHERE YOURTABLE.XH = TB.XH AND YOURTABLE.ELEMENT = 'Ca' ), 0 ) AS Ca
FROM YOURTABLE
;
#7
SELECT P.XH,
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
#8
SELECT P.XH,
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
MAX(CASE
WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AG,
MAX(CASE
WHEN 'Al' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AL,
MAX(CASE
WHEN 'Ba' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BA,
MAX(CASE
WHEN 'Be' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BE,
MAX(CASE
WHEN 'Br' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BR,
MAX(CASE
WHEN 'Au' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AU,
MAX(CASE
WHEN 'As' = P.ELEMENT THEN
'1'
ELSE
'0'
END) AS,
MAX(CASE
WHEN 'Ca' = P.ELEMENT THEN
'1'
ELSE
'0'
END) CA,
MAX(CASE
WHEN 'Bi' = P.ELEMENT THEN
'1'
ELSE
'0'
END) BI,
MAX(CASE
WHEN 'C' = P.ELEMENT THEN
'1'
ELSE
'0'
END) C
FROM TAB1 P
GROUP BY P.XH
#9
嗯。ren32408的想法应该性能更好些,建议使用。
稍微修改:
SELECT DISTINCT P.XH,
CASE WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END AG,
.....
FROM TAB1 P
就可以了。
稍微修改:
SELECT DISTINCT P.XH,
CASE WHEN 'Ag' = P.ELEMENT THEN
'1'
ELSE
'0'
END AG,
.....
FROM TAB1 P
就可以了。
#10
顶这个
select * from test t;
select t.xh,
max(decode(t.element, 'Ag', 1, 0)) Ag,
max(decode(t.element, 'Al', 1, 0)) Al,
max(decode(t.element, 'Au', 1, 0)) Au,
max(decode(t.element, 'As', 0, 0)) "As",
max(decode(t.element, 'Ba', 1, 0)) Ba,
max(decode(t.element, 'Be', 1, 0)) Be,
max(decode(t.element, 'Bi', 1, 0)) Bi,
max(decode(t.element, 'Br', 1, 0)) Br,
max(decode(t.element, 'C', 1, 0)) C,
max(decode(t.element, 'Ca', 1, 0)) Ca
from test t
group by t.xh
order by t.xh;
做这样的功能,就是用decode这个思路。
#11
以上各位都给出的是元素个数固定的情况下
如果元素个数很多或是不确定,就需要用动态sql写了。
如果元素个数很多或是不确定,就需要用动态sql写了。
#12
我很想知道oracle动态sql怎么写。早上的csdn不能搜索。
#13
三楼正解。
#14
http://www.oracle.com/technology/oramag/oracle/06-jul/o46sql.html
看看这个,能用吗
看看这个,能用吗
#15
谢谢啦,给分