如何基于第一个查询运行第二个查询?

时间:2019-06-10 06:20:19

标签: sql sql-server tsql

enter image description here我使用两个查询,第一个将一列分隔为两列,然后插入一个表,然后根据插入的表进行第二个查询(PIVOT)获取,

第一个查询

SELECT A.MDDID, A.DeviceNumber,  
          Split.a.value('.', 'VARCHAR(100)') AS MetReading
      FROM  (
             SELECT MDDID,DeviceNumber,  
                    CAST ('<M>' + REPLACE(Httpstring, ':', '</M><M>') + '</M>' AS XML) AS MetReading  
               FROM  [IOTDBV1].[dbo].[MDASDatas] E
               Where E.MDDID = 49101
             ) AS A CROSS APPLY MetReading.nodes ('/M') AS Split(a);

第二次查询

SELECT * FROM 
        (
           Select ID,MDDID,DeviceNumber,ReceivedDate
            , ROW_NUMBER() OVER(PARTITION BY ID ORDER BY (SELECT 1)) AS ID2
            , SPLT.MR.value('.','VARCHAR(MAX)') AS LIST FROM ( 
                        Select ID,MDDID,DeviceNumber,ReceivedDate
                                    , CAST( '<M>'+REPLACE(MeterReading,',','</M><M>')+'</M>' AS XML) AS XML_MR 
                                    From [dbo].[PARSEMDASDatas] E
                                    Where E.MeterReading is Not Null
                                    )E
                                    CROSS APPLY  E.XML_MR.nodes('/M') AS SPLT(MR)
                                    )A
                                    PIVOT
                                    (
                                        MAX(LIST) FOR ID2 IN ([1],[2],[3],[4],[5],[6],[7],[8])
                                    )PV

我希望第二个查询基于第一个查询运行,不需要表。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:9)

您的问题不是很清楚...这是一个很好的例子,为什么您总是应该添加MCVE,包括DDL,样本数据,自己的尝试,错误的输出和预期的输出。这次我为您这样做,请下次自己尝试准备这样的MCVE ...

如果我正确理解了这一点,则您的源表将包含一个CSV列,该列最多包含8个(最大值?)值。这可能更容易解决,无需将其分解为两个查询,不需要中间表,甚至不需要PIVOT。

--create a mockup-table to simulate your situation (slightly shortened for brevity)

DECLARE @YourTable TABLE(ID INT,MDDID INT, DeviceNumber VARCHAR(100),MetReading VARCHAR(2000));
INSERT INTO @YourTable VALUES
 (2,49101,'NKLDEVELOPMENT02','DCPL,981115,247484,9409') --the character code and some numbers
,(3,49101,'NKLDEVELOPMENT02','SPPL,,,,,,,,')            --eigth empty commas
,(4,49101,'NKLDEVELOPMENT02','BLAH,,,999,,');           --A value somewhere in the middle

-CTE将按原样返回表 。唯一的不同是对XML的强制转换(也是如此)

WITH Splitted AS
(
    SELECT ID
          ,MDDID
          ,DeviceNumber
          ,CAST('<x>' + REPLACE(MetReading,',','</x><x>') + '</x>' AS XML) AS Casted
    FROM @YourTable t
)
SELECT s.ID
      ,s.MDDID
      ,s.DeviceNumber
      ,s.Casted.value('/x[1]','varchar(100)') AS [1]
      ,s.Casted.value('/x[2]','varchar(100)') AS [2]
      ,s.Casted.value('/x[3]','varchar(100)') AS [3]
      ,s.Casted.value('/x[4]','varchar(100)') AS [4]
      ,s.Casted.value('/x[5]','varchar(100)') AS [5]
      ,s.Casted.value('/x[6]','varchar(100)') AS [6]
      ,s.Casted.value('/x[7]','varchar(100)') AS [7]
      ,s.Casted.value('/x[8]','varchar(100)') AS [8]
FROM Splitted s;

结果

ID  MDDID   DeviceNumber        1       2       3       4       5       6       7       8
2   49101   NKLDEVELOPMENT02    DCPL    981115  247484  9409    NULL    NULL    NULL    NULL
3   49101   NKLDEVELOPMENT02    SPPL                            
4   49101   NKLDEVELOPMENT02    BLAH                    999                     NULL    NULL

简而言之:

每个CSV都转换为类似于以下内容的XML:

<x>DCPL</x>
<x>981115</x>
<x>247484</x>
<x>9409</x>

使用XPath中的位置谓词,我们可以轻松地调用第一,第二和第三<x>

答案 1 :(得分:0)

CTE:WITH common_table_expression是答案

您可以在第一个查询中准备一些数据,然后在第二个查询中准备用户

 WITH cte_table AS
    (
        SELECT  *
        FROM sys.objects
    )
    SELECT *
    FROM cte_table
    where name  like 'PK%'