删除对PIVOT表的子查询

时间:2018-11-21 10:39:26

标签: sql sql-server select subquery pivot

我有一个使用子查询的查询,但是我想使其更高效,更轻松地工作。我一直在研究PIVOT逻辑,但似乎无法使其工作。以下是原始的子查询逻辑,可以有人帮助我或为我指出正确的方向。

SELECT 
    Parent.id
    ,[sitecode]
    ,[started]
    ,[completed]
    ,cast(( completed - started) as time)               
    ,[profileId]
    ,(SELECT TOP 1 CAST(RTRIM(LTRIM(REPLACE(REPLACE(CAST(Child.message as NVarchar(4000)) , 'Initialized', ''),'demographic records',''))) AS NVarchar(4000)) FROM [backend].[dbo].[Transaction_StatusMessage] AS Child WHERE Child.transactionId =Parent.Id  AND child.message like '%Initialized%' AND child.message like '%demographic records%'  ORDER BY Child.created DESC) AS Dem
    ,(SELECT TOP 1 CAST(RTRIM(LTRIM(REPLACE(REPLACE(CAST(Child.message as NVarchar(4000)) , 'Initialized', ''),'clinical records',''))) AS NVarchar(4000)) FROM [backend].[dbo].[Transaction_StatusMessage] AS Child WHERE child.message like '%Initialized%' AND child.message like '%clinical records%' AND Child.transactionId =Parent.Id ORDER BY Child.created DESC) AS Clincial        
    ,(SELECT TOP 1 CAST(RTRIM(LTRIM(REPLACE(CAST(Child.message as NVarchar(4000)),'Total valid patient types:',''))) AS NVarchar(4000)) FROM [backend].[dbo].[Transaction_StatusMessage] AS Child WHERE child.message like 'Total valid patient types:%' AND Child.transactionId =Parent.Id ORDER BY Child.created DESC) AS ValidPatientTypes
    ,(SELECT TOP 1 CAST(RTRIM(LTRIM(REPLACE(CAST(Child.message as NVarchar(4000)),'Total invalid patient types:',''))) AS NVarchar(4000)) FROM [backend].[dbo].[Transaction_StatusMessage] AS Child WHERE child.message like 'Total invalid patient types:%' AND Child.transactionId =Parent.Id ORDER BY Child.created DESC) AS NotValidPatientTypes
  FROM [Backend].[dbo].[Transaction] AS Parent
  where completed > '2018-11-17 00:00:00.00' AND transactionType = 'BulkImport' AND profileId = 122 
  order by sitecode, completed desc

任何帮助,谢谢。 即使Pivot无法正常工作,也可以有人指出我更好的逻辑

1 个答案:

答案 0 :(得分:1)

您可以将这些子查询转换为OUTER APPLY

SELECT 
    Parent.id
    ,[sitecode]
    ,[started]
    ,[completed]
    ,cast(( completed - started) as time)               
    ,[profileId]
    ,Child.Dem
    ,Child.Clincial        
    ,Child.ValidPatientTypes
    ,Child.NotValidPatientTypes
FROM    [Backend].[dbo].[Transaction] AS Parent
    OUTER APPLY
    (
        SELECT  TOP 1 
            MAX (CASE WHEN Child.message like '%Initialized%' AND Child.message like '%demographic records%'
                  THEN CAST(RTRIM(LTRIM(REPLACE(REPLACE(CAST(Child.message as NVarchar(4000)) , 'Initialized', ''),'demographic records',''))) AS NVarchar(4000))
                  END) AS Dem,
            MAX (CASE WHEN Child.message like '%Initialized%' AND child.message like '%clinical records%'
                  THEN CAST(RTRIM(LTRIM(REPLACE(REPLACE(CAST(Child.message as NVarchar(4000)) , 'Initialized', ''),'clinical records',''))) AS NVarchar(4000))
                  END) AS Clinical,
            MAX (CASE WHEN Child.message like 'Total valid patient types:%'
                  THEN CAST(RTRIM(LTRIM(REPLACE(CAST(Child.message as NVarchar(4000)),'Total valid patient types:',''))) AS NVarchar(4000)) 
                  END) AS ValidPatientTypes,
            MAX (CASE WHEN Child.message like 'Total invalid patient types:%'
                  THEN CAST(RTRIM(LTRIM(REPLACE(CAST(Child.message as NVarchar(4000)),'Total invalid patient types:',''))) AS NVarchar(4000))
                  END) AS NotValidPatientTypes
        FROM    [backend].[dbo].[Transaction_StatusMessage] AS Child 
        WHERE   Child.transactionId = Parent.Id  
        ORDER BY Child.created DESC
    ) Child
WHERE completed       > '2018-11-17 00:00:00.00' 
AND   transactionType = 'BulkImport' 
AND   profileId       = 122 
ORDER BY sitecode, completed desc