用示例查询解释我的问题
select p1.zonename,p1.beatname,p1.groupname,p2.partyname,p2.amount,
p2.groupofcompany from
party1 p1 inner join party2 p2 on p1.txno=p2.txno
此查询显示结果
ZoneName BeatName GroupName PartyName Amount Groupofcompany
AA BB CC A 100 Y
AA BB CC B 200 Y
DD EE FF C 300 N
但我希望这样显示
AA --> Zone Name
BB --> Beat Name
CC --> Group Name (if groupofcompany y then only show other wise not)
A 100 --> Party Name And Amount
B 200
DD
EE
C 300
所以,我试过像这样
SELECT unnest(array[zonename,beatname,groupname,partyname]) AS partyname,
unnest(array[0,0,0,amount]) AS Amount,
unnest(array[1,2,3,4]) AS
ORDER,
row_number() over()
FROM
(SELECT p1.zonename,
p1.beatname,
CASE
WHEN p2.groupofcompany='N' THEN ''
ELSE p1.groupname
END AS groupname,
p2.partyname,
p2.amount,
FROM party1 p1
INNER JOIN party2 p2 ON p1.txno=p2.txno) AS tt
ORDER BY row_number() over(),ORDER
它的显示输出
Party Name Amount Order Row_number
AA 1 1
BB 2 1
CC 3 1
A 100 4 1
AA 1 2
BB 2 2
CC 3 2
B 200 4 2
DD 1 3
EE 2 3
3 3
C 300 4 3
但我希望得到这样的
Party Name Amount
AA
BB
CC
A 100
B 200
DD
EE
C 300
如何删除重复和正确的顺序(zonename,beatname,groupname,partyname)
使用 Postgresql 9.3
答案 0 :(得分:0)
我不确定,但尝试这样
with cte as (
select unnest(array[zonename,beatname,groupname,partyname]) as partyname,
unnest(array[0,0,0,amount]) as Amount
from (
select p1.zonename,p1.beatname,case when p2.groupofcompany='N' then '' else
p1.groupname end as groupname,p2.partyname, p2.amount
from party1 p1 inner join party2 p2 on p1.txno=p2.txno
) t
)
select * from cte
group by partyname,Amount
order by (case when partyname = 'AA' then 1
when partyname = 'BB' then 2
when partyname = 'CC' then 3
when partyname = 'A' then 4
when partyname = 'B' then 5
when partyname = 'DD' then 6
when partyname = 'EE' then 7
when partyname = 'C' then 8
end)