2

所以我对 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'
4

3 回答 3

2

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

使它选择一个子查询,然后您可以使用列 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 所说,您还可以将案例按部分包含在分组中,这样您就不需要使用子查询。

于 2012-05-09T14:23:26.520 回答
0

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

于 2012-05-09T14:28:23.410 回答
0

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

于 2012-05-09T14:33:38.670 回答