[英]How to get Email ID(one value) for each ID for 1st table and then display Email Name from 2nd table as comma Separated in SQL Server 2012?
What i have tried Results wanted like this From One table , for each ID there can be multiple email id's based on some condition Ex 我尝试过的 结果想要这样的结果从一个表开始,对于每个ID,根据某些条件,可以有多个电子邮件ID,例如
ID EmailID's
1 Mike.Foster@Mail.com
1 lilly.Foster@Mail.com
2 Michel.Josh@Mail.com
2 Nash.Ted@Mail.com
I have to get email Name from these Ids from another table, something like this 我必须从另一个表中的这些ID中获取电子邮件名称,就像这样
Output i Need 我需要的输出
Email_Name
Foster.Mike,Foster.Lilly
Josh.Michel,Ted.Nash
This is what i tried. 这就是我尝试过的。
SELECT User_Email =
STUFF((SELECT DISTINCT ', ' + User_Email
FROM table1 b
WHERE b.Component_ID= a.Component_ID
and [Role] ='Team Lead' and Functional_Group ='Product'
FOR XML PATH('')), 1, 2, '')
FROM [WFS].table1 a
GROUP BY table1
Now another table i want Email Names 现在我要另一个表电子邮件名称
Select EmailNamefrom Table2 where EmailIDs IN ( 'code for Email')
Table1 schema 表1模式
ID Component_ID EmailIDs Role Functional_Group
1 1 Mike.Foster@Mail.com Team Lead Product
2 1 lilly.Foster@Mail.com Team Lead Product
3 2 Michel.Josh@Mail.com Team Lead Product
4 2 Nash.Ted@Mail.com Team Lead Product
Table 2 schema 表2模式
ID EmailIDs EmailName
1 Mike.Foster@Mail.com Foster.Mike
2 lilly.Foster@Mail.com Foster.Lilly
Any suggestions are welcome.Thanks in advance 欢迎任何建议。谢谢
You were actually close but your SQL doesn't match your schema. 您实际上已经关闭,但是您的SQL与您的架构不匹配。 This one works as you want:
这是您想要的作品:
SELECT Email_Name =
STUFF((SELECT DISTINCT ', ' + EmailIDs
FROM table1 b
WHERE b.Component_ID= a.Component_ID
and [Role] ='Team Lead' and Functional_Group ='Product'
FOR XML PATH('')), 1, 2, '')
FROM table1 a
GROUP BY a.Component_ID;
EDIT: I didn't understand what you are asking exactly. 编辑:我不明白你到底在问什么。 Might this be what you meant?
可能这就是您的意思吗?
SELECT STUFF((SELECT ', ' + EmailName
FROM Table2 where EmailIDs IN ( SELECT EmailIDs
FROM table1
WHERE [Role] ='Team Lead' and Functional_Group ='Product')
FOR XML PATH('')), 1, 2, '')
Or you meant this: 或者你的意思是这样的:
SELECT DISTINCT Component_ID, emailNames
FROM table1
CROSS APPLY (SELECT STUFF((SELECT ', '+t2.EmailName
FROM table2 t2
INNER JOIN TABLE1 t1 ON t1.EmailIDs = t2.EmailIDs
WHERE t1.Component_ID = Table1.Component_ID
FOR XML PATH('')), 1, 2, '')
) t(EmailNames)
WHERE [Role] ='Team Lead' and Functional_Group ='Product'
Here is SQLFiddle Demo 这是SQLFiddle演示
Disregard this answer as I found out that GROUP_CONCAT()
is a MySQL specific function , which means it won't work in SQL Server, however, I'll let it stay for future references. 当我发现
GROUP_CONCAT()
是MySQL特定的函数时 ,请忽略此答案 ,这意味着它在SQL Server中将不起作用,但是,我将其留作以后参考。
SELECT
GROUP_CONCAT(EmailName SEPARATOR ', ') as name
FROM
table1
INNER JOIN table2 ON table1.EmailIDs=table2.EmailIDs
WHERE
table1.EmailIDs=table2.EmailIDs
GROUP BY
table1.Component_ID
Output: 输出:
Foster.Mike, Foster.Lilly 福斯特·迈克,福斯特·莉莉
Ted.Nash, Josh.Michel 泰德·纳什,乔什·米歇尔
Working SQL fiddle 工作的SQL小提琴
Docs: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat 文件: https : //dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
You need CTE
with STUFF()
: 您需要使用
STUFF()
CTE
:
WITH t AS (
SELECT t1.*, t2.emailname
FROM table1 t1 INNER JOIN
table2 t2
ON t2.emailids = t1.emailids
)
SELECT id, STUFF ( (SELECT DISTINCT ','+t1.emailname
FROM t t1
WHERE t1.id = t.id
FOR XML PATH ('')
), 1, 1, ''
) AS Email_Name
FROM t
GROUP BY id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.