Sql中的表输出 [英] Table Output in Sql
本文介绍了Sql中的表输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询输出如下。
名称价格ProductTypeCode
pre>
Ram 120 P1
RAM 130 P1
RAM 140 P1
RAM 240 P1
RAM 340 P1
RAM 190 P2
RAM 160 P2
我想将以上输出排列为:
名称P2Price P1Price
pre>
Ram 190 120
RAM 160 130
RAM null 140
RAM null 240
RAM null 340
请帮助我实现上述输出。
解决方案您可以使用
row_number()
来枚举价格。然后枢轴数据。以下查询使用条件聚合执行此操作:select name,
max(producttypecode ='p2'价格结束)作为p2price,
max(当producttypecode ='p1'然后价格结束时的情况)作为p1price
从(select t。*,
row_number()over(分区按名称,producttypecode从表t
)t
按名称,seqnum;
I have a query output as below.
Name Price ProductTypeCode Ram 120 P1 RAM 130 P1 RAM 140 P1 RAM 240 P1 RAM 340 P1 RAM 190 P2 RAM 160 P2
I want to Arrange the above output as:
Name P2Price P1Price Ram 190 120 RAM 160 130 RAM null 140 RAM null 240 RAM null 340
Please help me to achieve the above output.
解决方案You can use
row_number()
to enumerate the prices. Then pivot the data. The following query does this using conditional aggregation:select name, max(case when producttypecode = 'p2' then price end) as p2price, max(case when producttypecode = 'p1' then price end) as p1price from (select t.*, row_number() over (partition by name, producttypecode order by name) as seqnum from table t ) t group by name, seqnum;
这篇关于Sql中的表输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文