我在SQL Server中有一个名为Fields
的表:
Fields
-------------------------------------
|ID (PK)| Field Number | Field Data |
-------------------------------------
| | 3 | 123456 |
| 1 | 6 | 45678 |
| | 13 | 58963 |
-------------------------------------
| | 3 | 12347 |
| 2 | 8 | 456789 |
| | 36 | 1234 |
-------------------------------------
我尝试使用Tableau实现的目标是:
_____________________________________
| Field Number |
|_____________________________________|
| 3 | 6 | 8 | 13 | 36 |
_______|_______|______|______|______|________|
| | | | | | |
|ID(PK) | | | | | |
|_______|_______|______|______|______|________|
| | | | | | |
| 1 | 123456| 45678| NULL | 58963| NULL |
|_______|_______|______|______|______|________|
| | | | | | |
| 2 | 12347 | NULL |456789| NULL | 1234 |
|_______|_______|______|______|______|________|
提前致谢
答案 0 :(得分:2)
SELECT ID,[3], [6], [8],[13],[36]
FROM
(
SELECT ID,FIELD_NUMBER,FIELD_DATA
FROM #A
) SRC
PIVOT
(
MAX(FIELD_DATA)
FOR FIELD_NUMBER IN ([3], [6], [8],[13],[36])
) PIV;
答案 1 :(得分:2)
使用Tableau时无需为此编写SQL查询,只需连接到您的表:
首先确保ID和字段编号都是离散尺寸*,然后是
*右键单击左边距数据窗格中的字段,根据需要进行更改。
答案 2 :(得分:0)
@ Venkatesh的解决方案似乎是正确的。只是要添加,如果[FIELD_NUMBER]列有多个值,请使用以下查询生成逗号分隔值。
DECLARE @myteam VARCHAR(MAX)
SELECT @myteam = COALESCE(@myteam+', ['+CAST(ID AS VARCHAR(10))+']','['+CAST(ID AS VARCHAR(10))+']')
FROM ( SELECT DISTINCT ID FROM (VALUES(1),(2),(3),(71),(32))AS MyTable(ID) ) T;--Replace your table here
PRINT @myteam
答案 3 :(得分:0)