2017-10-19 91 views
1

我有一個表movesPostgreSQL Group按類別,計數和計算百分比值

uuid | tag | ... 
-----| ----|---- 
abc | 520 | ... 
def | 510 | ... 
ghi | 500 | ... 
jkl | 310 | ... 
mno | 200 | ... 

tag代表移動的類型。我們正在談論沙灘排球的動作。第一個號碼,例如520中的5個是該類別。在這種情況下「服務」。在總我有六大類:

  1. 攻擊(1類,如100110120
  2. 塊(2類)
  3. 挖(3類)
  4. 接待(第4類)
  5. 服務(5類)
  6. 設定(6類)

最後的數字,即來自520的20,是結果。在這種情況下「贏」。每個類別有3個可能的結果:

  1. 錯誤(00
  2. 零(10
  3. 運(20

下面是從上表標籤上方

  1. 520 - 服務贏(這是一個王牌)
  2. 510 - 服務零
  3. 500 - 服務錯誤
  4. 310 - 挖零
  5. 200 - 塊錯誤

這裏是想什麼,我得到:給我的錯誤計數,零,以絕對值和相對值贏得每個類別。

我嘗試以下

select *, 
    (attack_error::float/attacks::float * 100) as attack_error_percentage, 
    (attack_zero::float/attacks::float * 100) as attack_zero_percentage, 
    (attack_win::float/attacks::float * 100) as attack_win_percentage 
    from (
     select 
      count(*) filter (where tag = 100) as attack_error, 
      count(*) filter (where tag = 110) as attack_zero, 
      count(*) filter (where tag = 120) as attack_win, 
      count(*) filter (where tag = 100 or tag = 110 or tag = 120) as attacks 
     from moves 
     where match_uuid = 'd7eea231-a63d-4d73-b48f-5ca8541ec9cf' and set = 1 
    ) 
as attack_stats 

,並得到了這樣的事情

att_error | att_zero | att_win | total | att_error_% | att_zero_% | att_win_% 
----------|----------|---------|-------|-------------|------------|---------- 
1   | 3  | 13  | 17 | 5.88  | 17.65  | 76.47 

但是它不覺得權利,我不得不一次又一次地重複查詢所有不同類別與所有的結果。

我真的很想得到的是這樣的。

category | error | zero | win | total | error_% | zero_% | win_% 
---------|-------|------|-----|-------|---------|--------|------ 
1  | 2  | 4 | 6 | 12 | 0.16 | 0.33 | 0.5 
2  | 3  | 8 | 13 | 24 | 0.125 | 0.33 | 0.54 
3  | ... | ... | ... | ... | ...  | ... | ... 
4  | ... | ... | ... | ... | ...  | ... | ... 
5  | ... | ... | ... | ... | ...  | ... | ... 
6  | ... | ... | ... | ... | ...  | ... | ... 

任何想法?

回答

1

考慮與CASE聲明有條件創建類別列,包括其作爲派生表彙總查詢

select *, 
    (error::float/total::float * 100) as error_percentage, 
    (zero::float/total::float * 100) as zero_percentage, 
    (win::float/total::float * 100) as win_percentage 
    from (
     select 
      case substring(tag::text, 1, 1) 
       when '1' then 'Attack' 
       when '2' then 'Block' 
       when '3' then 'Dig' 
       when '4' then 'Reception' 
       when '5' then 'Service' 
       when '6' then 'Setting' 
      end as category, 
      count(*) filter (where tag - round(tag/100, 0)*100 = 0) as error, 
      count(*) filter (where tag - round(tag/100, 0)*100 = 10) as zero, 
      count(*) filter (where tag - round(tag/100, 0)*100 = 20) as win, 
      count(*) filter (where tag - round(tag/100, 0)*100 <= 20) as total 
     from moves 
     where match_uuid = 'd7eea231-a63d-4d73-b48f-5ca8541ec9cf' and set = 1 
     group by 
      case substring(tag::text, 1, 1) 
       when '1' then 'Attack' 
       when '2' then 'Block' 
       when '3' then 'Dig' 
       when '4' then 'Reception' 
       when '5' then 'Service' 
       when '6' then 'Setting' 
      end 
    ) 
as attack_stats 
+0

它種工作的GROUP BY。我得到了預期的表格佈局,但只有Attack行有結果。所有其他行都是空的。我想這是因爲我們使用硬編碼的攻擊值('100','110','120')。任何想法如何填充其他行? – zemirco

+0

我沒有注意到,但您的條件聚合只針對* attack *值進行過濾:'count(*)filter(where tag = 100)'。我調整了以適應其他水平。我假設數值是10增量,0/10/20。 – Parfait

+0

謝謝!按預期工作。最後一個問題。即使缺少類別,是否可以始終獲得6行?例如,比賽剛剛開始,並且沒有任何塊(類別2)。目前我只得到5排。我能以某種方式總是得到所有具有0值的行以查找缺失的類別嗎? – zemirco