私はこのようになりますテーブルを持っている: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つのクエリでこれを行う方法はありますか?あなたは左を使用することができ
あなたの例で 'total_per_pilot_ind'列の' 16'と '26'値はどのように計算されましたか? – krokodilko
私は、テーブル全体を含めるように投稿を編集しました。表とデータはrextesterリンクにもあります。 total_per_pilot_indは、pilot_indフィールド(パイロットまたは非パイロットのいずれか)の各値に対するすべての行のカウントです。 pct_pilot_ind_totalは、(total_count/total_per_pilot_ind)として計算されます。 – psrpsrpsr