我们有一些功能可以将一些记录导出到我们的数据库中。在导出任何错误时,我们正在登录我们的数据库。
这是日志表数据:
现在我需要以下列方式显示这些记录:
这是您可以用来测试的SQL查询
CREATE TABLE #TEMP
(
ExportId INT,
AssigneeID INT,
HomeAssignmentID INT,
HostAssignmentID INT,
Error NVARCHAR(MAX)
)
INSERT INTO #TEMP
SELECT * FROM (
SELECT 1123 AS ExportId,3234 AS AssigneeID,0 AS HomeAssignmentID,0 AS HostAssignmentID ,'Gender does not exist' AS Error
UNION ALL
SELECT 1123 AS ExportId,3234 AS AssigneeID,2043 AS HomeAssignmentID ,0 AS HostAssignmentID,'Error while processing Assignee' AS Error
UNION ALL
SELECT 1123 AS ExportId,3234 AS AssigneeID,2043 AS HomeAssignmentID ,1768 AS HostAssignmentID,'Error while processing Home Assignment' AS Error
UNION ALL
SELECT 1123 AS ExportId,3234 AS AssigneeID,2044 AS HomeAssignmentID ,0 AS HostAssignmentID,'Error while processing Assignee' AS Error
UNION ALL
SELECT 1123 AS ExportId,3234 AS AssigneeID,2044 AS HomeAssignmentID ,1769 AS HostAssignmentID,'Error while processing Home Assignment' AS Error
UNION ALL
SELECT 1123 AS ExportId,3577 AS AssigneeID,2058 AS HomeAssignmentID ,0 AS HostAssignmentID,'Home Country does not exist in master data.' AS Error
UNION ALL
SELECT 1123 AS ExportId,3577 AS AssigneeID,2058 AS HomeAssignmentID ,2011 AS HostAssignmentID,'Host Country does not exist in master data.' AS Error
UNION ALL
SELECT 1123 AS ExportId,3577 AS AssigneeID,2059 AS HomeAssignmentID ,0 AS HostAssignmentID,'Home Country does not exist in master data.' AS Error
UNION ALL
SELECT 1123 AS ExportId,3577 AS AssigneeID,2059 AS HomeAssignmentID ,2012 AS HostAssignmentID,'Error occurred in Assignment Home processing.' AS Error
UNION ALL
SELECT 1123 AS ExportId,3578 AS AssigneeID,2060 AS HomeAssignmentID ,2015 AS HostAssignmentID,'Host Country does not exist in master data.' AS Error
) AS X
答案 0 :(得分:1)
根据您的数据,您可以使用窗口函数和排序列获取分组列:
select t.*, t2.error
from (select distinct exportid, assigneeid, HomeAssignmentID, HostAssignmentID
from t
where HomeAssignmentID <> 0 and HostAssignmentID <> 0
) join
t t2
on t2.exportid = t.exportid and t2.assigneeid = t.assigneeid and
(t2.HomeAssignmentID = t.HomeAssignmentID or t2.HomeAssignmentID = 0)
这实际上为每个错误添加了四列。由于数据的性质,给定的错误可能会附加到多行。
组由第一个子查询中的四列定义。
在SQL Server 2017+中,您可以将其放入聚合查询中:
select exportid, assigneeid, HomeAssignmentID, HostAssignmentID,
string_agg(error, '
') within group (order by hostassignmentid, homeassignmentid) as error
from (select t.*, t2.error
from (select distinct exportid, assigneeid, HomeAssignmentID, HostAssignmentID
from #temp 5
where HomeAssignmentID <> 0 and HostAssignmentID <> 0
) join
#temp t2
on t2.exportid = t.exportid and t2.assigneeid = t.assigneeid and
(t2.HomeAssignmentID = t.HomeAssignmentID or t2.HomeAssignmentID = 0)
) t
) t
group by exportid, assigneeid, HomeAssignmentID, HostAssignmentID;
在早期版本中,您需要XML:
with t as (
select t.*, t2.error
from (select distinct exportid, assigneeid, HomeAssignmentID, HostAssignmentID
from t
where HomeAssignmentID <> 0 and HostAssignmentID <> 0
) join
t t2
on t2.exportid = t.exportid and t2.assigneeid = t.assigneeid and
(t2.HomeAssignmentID = t.HomeAssignmentID or t2.HomeAssignmentID = 0)
)
select exportid, assigneeid, HomeAssignmentID,
stuff( (select '
' + error
from t t2
where t2.exportid = t.exportid and t2.assigneeid = t.assigneeid and
t2.HomeAssignmentID = t.HomeAssignmentID and
t2.HostAssignmentID = t.HostAssignmentID
order by t2.hostassignmentid, t2.homeassignmentid
for xml path ('') type
).value('.', 'varchar(max)'
), 1, 1, ''
) as error
from (select distinct exportid, assigneeid, HomeAssignmentID, HostAssignmentID
from t
) t;