2017-08-07 5 views
0

私はこのようになりますテーブルを持っている:GROUP BYの複数のフィールドの1つである場合、1つのフィールドのデータをどのように集計しますか?

| loc_id | pilot_ind | last_form_step_completed | 
|--------|-----------|--------------------------| 
| 9988 | non-pilot | 1      | 
| 9988 | non-pilot | 1      | 
| 9988 | non-pilot | 2      | 
| 9988 | non-pilot | 2      | 
| 9988 | non-pilot | 2      | 
| 9988 | non-pilot | 3      | 
| 9988 | non-pilot | 3      | 
| 9988 | non-pilot | 4      | 
| 9988 | non-pilot | 4      | 
| 1122 | non-pilot | 1      | 
| 1122 | non-pilot | 2      | 
| 1122 | non-pilot | 2      | 
| 1122 | non-pilot | 2      | 
| 1122 | non-pilot | 3      | 
| 1122 | non-pilot | 4      | 
| 1122 | non-pilot | 5      | 
| 5544 | pilot  | 1      | 
| 5544 | pilot  | 1      | 
| 5544 | pilot  | 2      | 
| 5544 | pilot  | 2      | 
| 5544 | pilot  | 2      | 
| 5544 | pilot  | 3      | 
| 5544 | pilot  | 3      | 
| 5544 | pilot  | 4      | 
| 5544 | pilot  | 4      | 
| 5544 | pilot  | 5      | 
| 5544 | pilot  | 5      | 
| 5544 | pilot  | 5      | 
| 5544 | pilot  | 5      | 
| 5544 | pilot  | 5      | 
| 3344 | pilot  | 1      | 
| 3344 | pilot  | 2      | 
| 3344 | pilot  | 2      | 
| 3344 | pilot  | 3      | 
| 3344 | pilot  | 3      | 
| 3344 | pilot  | 3      | 
| 3344 | pilot  | 4      | 
| 3344 | pilot  | 4      | 
| 3344 | pilot  | 4      | 
| 3344 | pilot  | 5      | 
| 3344 | pilot  | 5      | 
| 3344 | pilot  | 5      | 

私はこのようにそれを要約する必要があります。

| pilot_ind | last_step_compl | total_count | total_per_pilot_ind | pct_pilot_ind_total | 
|-----------|-----------------|-------------|---------------------|---------------------| 
| non-pilot | 1    | 3   | 16     | 18.8%    | 
| non-pilot | 2    | 6   | 16     | 37.5%    | 
| non-pilot | 3    | 3   | 16     | 18.8%    | 
| non-pilot | 4    | 3   | 16     | 18.8%    | 
| non-pilot | 5    | 1   | 16     | 6.3%    | 
| pilot  | 1    | 3   | 26     | 11.5%    | 
| pilot  | 2    | 5   | 26     | 19.2%    | 
| pilot  | 3    | 5   | 26     | 19.2%    | 
| pilot  | 4    | 5   | 26     | 19.2%    | 
| pilot  | 5    | 8   | 26     | 30.8%    | 

私はtotal_per_pilot_indフィールドにpilot_indためにグループ化合計を得るトラブルを抱えています - OVER(PARTITION BY)を使用してみましたが、GROUP BY句に2つのフィールドがあるため、結果が正しくありません。 ここでは例のデータと現在の試行へのリンクは次のとおりです。http://rextester.com/PCE62786

select 
    r.pilot_ind 
    ,r.last_form_step_completed 
    ,count(*) total_count 
    ,count(*) over() 
from 
    results r 
group by 
    r.pilot_ind 
    ,r.last_form_step_completed 
order by 1,2 

EDIT:1つのクエリでこれを行う方法はありますか?あなたは左を使用することができ

+0

あなたの例で 'total_per_pilot_ind'列の' 16'と '26'値はどのように計算されましたか? – krokodilko

+0

私は、テーブル全体を含めるように投稿を編集しました。表とデータはrextesterリンクにもあります。 total_per_pilot_indは、pilot_indフィールド(パイロットまたは非パイロットのいずれか)の各値に対するすべての行のカウントです。 pct_pilot_ind_totalは、(total_count/total_per_pilot_ind)として計算されます。 – psrpsrpsr

答えて

2

サブクエリを使用します:

SELECT *, 
     sum(total_count) over(partition by pilot_ind) As total_per_pilot_ind, 
     round(100.0 * total_count/sum(total_count) over(partition by pilot_ind) ,1) 
     as pct_pilot_ind_total 
FROM (
select 
    r.pilot_ind 
    ,r.last_form_step_completed 
    ,count(*) total_count 
from 
    results r 
group by 
    r.pilot_ind 
    ,r.last_form_step_completed 
) x 
order by 1,2 

デモ:http://sqlfiddle.com/#!17/0f411/5

0

select 
     t.pilot_ind 
     , t.last_form_step_completed 
     , sum(a.last_form_step_completed) total_count 
    from ( 
    select distinct pilot_ind, last_form_step_completed 
    from my_table) t 
    left join my_table a on t.pilot_ind = a.pilot_ind 
     and t.last_form_step_completed = a.last_form_step_completed 
    group by t.pilot_ind 
     , t.last_form_step_completed 

に参加する(ので、私はそれらを省略..あなたはあなたのサンプルに2つの最後の列を取得する方法は明らかではない)

0
select a.pilot_id, a.last_step, a.total_count, b.total_per_pilot_ind, 
(a.total_count/ b.total_per_pilot_ind)*100 as Per_tage 
from 
(
select pilot_ind, last_form_step_completed as last_step, count(*) as total_count 
from table1 
group by pilot_ind, last_form_step_completed 
) a, 
(
select pilot_ind, sum(last_form_step_completed) as total_per_pilot_ind 
from table1 
group by pilot_id 
) b 
where a.pilot_ind=b.pilot_ind 
関連する問題