2012-05-09 26 views
1

だから、私はかなりSQLに慣れていて、これまでほとんど完全に独学しています。私は現在、スタッフの階級についていくつかの分析を実行しようとしています。ここでは、約15以上のランクを4ランクのカテゴリに「再分類」しようとしています。これらのカテゴリ別にグループ化したいのですが、いくつかの問題が発生しています(ダミーのSQLにはスチームがなくなったようです。答えが見つかりません...)。大文字小文字のグループ化

とにかく、私が今質問している質問は次のとおりです。これは、グループ参照に外部参照ではない列が1つ含まれていなければならないというエラーを示しています。これはわかっていますが、回避策を知る方法はわかりません。

EDITは:

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' 

答えて

1

を をいただければ幸いですすべてのヘルプ:私は次の私に与えた結果を得ることを目指していますに定義されている列で現在を選択します。

サブクエリを選択し、RankDesc列を使用してグループを作成することができます。


最終クエリは、このようなものでなければなりません:@PhilipKelleyは述べたように、あなたはまた、一部でグループのケースを含むことができ、それをあなたはないでしょう

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 

サブクエリを使用する必要があります。

+0

aF - 多くの感謝 - それはそれをひどくしたようです。私は直感的に何が起こっているのか理解していると思います(私はなぜ私のものが明確に機能しないのでしょう)が、私はそれを得るために良い選択をします。 – mattknight1986

+0

case文全体をgroup by節に含めることもできますが、サイズと複雑さを考えると、これははるかに明確です。フィリップ氏のように –

+0

@ mattknight1986と書かれているように、あなたはまた、部分的にグループにケースを含めることができ、サブクエリを使う必要はありません。 –

0

サブクエリを作成し、その結果のフィールドでグループ化するか、(十分に再利用可能な場合はそれよりも優れていると思われます)フィールドを使用してビューとグループを作成します。

0

select with caseはサブクエリでなければなりません。これにより、テーブルが表示され、実行時に簡単にグループ化できます。

関連する問題