英文:
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 = '' 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
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(', ',
ModifiedLineData,
LAG(ModifiedLineData) OVER(ORDER BY LineNumber))
ELSE ModifiedLineData
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 [backstreet_WMS_Optimizer].[dbo].[Temp_GoodsIn_Raw_Data]
WHERE LineData NOT LIKE ',,,0,,,,0'
) 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(', ',
ModifiedLineData,
LAG(ModifiedLineData) OVER(ORDER BY LineNumber))
ELSE ModifiedLineData
END AS ModifiedLineData
FROM <your query>) 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论