如何在SQL Server数据透视表中获取动态不同类型的数据类型值

时间:2016-08-15 17:34:38

标签: sql-server datetime subquery pivot inner-join

我有以下表格,其中包含标题信息:

InformationID  Name               DispalyName                         VAlueType
1              Occupation         Occupation                          Text 
3              Surgery            Surgery                             Header
4              SurgeryType        SurgeryType                         Text
5              SurgeryDate        SurgeryDate                         Datetime
8              Rehabilitation     Rehabilitation                      Header
9              RehabilitationPT   Rehabilitation Physiotherapist      Text
10             RehabilitationDate Rehabilitation Date                 DateTime
11             DischargeSession   Discharge Session Number            int
12             Height             Height                              decimal
13             Weight             Weight                              decimal

I have another table has all these rows values stored like this:

InfomationID  ClientID    TextValue          NumberValue     DateValue
1             XXXX        Programmer         null            null
4             XXXX        Left knee surgery  null            null
5             XXXX        null               null            2016-08-01 01:00:00
11            XXXX        null               6               null
12            XXXX        null               164.592         null

现在我需要在ClientID = XXXX的子查询中获取它们 部分结果如下:

CFname| CLname| Occupation| SurgeryType      | SurgeryDate| DischargeSession
-------------------------------------------------------------------------------
CFName| CLname| Programmer| Left knee surgery| 2016-08-01 | 6
::::
::::
More records 

此致

1 个答案:

答案 0 :(得分:0)

你只需要将数字和日期值转换为varchar ..你可以使用coalesce来获得第一个非null值。

你的支点看起来像这样

SELECT  *
FROM (
        SELECT  c.CFName,
                c.CLName,
                fh.Name,
                COALESCE(fv.TextValue, 
                         CAST(fv.NumberValue AS VARCHAR(50)), -- convert numbers to text
                         CONVERT(VARCHAR(10), fv.DateValue, 101) AS DynamicValue -- convert date to text 
                                                                                 -- increase varchar length for time
                                                                                 -- or 101 for different format
        FROM    ClientInfo c
        JOIN    FieldValues fv ON c.ClientID = fv.ClientID
        JOIN    FieldHeaders fh ON fh.InformationID = fv.InformationID
) t
PIVOT (
    MAX(DynamicValue)
    FOR fh.Name IN ([Occupation], [SurgeryType], [SurgeryDate], [DischargeSession])
) p

要动态地执行此操作,您需要构建您的sql并通过使用STUFF(FOR XML)将值注入nvarchar来将数据库列注入到sql中

DECLARE @Sql NVARCHAR(MAX),
        @Columns NVARCHAR(MAX)

SELECT  @Columns = STUFF((
                        SELECT  ',' + QUOTENAME(Name)
                        FROM    FieldHeaders
                        FOR XML PATH('')
                    ), 1, 1, 0)
SET     @Sql = N'
    SELECT  *
    FROM (
            SELECT  c.CFName,
                    c.CLName,
                    fh.Name,
                    COALESCE(fv.TextValue, 
                             CAST(fv.NumberValue AS VARCHAR(50)), -- convert numbers to text
                             CONVERT(VARCHAR(10), fv.DateValue, 101) AS DynamicValue -- convert date to text 
                                                                                     -- increase varchar length for time
                                                                                     -- or 101 for different format
            FROM    ClientInfo c
            JOIN    FieldValues fv ON c.ClientID = fv.ClientID
            JOIN    FieldHeaders fh ON fh.InformationID = fv.InformationID
    ) t
    PIVOT (
        MAX(DynamicValue)
        FOR fh.Name IN (' + @Columns + ')
    ) p
'
EXEC(@Sql)