【问题标题】:Group by SQL Server (show all elements in selected group by) [duplicate]按 SQL Server 分组(显示所选分组中的所有元素)[重复]
【发布时间】:2017-01-03 02:17:26
【问题描述】:

我对查询有点困惑,我无法在 SQL Server 的查询中显示表。希望我下面的截图清楚地显示了我的问题:

this is my expectation

【问题讨论】:

    标签: mysql sql sql-server group-by


    【解决方案1】:
        ;WITH tb(id,id_ang,bulan)AS(
           SELECT 1,1,1 UNION
           SELECT 2,2,1 UNION
           SELECT 3,1,13 UNION
           SELECT 4,1,2
        )
        SELECT id_ang,STUFF(c.bulans,1,1,'') AS bulans FROM tb 
        OUTER APPLY(SELECT ','+LTRIM(bulan) FROM tb AS tt WHERE tt.id_ang=tb.id_ang ORDER BY ID FOR XML PATH('') ) c(bulans)
        GROUP BY id_ang,c.bulans
    
    id_ang bulans 1 1,13,2 2 1

    【讨论】:

    • 谢谢诺兰。这是完美的工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2023-03-03
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多