sql - 按案例字段分组

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

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

无论如何,我目前的疑问如下。这会产生错误,即分组依据必须包含一列不是外部引用,我理解这一点,但不知道如何解决。

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

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'

最佳答案

您无法使用当前选择中定义的列进行分组。

将此选择作为子查询,然后您可以使用 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 所说,您还可以按部分将大小写包含在分组中,这样您就不需要使用子查询。

关于sql - 按案例字段分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518061/

相关文章:

sql - case 语句中的 Hive 摘要函数

sql - 波斯语全文索引停止列表

mysql - 有没有更有效的方法来编写这个查询?

使用 Max() 投影对键字段和按外主键分组的 Hibernate 条件查询

MySQL 分组依据与时间差异

sql - Case 语句多个 Then 值

mysql - 案例陈述的问题(按类(class)数量组织部门)

sql - 如何从 MS SQL 2014 中的 SQL 查询生成 JSON

sql - 如何过滤 where 子句中的复合键?

MySQL 按情况排序日期 ASC/DESC