简体   繁体   English

SQL查询-除透视之外的更多选项和建议

[英]SQL Query - More options and suggestions apart from pivoting

New to SQL please dont mind if this is a silly question.. My table looks like this SQL的新手,请不要介意这是否是一个愚蠢的问题。

在此处输入图片说明

I want to send only one email to manager telling him that these employees in your group failed to fill timesheet. 我只想向经理发送一封电子邮件,告诉他您组中的这些员工未能填写时间表。 currently i have pivoted the above table that looks like this and sending emails by concatinating firstemp+secondemp+thirdemp+------ can this be done in any more easiest way..? 目前,我已经绕过了上面的表格,并通过隐藏firstemp + secondemp + thirdemp + ------发送电子邮件,这可以通过任何更简单的方法来完成吗? 在此处输入图片说明

You can use CONCAT() function to retrieve a single row data in one column 您可以使用CONCAT()函数在一行中检索单行数据

SELECT M_EMAIL,
       CONCAT(FIRSTEMP, SECONDEMP, THIRDEMP, FOURTHEMP, FIFTHEMP...)
FROM 'your_table';

CONCAT() replaces NULL values with an empty string. CONCAT()用空字符串替换NULL值。

Please don't pivot, as the concat is really ugly to maintain (and will break if a more capable manager pops up with more subordinates than your concat columns). 请不要旋转,因为concat维护起来确实很丑陋(如果弹出一个比您的concat列更多的能力更强的经理并且下属更多,那么该管理器就会崩溃)。

The syntax depends on what SQL server you use. 语法取决于您使用的SQL Server。 For example, in MSSQL you could do this: 例如,在MSSQL中,您可以这样做:

select manager, m_email, STRING_AGG(employee, ', ') as subordinates
from Employee
group by manager, m_email

This result has only 1 row per manager and fixed number of columns regardless how many subordinates the manager has: 无论管理器有多少个下属,此结果每个管理器只有1行,列数是固定的:

manager | m_email   | subordinates
----------------------------------
A       | A@A.COM   | b, D
D       | D@D.COM   | e, h
I       | I@I.COM   | j

Play with the example here: http://sqlfiddle.com/#!18/73bb5/5 在此处播放示例: http : //sqlfiddle.com/#!18/73bb5/5

Another option is just query relevant data to application layer and do the grouping there. 另一种选择是将相关数据查询到应用程序层并在那里进行分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM