从另一个表中按计算的Case列分组

时间:2015-10-26 11:28:38

标签: sql-server-2005 group-by case

我有以下代码正常工作,但数据不是我想要显示的格式。该代码仅在我按Plant分组时才有效,但这显然会创建多个PlantGrps。是否可以按语句分组?

select 
    Case 
    When Plant IN(
                    Select Client From 
                                            CO_001_Plants_090_Final
                                                                    where CustGrp = 'HovisMills'
                    ) Then 'HovisMills' 
        When Plant IN(
                    Select Client From 
                                            CO_001_Plants_090_Final
                                                                    where CustGrp = 'HovisBakeries'
                    ) Then 'HovisBakeries'                  

                            When Plant IN(
                    Select Client From 
                                            CO_001_Plants_090_Final
                                                                    where CustGrp = 'HovisGroup'
                    ) Then 'HovisGroup'     

                    end as PlantGrp,
    --Type
    sum(Line_Val),Count(Material)

    FROM            dbo.vw_DA_003_ChargingRecord as P

    where   

            Plant IN(select Client 
    FROM            dbo.CO_001_Plants_090_Final
    where Upper(CustGrp) = 'HOVIS')
    and Charging_Period = '201509'
    and Type <> 'Carriage'
    group by 


    Plant, Type

0 个答案:

没有答案