将具有唯一值的列转换为行的SQL查询
我对SQL编程相当陌生,因此发现很难实现下面给出的要求将具有唯一值的列转换为行的SQL查询,sql,pivot,Sql,Pivot,我对SQL编程相当陌生,因此发现很难实现下面给出的要求 BARCODE MATERIAL SET WEIGHT 112345 PBR1 34 112345 PBR2 34 112346 PBR11 34 112346 PBR21 34 112347 PBR43 34 所需输出 BARCODE MATERIAL SET WEIGHT MATERI
BARCODE MATERIAL SET WEIGHT
112345 PBR1 34
112345 PBR2 34
112346 PBR11 34
112346 PBR21 34
112347 PBR43 34
所需输出
BARCODE MATERIAL SET WEIGHT MATERIAL SET WEIGHT
112345 PBR1 34 PBR2 34
112346 PBR11 34 PBR21 34
112347 PBR43 34
请提供帮助您可以使用行数和条件聚合:
select
barcode,
material,
max(case when rn = 1 then set_weight end) set_weight1,
max(case when rn = 1 then material end) material1,
max(case when rn = 2 then set_weight end) set_weight2,
max(case when rn = 2 then material end) material2
from (
select
t.*,
row_number() over(partition by barcode order by material) rn
from mytable t
) t
大家好,欢迎来到SO。请添加一些代码,说明您到目前为止所做的尝试,以及您希望采用的方法。Stackoverflow并不意味着为您完成所有工作:-