sqlserver列转行(多次分组)

select     CASE r.rule_category
        WHEN '1' THEN
        '温度'
        WHEN '2' THEN '湿度'
        WHEN '3' THEN '气压'
        ELSE '未知' END+r.name as name,r.ruleIds
        from (
select rule_category,level,name,stuff((select ','+convert(varchar,rule_id) 
        from alarm_rule where rule_category = e.rule_category and level=e.level
         for xml path('')),1,1,'') as 
        ruleids from alarm_rule e
group by rule_category,level,name
) r

 

posted @ 2019-07-30 11:08  jack_zc  阅读(856)  评论(0编辑  收藏  举报