对数字和字符串非数字列进行透视

Pivot both numeric and strung non-numeric columns

我有一个特定的要求,即向上旋转两列,一列是数字,一列是字符串。我精通 SQL pivot,但是,无法为此找到解决方案。

我的原始数据是这样的:

Country | Segment | Year | Parameter_Name | Parameter_Value_Numeric | Parameter_Value_String    
USA      | 1      | 2003 | Datapoint1     | 100                     | null
USA      | 1      | 2003 | Datapoint2     | 148                     | null
USA      | 1      | 2003 | Datapoint3     | null                    | Upper values(s)
USA      | 2      | 2003 | Datapoint1     | 121                     | null
USA      | 2      | 2003 | Datapoint2     | 180                     | null
USA      | 2      | 2003 | Datapoint3     | null                    | Medium values(s)

我想要的结果是这样的:

Country  | Segment       | Year | Datapoint1     | Datapoint2     | Datapoint3
USA      | 1             | 2003 | 100            | 148            | Upper values(s)
USA      | 2             | 2003 | 121            | 180            | Medium values(s)

面临的问题是数据点 1 和数据点 2 是 float,数据点 3 是 nvarchar。因此,pivot 只能使用其中之一。

你可以试试下面的逻辑-

DEMO HERE

SELECT Country,Segment,Year,
MAX(CASE WHEN Parameter_Name = 'Datapoint1' THEN Parameter_Value_Numeric END) Datapoint1,
MAX(CASE WHEN Parameter_Name = 'Datapoint2' THEN Parameter_Value_Numeric END) Datapoint2,
MAX(CASE WHEN Parameter_Name = 'Datapoint3' THEN Parameter_Value_String END) Datapoint3
FROM your_table
GROUP BY Country,Segment,Year