查询以列而不是行返回记录值?

时间:2018-05-03 18:01:02

标签: sql sql-server tsql

我在创建查询时需要帮助。我的客户端表具有唯一的客户端信息 - 由其唯一的ClientID标识。我还有一个Client_UserDefinedFields表,其中包含客户端的自定义数据值。它们通过ClientID链接,并且此Client_UserDefinedFields表中的ClientID可能有许多记录。

我的情况是,我需要知道3个自定义数据字段,以了解给定客户端的值(如我的CASE语句所示)。我当前的查询是将客户端带回3次(每个值一行),我想只看到客户端一次(一行)并将这些值显示为列。不确定这是否可行或如何做到这一点。此外,当我尝试在我的选择中使用CASE语句时,我不能使用AS' fieldname'识别它 - 因为它给了我关于AS关键字的错误。

我当前的SQL SELECT语句的一个示例

    SELECT
        c.ClientID
      , c.LastName
      , c.FirstName
      , c.MiddleName
      , CASE WHEN cudf.UserDefinedFieldFormatULink = '93fb3820-38aa-4655-8aad-a8dce8aede' THEN cudf.UDF_ReportValue --AS 'DA Status'
             WHEN cudf.UserDefinedFieldFormatULink = '2144a742-08c5-4c96-b9e4-d6f1f56c76' THEN cudf.UDF_ReportValue --AS 'FHAP Status'
             WHEN cudf.UserDefinedFieldFormatULink = 'c3d29be9-af58-4241-a02d-9ae9b43ffa' THEN cudf.UDF_ReportValue --AS 'HCRA Status'
        END
    FROM Client_Program cp
    INNER JOIN client c ON c.ulink = cp.clientulink
    INNER JOIN code_program p ON p.ulink = cp.programulink
    INNER JOIN Code_System_State css ON c.ContactMailingStateUlink = css.ulink 
    INNER JOIN Code_ClientStatus ccs ON c.ClientStatusULink = ccs.ULink
    INNER JOIN Client_UserDefinedField cudf ON c.ULink = cudf.ClientULink
    AND cp.ProgramStatusULink = '1' -- Open (active) program
    AND c.ClientStatusULink = '10000000' --Active client
    AND cp.programulink in ('7280f4a7-cd94-49be-86ad-a74421ff6f',
      '0a9b94a3-edd7-4918-b79c-bf2b20f9da',
      '54f6c691-2eba-49e5-8380-85f5349bca',
      'ed8c497d-d4fe-41d7-a218-4235fd0734',
      '5be826f0-b3c3-4ebe-871d-4d20b56da5')
     AND cudf.UserDefinedFieldFormatULink IN ('93fb3820-38aa-4655-8aad-a8dce8aede', -- DA Status
                                              '2144a742-08c5-4c96-b9e4-d6f1f56c76', --FHAP Status
'c3d29be9-af58-4241-a02d-9ae9b43ffa') --HCRA Status

同样,我的问题是,如果在Client_UserDefinedFields表中有多个条目,我不想多次带回同一个客户端。我希望将每个" Status"字段正确填充为列。我该怎么做呢?这是我当前输出的样本:

ClientID        LastName    FirstName   MiddleName  PCHP/HCH Status DA Status   FHAP Status         HCRA Status         
XXXXXXXXXXXX    River       Mike        Allan       Active          (null)      - None Selected -   (null)              
XXXXXXXXXXXX    River       Mike        Allan       Active          Active      (null)              (null)              
XXXXXXXXXXXX    River       Mike        Allan       Active          (null)      (null)              - None Selected -   

最终希望只看到包含所有值的一条记录

ClientID        LastName    FirstName   MiddleName  PCHP/HCH Status DA Status   FHAP Status         HCRA Status         
XXXXXXXXXXXX    River       Mike        Allan       Active          Active      - None Selected -   - None Selected -

示例非常有用,因为我不是SQL大师。谢谢!

3 个答案:

答案 0 :(得分:0)

这是未经测试的代码 - 但PIVOT命令可以正常工作:

SELECT
c.ClientID
, c.LastName
, c.FirstName
, c.MiddleName
, CASE WHEN cudf.UserDefinedFieldFormatULink = '93fb3820-38aa-4655-8aad-a8dce8aede' THEN cudf.UDF_ReportValue --AS 'DA Status'
     WHEN cudf.UserDefinedFieldFormatULink = '2144a742-08c5-4c96-b9e4-d6f1f56c76' THEN cudf.UDF_ReportValue --AS 'FHAP Status'
     WHEN cudf.UserDefinedFieldFormatULink = 'c3d29be9-af58-4241-a02d-9ae9b43ffa' THEN cudf.UDF_ReportValue --AS 'HCRA Status'
   END
 FROM Client_Program cp
INNER JOIN code_program p ON p.ulink = cp.programulink
INNER JOIN Code_System_State css ON c.ContactMailingStateUlink = css.ulink 
INNER JOIN Code_ClientStatus ccs ON c.ClientStatusULink = ccs.ULink
INNER JOIN client c ON c.ulink = cp.clientulink
INNER JOIN (
    select * 
    from ( select ClientULink, UserDefinedFieldFormatULink , ROW_NUMBER() OVER (PARTITION BY ClientULink ORDER BY (SELECT NULL)) RN
            from Client_UserDefinedField
            where UserDefinedFieldFormatULink IN ('93fb3820-38aa-4655-8aad-a8dce8aede', -- DA Status
                 '2144a742-08c5-4c96-b9e4-d6f1f56c76')--FHAP Status
        ) as c
        PIVOT (
            MAX(UserDefinedFieldFormatULink) FOR RN IN ([1],[2],[3])
        ) as pvt
    ) cudf ON c.ULink = cudf.ClientULink
