假设你的表名字叫table1 如果编号是主键可以这样写 select a.编号 ||',' ||b.编号 col1, a.名称 || ','||b.名称 col2, (a.数量+b.数量) col3 from (select t.编号, t.名称,t.数量, '1' row1 from table1 t where t.编号 = 001') a, (select t.编号, t.名称,t.数量, '1' row1 from table1 t where t.编号 = 002') b where a.row1 = b.row1
Create Table test (编号 Varchar2(3),名称 Varchar2(10),数量 Integer) Insert Into test Values ('001','小刀',3) Insert Into test Values ('002','剪刀',7)
Select 编号,名称,数量 From ( Select a.编号||'/'||b.编号 编号,a.名称||'/'||b.名称 名称,Sum(a.数量+b.数量) 数量,row_number() over (Order By a.编号||'/'||b.编号) over_id From test a,test b Where Not a.编号=b.编号 Group By a.编号||'/'||b.编号,a.名称||'/'||b.名称 ) Where Rownum =1
哈哈,受包兄的影响不小啊
from a a1,a a2
但是會有四條紀錄應該加判斷。但是如果紀錄多了就沒辦法直接實現,
還是用過程吧
如果编号是主键可以这样写
select a.编号 ||',' ||b.编号 col1,
a.名称 || ','||b.名称 col2,
(a.数量+b.数量) col3
from (select t.编号, t.名称,t.数量, '1' row1
from table1 t
where t.编号 = 001') a,
(select t.编号, t.名称,t.数量, '1' row1
from table1 t
where t.编号 = 002') b
where a.row1 = b.row1
Insert Into test Values ('001','小刀',3)
Insert Into test Values ('002','剪刀',7)
Select 编号,名称,数量 From
(
Select a.编号||'/'||b.编号 编号,a.名称||'/'||b.名称 名称,Sum(a.数量+b.数量) 数量,row_number() over (Order By a.编号||'/'||b.编号) over_id
From test a,test b
Where Not a.编号=b.编号
Group By a.编号||'/'||b.编号,a.名称||'/'||b.名称
) Where Rownum =1
结果:
1 001/002 小刀/剪刀 10