连接到下一行

huangapple go评论145阅读模式
英文:

Concatenate onto Next Row

问题

以下是修改后的查询代码:

SELECT *
FROM (
    SELECT FileName, LineNumber, LineData, Column2,
        CASE
            WHEN Column2 IS NULL THEN CONCAT_WS(',', ModifiedLineData, LAG(ModifiedLineData) OVER (ORDER BY LineNumber))
            ELSE CONCAT_WS(',', ModifiedLineData, ISNULL(Column2, '""'))
        END AS ModifiedLineData
    FROM (
        SELECT *,
            ModifiedLineData = CASE
                WHEN Column2 = '' AND LineData NOT LIKE ',,,0,,,,0'
                    THEN CONCAT(STUFF(LineData, CHARINDEX(',', LineData, CHARINDEX(',', LineData) + 1), 0, '"No PO Number"'), '')
                ELSE CONCAT(LineData, '')
            END
        FROM (
            SELECT *,
                Column2 = CONVERT(XML, '<s>' + REPLACE((SELECT ISNULL(LineData, '') FOR XML PATH('')), ',', '</s><s>') + '</s>').value('/s[2]', 'varchar(100)')
            FROM [dbo].[Temp_Raw_Data]
            WHERE LineData NOT LIKE ',,,0,,,,0'
        ) AS Subquery
    ) AS cte
) AS Subquery
WHERE Column2 IS NOT NULL
ORDER BY FileName, LineNumber

这个修改后的查询会按照您的要求,如果Column2为NULL,则将LineData连接到下一行的ModifiedLineData中,并且在连接的时候会处理NULL值,使结果看起来像您描述的那样。

英文:

I have some SQL that does some manipulation to the data i.e. filling in empty columns.