AND cp.ProgramStatusULink = '1' -- Open (active) program
AND c.ClientStatusULink = '10000000' --Active client
AND cp.programulink in ('7280f4a7-cd94-49be-86ad-a74421ff6f',
   '0a9b94a3-edd7-4918-b79c-bf2b20f9da',
  '54f6c691-2eba-49e5-8380-85f5349bca',
  'ed8c497d-d4fe-41d7-a218-4235fd0734',
  '5be826f0-b3c3-4ebe-871d-4d20b56da5')

答案 1 :(得分:0)

如果我理解你的问题,那么从我这一边就是解决方案

 SELECT DISTINCT
    c.ClientID
  , c.LastName
  , c.FirstName
  , c.MiddleName
  , (CASE WHEN cudf.UserDefinedFieldFormatULink = '93fb3820-38aa-4655-8aad-a8dce8aede' THEN cudf.UDF_ReportValue ELSE NULL END)  AS 'DA Status'
    ,(CASE WHEN cudf.UserDefinedFieldFormatULink = '2144a742-08c5-4c96-b9e4-d6f1f56c76' THEN cudf.UDF_ReportValue ELSE NULL END)  AS 'FHAP Status'
    ,(CASE WHEN cudf.UserDefinedFieldFormatULink = 'c3d29be9-af58-4241-a02d-9ae9b43ffa' THEN cudf.UDF_ReportValue ELSE NULL END) AS 'HCRA Status'

FROM Client_Program cp
INNER JOIN client c ON c.ulink = cp.clientulink
INNER JOIN code_program p ON p.ulink = cp.programulink
INNER JOIN Code_System_State css ON c.ContactMailingStateUlink = css.ulink 
INNER JOIN Code_ClientStatus ccs ON c.ClientStatusULink = ccs.ULink
INNER JOIN Client_UserDefinedField cudf ON c.ULink = cudf.ClientULink
AND cp.ProgramStatusULink = '1' -- Open (active) program
AND c.ClientStatusULink = '10000000' --Active client
AND cp.programulink in ('7280f4a7-cd94-49be-86ad-a74421ff6f',
  '0a9b94a3-edd7-4918-b79c-bf2b20f9da',
  '54f6c691-2eba-49e5-8380-85f5349bca',
  'ed8c497d-d4fe-41d7-a218-4235fd0734',
  '5be826f0-b3c3-4ebe-871d-4d20b56da5')
 AND cudf.UserDefinedFieldFormatULink IN ('93fb3820-38aa-4655-8aad-a8dce8aede', -- DA Status
                                          '2144a742-08c5-4c96-b9e4-d6f1f56c76')--FHAP Status

答案 2 :(得分:0)

SELECT c.ClientID,
   c.LastName,
   c.FirstName,
   c.MiddleName,
   CASE
       WHEN cudf.UserDefinedFieldFormatULink = '93fb3820-38aa-4655-8aad-a8dce8aede' THEN
           cudf.UDF_ReportValue AS 'DA Status'
       WHEN cudf.UserDefinedFieldFormatULink = '2144a742-08c5-4c96-b9e4-d6f1f56c76' THEN
           cudf.UDF_ReportValue AS 'FHAP Status'
       WHEN cudf.UserDefinedFieldFormatULink = 'c3d29be9-af58-4241-a02d-9ae9b43ffa' THEN
           cudf.UDF_ReportValue AS 'HCRA Status'
   END
INTO #Temp
FROM Client_Program cp
INNER JOIN client c
    ON c.ulink = cp.clientulink
INNER JOIN code_program p
    ON p.ulink = cp.programulink
INNER JOIN Code_System_State css
    ON c.ContactMailingStateUlink = css.ulink
INNER JOIN Code_ClientStatus ccs
    ON c.ClientStatusULink = ccs.ULink
INNER JOIN Client_UserDefinedField cudf
    ON c.ULink = cudf.ClientULink
       AND cp.ProgramStatusULink = '1' -- Open (active) program
       AND c.ClientStatusULink = '10000000' --Active client
       AND cp.programulink IN ( '7280f4a7-cd94-49be-86ad-a74421ff6f', '0a9b94a3-edd7-4918-b79c-bf2b20f9da',
                                '54f6c691-2eba-49e5-8380-85f5349bca', 'ed8c497d-d4fe-41d7-a218-4235fd0734',
                                '5be826f0-b3c3-4ebe-871d-4d20b56da5'
                              )
       AND cudf.UserDefinedFieldFormatULink IN (   '93fb3820-38aa-4655-8aad-a8dce8aede', -- DA Status
                                                   '2144a742-08c5-4c96-b9e4-d6f1f56c76'
                                               ); --FHAP Status

SELECT COALESCE(A.CilentID, B.ClientID, C.ClientID),LastName,
   FirstName, MiddleName, [DA Status], [FHAP Status], [HCRA Status] FROM 
(SELECT ClientID, LastName, FirstName, [DA Status] FROM #Temp WHERE [DA Status] IS NOT NULL) AS A FULL OUTER JOIN  
(SELECT ClientID, LastName, FirstName, [FHAP Status] FROM #Temp WHERE [FHAP Status] IS NOT NULL) AS B ON A.ClientID = B.ClientID FULL OUTER JOIN 
(SELECT ClientID, LastName, FirstName, [HCRA Status] FROM #Temp WHERE [HCRA Status] IS NOT NULL) AS C ON C.ClientID = A.ClientID