Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 删除数据透视表的子查询_Sql_Sql Server_Select_Subquery_Pivot - Fatal编程技术网

Sql 删除数据透视表的子查询

Sql 删除数据透视表的子查询,sql,sql-server,select,subquery,pivot,Sql,Sql Server,Select,Subquery,Pivot,我有一个使用子查询的查询,但我想让它更高效、更容易工作。我一直在研究PIVOT逻辑,但似乎无法使其发挥作用。下面是原始的子查询逻辑,是否有人可以帮助我或为我指出正确的方向 SELECT Parent.id ,[sitecode] ,[started] ,[completed] ,cast(( completed - started) as time) ,[profileId] ,(SELECT TOP 1 C

我有一个使用子查询的查询,但我想让它更高效、更容易工作。我一直在研究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不起作用,有人能给我指出一个更好的逻辑吗

您可以将这些子查询转换为
外部应用

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

谢谢松鼠,这很有帮助。我遇到一个问题,子查询返回null,我想从主结果中删除该行。。再次感谢