我想在没有聚合的情况下转换(旋转)它。数据应该是这样的

enter image description here

我尝试了以下代码;

SELECT *
FROM (
        SELECT
        dm
        ,max(hucre) as mxhucre
        FROM #dene
        group by dm
        ) as pTablom
PIVOT
(
        max(mxhucre)
        FOR dm IN (
        [1000_TEKİRDAĞ TM]
        ,[10000_TEKIRDAG OB1]
        ,[10100_YAĞCI DM]
))AS Pvt

但它得到,

enter image description here

这不是我想要的数据。大多数值(value)观已经消失

如果我不使用 aggrate(max-min.. 等),则代码不起作用。

请帮忙

最佳答案

使用条件聚合

SELECT
    max(case when dm='1000_TEKİRDAĞ TM' then hucre end) as '1000_TEKİRDAĞ TM'
    max(case when dm='10000_TEKIRDAG OB1'then hucre end) as '10000_TEKIRDAG OB1',
    max(case when dm='10100_YAĞCI DM  H04_T10' then hucre end) as '10100_YAĞCI DM  H04_T10'
FROM #dene
group by dm

关于sql - MSSQL : Transpose-Pivot table without aggregate function,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56001868/

10-11 20:21