【问题标题】:Grouping by case field按案例字段分组
【发布时间】:2012-05-09 14:21:59
【问题描述】:

所以我对 SQL 还是很陌生,到目前为止几乎完全是自学的。我目前正在尝试对员工的等级进行一些分析,我正在尝试将大约 15 个以上的等级“重新分类”为 4 个等级类别。然后我想按这些类别进行分组,但遇到了一些麻烦(SQL for Dummies 似乎已经失去动力,或者我找不到答案......)。

无论如何,我目前的查询如下。这给出了 group by 必须包含一个不是外部引用的列的错误,我理解,但不知道如何解决。

编辑:我的目标是获得以下结果:

RANK                      Hours etc
Equity Partner            12
Fixed Share Partner       20
Associate                 50
Trainee                   25
Other                     15 

任何帮助将不胜感激 马特

declare @startperiod as integer
declare @endperiod as integer
declare @offc as integer
declare @dept as varchar(3)

select @startperiod = '201101'
select @endperiod = '201112'
select @offc = '55'

select 
    case k.rank_code 
                when '10'   then 'Equity Partner'
                when '110'  then 'Fixed Share Partner'
                when '130'  then 'Associate'
                when '131'  then 'Associate' 
                When '132'  then 'Associate'
                when '133'  then 'Associate'
                when '134'  then 'Associate'
                when '135'  then 'Associate'
                when '136'  then 'Associate'
                when '137'  then 'Associate'
                when '141'  then 'Associate'
                when '142'  then 'Associate'
                when '341'  then 'Trainee'
                when '342'  then 'Trainee'
                else 'Other'
end as 'Rank Desc',
sum(b.base_hrs) as 'Base Hrs',sum(b.tobill_hrs) as 'ToBill Hrs',sum(b.billed_hrs) as 'Billed Hrs', 
sum(b.base_amt) as 'Base Amt',sum(b.tobill_amt) as 'ToBill Amt',sum(b.billed_amt) as 'Billed Amt'

from blh_billed_fees b, tbl_rank k, tbm_persnl p
where b.tk_empl_uno = p.empl_uno
and p.rank_code = k.rank_code

group by 'Rank Desc'

【问题讨论】:

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


    【解决方案1】:

    您不能使用 current 选择中定义的列进行分组。

    选择一个子查询,然后您可以使用 RankDesc 列来进行分组。


    最终的查询应该是这样的:

    select 
    aux.RankDesc as 'Rank Desc',
    sum(aux.base_hrs) as 'Base Hrs',
    sum(aux.tobill_hrs) as 'ToBill Hrs',
    sum(aux.billed_hrs) as 'Billed Hrs', 
    sum(aux.base_amt) as 'Base Amt',
    sum(aux.tobill_amt) as 'ToBill Amt',
    sum(aux.billed_amt) as 'Billed Amt'
    
    from
        (select 
            case k.rank_code 
                        when '10'   then 'Equity Partner'
                        when '110'  then 'Fixed Share Partner'
                        when '130'  then 'Associate'
                        when '131'  then 'Associate' 
                        When '132'  then 'Associate'
                        when '133'  then 'Associate'
                        when '134'  then 'Associate'
                        when '135'  then 'Associate'
                        when '136'  then 'Associate'
                        when '137'  then 'Associate'
                        when '141'  then 'Associate'
                        when '142'  then 'Associate'
                        when '341'  then 'Trainee'
                        when '342'  then 'Trainee'
                        else 'Other'
        end as 'RankDesc',
        b.base_hrs,
        b.tobill_hrs,
        b.billed_hrs, 
        b.base_amt,
        b.tobill_amt,
        b.billed_amt
    
        from blh_billed_fees b, tbl_rank k, tbm_persnl p
        where b.tk_empl_uno = p.empl_uno
        and p.rank_code = k.rank_code) aux
    group by aux.RankDesc
    

    就像@PhilipKelley 所说,您还可以将案例按部分包含在分组中,这样您就不需要使用子查询了。

    【讨论】:

    • aF - 非常感谢 - 似乎已经破解了它。我想我直觉地理解发生了什么(我明白为什么我的不能确定地工作),但我会好好挑选以确保我明白。
    • 您也可以在 group by 子句中包含整个 case 语句,但考虑到大小和复杂性,这会更清晰。
    • @mattknight1986 就像 Philip 所说的那样,您还可以将案例按部分包含在组中,这样您就不需要使用子查询了。
    • @mattknight1986 如果它工作正常,你应该接受答案:meta.stackexchange.com/questions/5234/…
    【解决方案2】:

    要么将其设为子查询,然后按其结果字段进行分组,要么(如果它足够可重用,可能会更好)创建一个视图并按其字段分组。

    【讨论】:

      【解决方案3】:

      带有 case 的 select 必须是子查询,这使得它在父执行时显示为表,然后可以轻松地对其应用分组。

      【讨论】:

        猜你喜欢
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-13
        • 2021-01-27
        相关资源
        最近更新 更多