[英]Need to transpose Table Data with adding 0 for non existing column
I have data as follows - 我有以下数据-
FieldValue FiledID UnqiueID
Name1 13 1
Address1 14 1
NAme2 13 2
Address2 14 2
Name3 13 3
Address3 14 3
Date1 15 3
Date2 16 3
Date3 17 3
I would like to transpose it. 我想换位。 My outcome will be as follows - 我的结果如下:
Name1 Address1 0 0 0 名称1地址1 0 0 0
Name2 Address2 0 0 0 名称2地址2 0 0 0
Name3 Address3 Date1 Date2 Date3 名称3地址3日期1日期2日期3
I was trying to do it through PIVOT, but no success. 我试图通过PIVOT做到这一点,但没有成功。 Like to achieve it through SQL not preferring any SP. 喜欢通过SQL实现它,而不希望使用任何SP。
I would be inclined to do this with conditional aggregation instead of pivot
: 我倾向于使用条件聚合而不是pivot
来做到这一点:
select max(case when FieldId = 13 then FieldValue end) as Name,
max(case when FieldId = 14 then FieldValue end) as Address,
max(case when FieldId = 15 then FieldValue end) as Date1,
max(case when FieldId = 16 then FieldValue end) as Date2,
max(case when FieldId = 17 then FieldValue end) as Date3
from table t
group by uniqueid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.