Sql 透视查询转换为SSR的分组依据
如何将以下pivot查询更改为group bySql 透视查询转换为SSR的分组依据,sql,sql-server,reporting-services,Sql,Sql Server,Reporting Services,如何将以下pivot查询更改为group by SELECT comb_id , [orgunit] , [time] , [gender] , [jobcategory] , [ethnicity] , [gradegroup] , [regiongeo] , [emplclass] , operatorid , seq_id , sequance_name , listid
SELECT
comb_id
, [orgunit]
, [time]
, [gender]
, [jobcategory]
, [ethnicity]
, [gradegroup]
, [regiongeo]
, [emplclass]
, operatorid
, seq_id
, sequance_name
, listid
FROM
(SELECT
C.comb_id
, dim_name
, V.operatorid
,V.seq_id
, V.listid
, dim_value
, S.sequance_name
FROM
gmt_combinationsflat C
JOIN
gmt_valuesflat V
ON
C.comb_id = V.comb_id
JOIN
gmt_rangeseq S
ON
V.seq_id = S.seq_id
JOIN
gmt_dimensions D
ON
C.dim_id = D.dim_id
WHERE
C.kpi_id = 9
AND
C.last = 1) AS P
PIVOT
(Max(dim_value) FOR dim_name IN ( [ORGUNIT ]
, [Time ]
, [Gender ]
, [JobCategory ]
, [Ethnicity ]
, [GradeGroup ]
, [RegionGeo ]
, [EmplClass ]
)
) AS pvtt
我试着换到下面的。但我不确定应该为groupby设置最大值
select C.comb_id, [ORGUNIT ], [Time ], [Gender ], [JobCategory ], [Ethnicity ], [GradeGroup ], [RegionGeo ], [EmplClass ] , operatorid, seq_id, sequance_name, listid
FROM gmt_combinationsflat C
JOIN gmt_valuesflat V ON C.comb_id = V.comb_id
JOIN gmt_rangeseq S ON V.seq_id = S.seq_id
JOIN gmt_dimensions D ON C.dim_id = D.dim_id
WHERE C.kpi_id = 9 AND C.last = 1
group by comb_id,[ORGUNIT ], [Time ], [Gender ], [JobCategory ], [Ethnicity ], [GradeGroup ], [RegionGeo ], [EmplClass ]
相当于:
SELECT C.comb_id,
V.operatorid,
V.seq_id,
V.listid,
S.sequance_name,
MAX(CASE WHEN dim_name = 'ORGUNIT' THEN dim_value END) AS [orgunit],
MAX(CASE WHEN dim_name = 'Time' THEN dim_value END) AS [time]
....
FROM gmt_combinationsflat C
JOIN gmt_valuesflat V ON C.comb_id = V.comb_id
JOIN gmt_rangeseq S ON V.seq_id = S.seq_id
JOIN gmt_dimensions D ON C.dim_id = D.dim_id
WHERE C.kpi_id = 9 AND C.last = 1
GROUP BY C.comb_id,
V.operatorid,
V.seq_id,
V.listid,
S.sequance_name
如果你遇到任何问题,为什么不试着去解决你的问题呢。现在对我来说,在MAX case中使用的列名更有意义了。这很有帮助,因为我对前面使用的[1]列感到困惑。