如何使用 SQL Server 在一行中检索特定 id 的多条记录
Posted
技术标签:
【中文标题】如何使用 SQL Server 在一行中检索特定 id 的多条记录【英文标题】:How to retrieve multiple records of a particular id in one row using SQL Server 【发布时间】:2017-03-22 12:14:21 【问题描述】:我有3张表如下
问题
issue_num issue_desc issue_status issue_date
-----------------------------------------------------------------------
abc123 login issue 1 2017-03-15 00:00:00.000
abc345 session issue 1 2017-03-15 00:00:00.000
abc334 logeger issue 1 2017-03-15 00:00:00.000
用户
id user_id user_first_name user_last_name user_email_id
--------------------------------------------------------------------------
1 yash123 yashn ray yash@gmail.com
2 ray234 raah kumar raah@yahoo.com
3 aniket aniket pal aniket@yahoo.com
分配的问题
issue_num assigned_to_user_id comment assign_date
------------------------------------------------------------------------------------
abc123 1 replicating issue 2017-03-15 00:00:00.000
abc123 2 replicating issue 2017-03-15 00:00:00.000
abc345 2 replicating issue 2017-03-15 00:00:00.000
abc345 3 replicating issue 2017-03-15 00:00:00.000
abc334 3 replicating issue 2017-03-15 00:00:00.000
如果您签入Issue_Assigned
表,则会将一些问题分配给 2 个用户。我想在我的预期结果中写一个查询
Issue_number issue_desc assigned_to_user_id comment assign_date
---------------------------------------------------------------------------------------------------------
abc123 login issue yash123,ray234 replicating issue 2017-03-15 00:00:00.000
abc345 session issue ray234,aniket replicating issue 2017-03-15 00:00:00.000
abc334 session issue aniket replicating issue 2017-03-15 00:00:00.000
【问题讨论】:
到目前为止你做了什么?提示:查看***.com/q/9726660/1662973 以获得创建实际 CSV 列表的一些答案。 这是假设如果多个用户被分配一个问题,他们将有相同的分配评论和分配日期? 您可以查看接受的答案here. @DhruvJoshi 不,我只是写了它,例如 【参考方案1】:使用stuff()
with select ... for xml path ('')
method of string concatenation。
select
issue_num
, issue_desc
, assigned_to_user_id = stuff((
select distinct ','+u.user_id
from Users u
inner join IssueAssigned sia
on u.id = sia.assigned_to_user_id
where sia.issue_num = i.issue_num
for xml path (''), type).value('.','nvarchar(max)')
,1,1,'')
, x.comment
, x.assign_date
from Issues i
cross apply (
select top 1 ia.comment, ia.assign_date
from IssueAssigned ia
where ia.issue_num = i.issue_num
) as x
rextester 演示:http://rextester.com/QFVR26057
返回:
+-----------+---------------+---------------------+-------------------+---------------------+
| issue_num | issue_desc | assigned_to_user_id | comment | assign_date |
+-----------+---------------+---------------------+-------------------+---------------------+
| abc123 | login issue | ray234,yash123 | replicating issue | 2017-03-15 00:00:00 |
| abc345 | session issue | aniket,ray234 | replicating issue | 2017-03-15 00:00:00 |
| abc334 | logeger issue | aniket | replicating issue | 2017-03-15 00:00:00 |
+-----------+---------------+---------------------+-------------------+---------------------+
感谢 Gouri Shankar Aechoor 编写样本数据。
【讨论】:
在第 4 行、第 10 行和 as 中获取 '=' 的语法异常 @Suyash 更新了一个演示 我正在尝试在 ms sql 管理工作室中运行相同的操作,但出现错误 @Suyash 乐于助人!【参考方案2】:希望对你有帮助
;WITH cte_Issues(issue_num,issue_desc,issue_status,issue_date) AS
(
SELECT 'abc123','login issue','1','2017-03-15 00:00:00.000' UNION ALL
SELECT 'abc345','session issue','1','2017-03-15 00:00:00.000' UNION ALL
SELECT 'abc334','logeger issue','1','2017-03-15 00:00:00.000'
)
,cte_users(id,user_id,user_first_name,user_last_name,user_email_id) AS
(
SELECT 1,'yash123','yashn','ray','yash@gmail.com' UNION ALL
SELECT 2,'ray234','raah','kumar','raah@yahoo.com' UNION ALL
SELECT 3,'aniket','aniket','pal','aniket@yahoo.com'
)
,cte_IssueAssigned (issue_num,assigned_to_user_id,comment,assign_date) AS
(
SELECT 'abc123',1,'replicating issue',CAST('2017-03-15 00:00:00.000' AS DATE) UNION ALL
SELECT 'abc123',2,'replicating issue',CAST('2017-03-15 00:00:00.000' AS DATE) UNION ALL
SELECT 'abc345',2,'replicating issue',CAST('2017-03-15 00:00:00.000' AS DATE) UNION ALL
SELECT 'abc345',3,'replicating issue',CAST('2017-03-15 00:00:00.000' AS DATE) UNION ALL
SELECT 'abc334',3,'replicating issue',CAST('2017-03-15 00:00:00.000' AS DATE)
)
,
cte_Staging AS (
SELECT ci.issue_num,
issue_desc,
user_id AS assigned_to_user_id,
comment,
issue_date
FROM cte_Issues ci
INNER JOIN cte_IssueAssigned cia
ON ci.issue_num = cia.issue_num
INNER JOIN cte_users cu
ON cu.id = cia.assigned_to_user_id
)
SELECT issue_num,
issue_desc,
STUFF((
SELECT ',' + b.assigned_to_user_id AS [text()]
FROM cte_Staging b
WHERE b.issue_num = a.issue_num
FOR XML PATH('')
), 1, 1, '') AS assigned_to_user_id,
comment,
issue_date
FROM cte_Staging a
【讨论】:
【参考方案3】:试试,应该没问题
select i.issue_num, i.issue_desc, iss.* from Issues i join
(select distinct a.issue_num, a.comment, a.assign_date, STUFF((
Select u.user_id + ',' AS assigner_to_user_id
From Issue_Assigned isu join Users u on isu.assigned_to_user_id = u.id
Where isu.issue_num = a.issue_num
ORDER BY u.id
For XML PATH (''), type).value('.','nvarchar(max)'
) ,1,1, '') as assigned_to_user_id
from Issue_Assigned a ) iss on i.issue_num = iss.issue_num
【讨论】:
我会修复它,一分钟 从 ISSUE_ASSIGNED 中获取 ) 的语法异常 a) 预期为 id 或引用的 id 我已经修复了,只是改变了你的列和表名,我已经改变了我的评论,请看上面 对于匹配的情况,它显示 2 行,并且它正在删除 userId 的第一个字母【参考方案4】:这应该会产生预期的结果。
select T2.issue_num, issue_desc, stuff((select ',' + cast(user_id as varchar(20))
from Users T1
where T1.id=T2.assigned_to_user_id
for xml path('')),1,1,'') userid,T2.comment,T2.assign_date
from IssueAssigned T2 inner join issues T3 on T2.issue_num=T3.issue_num
【讨论】:
这个结果我得到了 2 个单独的行 @Suyash,这就是你需要的,对吧?您在所需的输出中提到了 3 行。以上是关于如何使用 SQL Server 在一行中检索特定 id 的多条记录的主要内容,如果未能解决你的问题,请参考以下文章