SELECT *,
    ModifiedLineData = CASE
        WHEN Column2 = &#39;&#39; AND LineData NOT LIKE &#39;,,,0,,,,0&#39;
            THEN CONCAT(STUFF(LineData, CHARINDEX(&#39;,&#39;, LineData, CHARINDEX(&#39;,&#39;, LineData) + 1), 0, &#39;&quot;No PO Number&quot;&#39;), &#39;,&quot;&quot;&#39;)
        ELSE CONCAT(LineData, &#39;,&quot;&quot;&#39;)
    END
FROM (
    SELECT
        *,
        Column2 = CONVERT(XML, &#39;&lt;s&gt;&#39; + REPLACE((SELECT ISNULL(LineData, &#39;&#39;) FOR XML PATH(&#39;&#39;)), &#39;,&#39;, &#39;&lt;/s&gt;&lt;s&gt;&#39;) + &#39;&lt;/s&gt;&#39;).value(&#39;/s[2]&#39;, &#39;varchar(100)&#39;)
    FROM [dbo].[Temp_Raw_Data]
    WHERE LineData NOT LIKE &#39;,,,0,,,,0&#39;
) AS Subquery

Now lets say this returns

FileName LineNumber LineData Column2 ModifiedLineData
file1 4 1232,,"product-1", 1,0 1232,NA,"product-1", 1,0
file2 7 "failed" NULL "failed"
file3 8 1235,,"product-2", 1,0 1235,NA,"product-2", 1,0

How can I modify this query so that if Column2 is NULL then it would concatenate the LineData onto the next row (ModifiedLineData) else just concatenate a ,"" and then remove that NULL result (if possible else it doesnt matter) so that my result would look like:

FileName LineNumber LineData Column2 ModifiedLineData
file1 4 1232,,"product-1", 1,0 1232,NA,"product-1", 1,0,""
file3 8 1235,,"product-2", 1,0 1235,NA,"product-2", 1,0,"failed"

I tried playing around with LEAD() but couldn't get it how i wanted.

Note: Two null rows are not possible to be together. This is due to the nature of the data. The next row should simply be the next available row when selecting all rows as they are imported one by 1.

Updated Query that isn't concatenating:

SELECT * 
  FROM (SELECT FileName, LineNumber, LineData, Column2, 
               CASE WHEN LAG(Column2) OVER(ORDER BY LineNumber) IS NULL
                    THEN CONCAT_WS(&#39;, &#39;,
                                ModifiedLineData, 
                                LAG(ModifiedLineData) OVER(ORDER BY LineNumber))
                    ELSE ModifiedLineData
               END AS ModifiedLineData
        FROM (
        SELECT *,
            ModifiedLineData = CASE
                WHEN Column2 = &#39;&#39; AND LineData NOT LIKE &#39;,,,0,,,,0&#39;
                    THEN CONCAT(STUFF(LineData, CHARINDEX(&#39;,&#39;, LineData, CHARINDEX(&#39;,&#39;, LineData) + 1), 0, &#39;&quot;No PO Number&quot;&#39;), &#39;&#39;)
                ELSE CONCAT(LineData, &#39;&#39;)
            END
        FROM (
            SELECT *,
                Column2 = CONVERT(XML, &#39;&lt;s&gt;&#39; + REPLACE((SELECT ISNULL(LineData, &#39;&#39;) FOR XML PATH(&#39;&#39;)), &#39;,&#39;, &#39;&lt;/s&gt;&lt;s&gt;&#39;) + &#39;&lt;/s&gt;&#39;).value(&#39;/s[2]&#39;, &#39;varchar(100)&#39;)
            FROM [backstreet_WMS_Optimizer].[dbo].[Temp_GoodsIn_Raw_Data]
            WHERE LineData NOT LIKE &#39;,,,0,,,,0&#39;
        ) AS Subquery
    ) AS cte
) AS Subquery
WHERE Column2 IS NOT NULL
order by FileName, LineNumber

答案1

得分: 3

鉴于不能连续存在NULL值,使用LEAD/LAG应该适用于此任务。在没有原始数据的情况下,我们可以处理您的查询,并在其上添加两个子查询,最后一个是可选的:

  • 内部查询添加了记录在"Column2=NULL"记录之后所需的信息
  • 外部查询删除了具有这些空值的记录
SELECT * 
FROM (SELECT FileName, LineNumber, LineData, Column2, 
             CASE WHEN LAG(Column2) OVER(ORDER BY LineNumber) IS NULL
                  THEN CONCAT_WS(', ',
                              ModifiedLineData, 
                              LAG(ModifiedLineData) OVER(ORDER BY LineNumber))
                  ELSE ModifiedLineData
             END AS ModifiedLineData
      FROM <your query>) cte
WHERE Column2 IS NOT NULL 

输出

FileName LineNumber LineData Column2 ModifiedLineData
file1 4 1232,,"product-1", 1,0 1232,NA,"product-1", 1,0
file3 8 1235,,"product-2", 1,0 1235,NA,"product-2", 1,0"failed"

查看演示这里

英文:

Given that you can't have consecutive NULL values, using LEAD/LAG should be suitable for this task. Without knowledge of your original data, we can work on your query and add on top two subqueries, last of which is optional:

  • the inner adds the information needed to the record successive to "Column2=NULL" records
  • the outer removes records having those null values
SELECT * 
  FROM (SELECT FileName, LineNumber, LineData, Column2, 
               CASE WHEN LAG(Column2) OVER(ORDER BY LineNumber) IS NULL
                    THEN CONCAT_WS(&#39;, &#39;,
                                ModifiedLineData, 
                                LAG(ModifiedLineData) OVER(ORDER BY LineNumber))
                    ELSE ModifiedLineData
               END AS ModifiedLineData
        FROM &lt;your query&gt;) cte
WHERE Column2 IS NOT NULL 

Output:

FileName LineNumber LineData Column2 ModifiedLineData
file1 4 1232,,"product-1", 1,0 1232,NA,"product-1", 1,0
file3 8 1235,,"product-2", 1,0 1235,NA,"product-2", 1,0"failed"

Check the demo here.

huangapple
  • 本文由 发表于 2023年6月2日 00:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定