我有3行查询返回的数据
OrderId | OtherId
--------+---------
1234 | 444
1234 | 555
1234 | 666
如何以这种格式返回数据
OrderId | OtherId | OtherId2 | OtherId
--------+---------+----------+--------
1234 | 444 | 555 | 666
我能用吗 Distinct
为了这个问题?
编辑
结果集来自一个查询,例如
left join (
Select distinct
o.id
,OL2.db
,case
when i.cust = 'cust2' then
case
when
lower(pdf.dept) not like 'abc%' or pdf.dept is null
then 'Yes'
else'No'
end
else 'Yes'
end as 'Show'
,otherId
from order o with(nolock)
Join instance i with(nolock) on i.id = o.id
Join orderLine ol with(nolock) on ol.id = o.SocialNetworker_Order_InstanceId
and ol.id = o.id
join product p with(nolock) on o.id = p.id
and ol.id = p.id
left join productEx pdf with(nolock) on p.id = pdf.id
and o.id = pdf.id
Where i.cust in ('cust1')
) ol2 on OL2.id = sno.id
and OL2.id2 = sno.id2
and i.db = cc.db
如何将解决方案转化为上述代码?
2条答案
按热度按时间lymgl2op1#
你可以用
row_number()
和条件聚合:r7s23pms2#
使用具有级联联接条件的外部联接:
如果其他ID少于3个,则最右边的列将为空。
这个查询几乎适用于任何数据库。