SQL Server分组数据
我们的一个日志表中的一个结果如下所示,
Unique__Nm reviewer 1 reviewer 2 reviewer 3
--------------------------------------------------
859 NULL NULL Joel
859 NULL Joseph NULL
859 Antony NULL NULL
163 Robert NULL NULL
163 NULL Joseph NULL
164 NULL Andrew NULL
896 NULL karry NULL
我需要根据唯一数字对数据进行分组,并且提取结果像以下
Unique__Num reviewer 1 reviewer 2 reviewer 3
------------------------------------------------
859 Antony Joseph Joel
163 Robert Joseph NULL
164 NULL Andrew NULL
896 NULL Kerry NULL
人可以提供帮助吗?
One of our log table got result like below
Unique__Nm reviewer 1 reviewer 2 reviewer 3
--------------------------------------------------
859 NULL NULL Joel
859 NULL Joseph NULL
859 Antony NULL NULL
163 Robert NULL NULL
163 NULL Joseph NULL
164 NULL Andrew NULL
896 NULL karry NULL
I need to group data based on Unique number and extract result like below
Unique__Num reviewer 1 reviewer 2 reviewer 3
------------------------------------------------
859 Antony Joseph Joel
163 Robert Joseph NULL
164 NULL Andrew NULL
896 NULL Kerry NULL
Can someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的输入是:
输出中应该是评论者2什么?
如果完全可以使用这种输入,则应指定应该发生的事情,即。我们应该作为Rewiever 2得到什么。
否则这将完成工作:
What if your input is like:
What should be reviewer 2 in the output?
If such input is possible at all, you should specify what should happen, ie. what should we get as rewiever 2.
Otherwise this will do the work:
您的数据
使用提供
String_agg
提供更好的情况,而每个unique_nm存在两个[reviwer],并且它将concat
它们,此外,它掩盖了null值。但是,使用
max
也可能Your data
using provide
String_agg
provide better situation where exist two [reviwer] per Unique_Nm and it willconcat
them, In addition, it mask null values.however using
Max
is also possibledbfiddle
如果所有
审阅者1
,<代码>审阅者2 和审阅者3
列可以具有只有一个不为null > unique__nm ,您可以使用SimpleCocece
由unique_nm
值分组的列,例如If all of
reviewer 1
,reviewer 2
andreviewer 3
columns can have only one not null value perUnique__Nm
, you can use simplecoalesce
per column grouped byUnique_Nm
values, e.g.