TSQL 中的合并和透视

发布于 2024-07-16 18:25:41 字数 385 浏览 5 评论 0原文

我无法弄清楚如何合并或旋转 SQL 记录集,如下所示:

ID      VALUE       GROUP
3       John        18
4       Smith       18
5       Microsoft   18
3       Randy       21
4       Davis       21
5       IBM     21
etc

并且我想要这样的格式,

NEWVALUE                GROUP
Smith, John (Microsft)      18
Davis, Randy (IBM)          21  

感谢您的任何建议和帮助!

I am having trouble figuring out how to coalesce or pivot on a SQL recordset that looks like this:

ID      VALUE       GROUP
3       John        18
4       Smith       18
5       Microsoft   18
3       Randy       21
4       Davis       21
5       IBM     21
etc

and I want formatted like this

NEWVALUE                GROUP
Smith, John (Microsft)      18
Davis, Randy (IBM)          21  

thanks for any suggestions and help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁我热情 2024-07-23 18:25:41

这就是我所做的,我希望它适合你

DECLARE @t table (id int, value VARCHAR(20), grupo int)
INSERT @T VALUES (3, 'John', 18)
INSERT @T VALUES (4, 'Smith', 18)
INSERT @T VALUES (5, 'Microsoft', 18)
INSERT @T VALUES (3, 'Randy', 21)
INSERT @T VALUES (4, 'Davis', 21)
INSERT @T VALUES (5, 'IBM', 21)


SELECT grupo, (SELECT value FROM @t t2 WHERE t2.grupo = t.grupo AND id = 4) + ', ' + 
(SELECT value FROM @t t2 WHERE t2.grupo = t.grupo AND id = 3) + ' (' +
(SELECT value FROM @t t2 WHERE t2.grupo = t.grupo AND id = 5) + ')'
FROM @t t 
GROUP BY grupo

This is what i done, i hope it fits for you

DECLARE @t table (id int, value VARCHAR(20), grupo int)
INSERT @T VALUES (3, 'John', 18)
INSERT @T VALUES (4, 'Smith', 18)
INSERT @T VALUES (5, 'Microsoft', 18)
INSERT @T VALUES (3, 'Randy', 21)
INSERT @T VALUES (4, 'Davis', 21)
INSERT @T VALUES (5, 'IBM', 21)


SELECT grupo, (SELECT value FROM @t t2 WHERE t2.grupo = t.grupo AND id = 4) + ', ' + 
(SELECT value FROM @t t2 WHERE t2.grupo = t.grupo AND id = 3) + ' (' +
(SELECT value FROM @t t2 WHERE t2.grupo = t.grupo AND id = 5) + ')'
FROM @t t 
GROUP BY grupo
人│生佛魔见 2024-07-23 18:25:41
SELECT  LEFT(gvalue, LEN(gvalue) - 1) AS newvalue, _group
FROM    (
        SELECT  DISTINCT _group
        FROM    mytable
        ) qo
CROSS APPLY
        (
        SELECT  value + ', '
        FROM    mytable qi
        WHERE   qi._group = qo._group
        FOR XML PATH ('')
        ) gr(qvalue)

如果每个 _group 始终有一组三个硬编码的 ID,则可以使用:

SELECT m3._group, m3.value + ', ' + m4.value + '(' + m5.value + ')' AS newvalue
FROM   mytable m3
LEFT JOIN
       mytable m4
ON     m4._group = m3.group
LEFT JOIN
       mytable m5
ON     m5._group = m3.group
WHERE  m3.id = 3
       AND m4.id = 4
       AND m5.id = 5
SELECT  LEFT(gvalue, LEN(gvalue) - 1) AS newvalue, _group
FROM    (
        SELECT  DISTINCT _group
        FROM    mytable
        ) qo
CROSS APPLY
        (
        SELECT  value + ', '
        FROM    mytable qi
        WHERE   qi._group = qo._group
        FOR XML PATH ('')
        ) gr(qvalue)

If you always have a set of three hardcoded ID's for each _group, you can just use:

SELECT m3._group, m3.value + ', ' + m4.value + '(' + m5.value + ')' AS newvalue
FROM   mytable m3
LEFT JOIN
       mytable m4
ON     m4._group = m3.group
LEFT JOIN
       mytable m5
ON     m5._group = m3.group
WHERE  m3.id = 3
       AND m4.id = 4
       AND m5.id = 5
审判长 2024-07-23 18:25:41

您需要的不是旋转查询,而是带有分组依据和聚合字符串连接函数的简单选择。 但我不记得 tsql 中的确切函数。

更新:tsql 中没有聚合连接函数,但从 sql2005 开始,您可以编写自己的扩展来实现此类函数。 谷歌搜索上有很多示例:tsql 2005 concatenationaggregate example。

What you need is not pivoted query but a simple select with group by and an aggregate string concatenation function. But i don't remember the exact function in tsql.

Update: there is no aggregate concatenation function in tsql but since sql2005 you can write your own extension to implement such function. There is plenty of examples on google search for: tsql 2005 concatenation aggregate example.

油焖大侠 2024-07-23 18:25:41

这有点做作,但我认为它对于小数据集应该工作得相当好。 如果您有大量数据,则需要创建游标和循环。

select max(case when ID = 4 then VALUE else null end) + ', ' + 
    max(case when ID = 4 then VALUE else null end) + '( ' +
    max(case when ID = 5 then VALUE else null end) + ') as NEWVALUE,
    [GROUP]
group by [GROUP]

This is a little hokey, but I think it should work reasonably well for a small data set. If you've got a lot of data you need to create a cursor and a loop.

select max(case when ID = 4 then VALUE else null end) + ', ' + 
    max(case when ID = 4 then VALUE else null end) + '( ' +
    max(case when ID = 5 then VALUE else null end) + ') as NEWVALUE,
    [GROUP]
group by [GROUP]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文