2017-02-22 119 views
0

我有以下查詢,讓我彙總的數據,我想支點:添加樞軸彙總

select provider, title, hd_or_sd, SUM(customer_price), count(distinct title) 
from `100` group by provider, title, hd_or_sd with rollup 

enter image description here

我想轉動的sd_or_hd列,從而使我的結果看起來像:

         hd    sd   total 
provider   title    rev. count  rev. count rev. count 
DISTRIBBER           16.99 1  16.99 1 
DISTRIBBER  finding Joe      16.99 1  16.99 1 

我該如何做這個查詢?另外,我注意到Rollup並不完美 - 例如,在Electric娛樂中,它顯示5個標題,但具有hd_or_sd值「HD」,儘管其中幾個標題是SD。爲什麼會發生?

以下是樣本的數據:http://sqlfiddle.com/#!9/a9b5d9/1。這裏是最後的結果應該是什麼(減去陌生號碼的格式) - enter image description here

請注意,我也想做到這一點,而無需使用CASE語句作爲轉動柱可能有許多不同的值。

+0

那麼,你決定和rollup一起去呢?爲了通過'hd_or_sd'彙總,您需要通過'hd_or_sd' – cha

+0

@cha分組哦,我看到了 - 謝謝。 – David542

+0

如果您將這些值放入您的預期結果 – cha

回答

1

您需要使用SUM與CASE操作,這樣的:

select provider, title, 
sum(case when hd_or_sd = 'HD' then 1 else 0 end) as HD, 
sum(case when hd_or_sd = 'SD' then 1 else 0 end) as SD, 
sum(case when hd_or_sd = 'HD' then customer_price else 0 end) as HD_price, 
sum(case when hd_or_sd = 'SD' then customer_price else 0 end) as SD_price, 
sum(customer_price) revenue from `100` 
group by provider, title 
with rollup 

結果:

|    provider |      title | HD | SD | HD_price | SD_price | revenue | 
|------------------------|----------------------------|----|----|----------|----------|---------| 
|    DISTRIBBER |    Finding Joe | 0 | 1 |  0 | 16.99 | 16.99 | 
|    DISTRIBBER |      (null) | 0 | 1 |  0 | 16.99 | 16.99 | 
|   Echo Bridge |    Do Something | 0 | 1 |  0 |  1.99 | 1.99 | 
|   Echo Bridge |     Down in LA | 2 | 2 |  0 |  0 |  0 | 
|   Echo Bridge | The L.A. Complex, Season 1 | 0 | 1 |  0 | 19.99 | 19.99 | 
|   Echo Bridge | The Other Side of the Door | 1 | 2 |  2.99 |  3.98 | 6.97 | 
|   Echo Bridge |    Who You Know | 0 | 2 |  0 |  3.98 | 3.98 | 
|   Echo Bridge |      (null) | 3 | 8 |  2.99 | 29.94 | 32.93 | 
| Electric Entertainment |   Leverage, Season 4 | 0 | 1 |  0 | 31.99 | 31.99 | 
| Electric Entertainment |  The Cross My Heart Job | 1 | 0 |  2.99 |  0 | 2.99 | 
| Electric Entertainment |    The Inside Job | 0 | 1 |  0 |  1.99 | 1.99 | 
| Electric Entertainment |    The Radio Job | 0 | 1 |  0 |  1.99 | 1.99 | 
| Electric Entertainment |  The Scheherazade Job | 1 | 0 |  2.99 |  0 | 2.99 | 
| Electric Entertainment |      (null) | 2 | 3 |  5.98 | 35.97 | 41.95 | 
|    HALLMARK |  The Good Witch's Gift | 0 | 1 |  0 |  3.99 | 3.99 | 
|    HALLMARK |      (null) | 0 | 1 |  0 |  3.99 | 3.99 | 
|   Quebec Inc. |  2 Frogs In the West | 1 | 0 |  5.99 |  0 | 5.99 | 
|   Quebec Inc. |      (null) | 1 | 0 |  5.99 |  0 | 5.99 | 
|     VIRGIL |   One Lucky Elephant | 0 | 1 |  0 |  3.99 | 3.99 | 
|     VIRGIL |      (null) | 0 | 1 |  0 |  3.99 | 3.99 | 
|     (null) |      (null) | 6 | 14 | 14.96 | 90.88 | 105.84 | 

SQL Fiddle

或者,你可以簡單地通過hd_or_sd ROLLUP和有PIVOT如下所示:

select provider, title, hd_or_sd, 
sum(customer_price) revenue from `100` 
group by provider, hd_or_sd, title 
with rollup 
+0

在這裏,我不是案例操作員的忠實粉絲,因爲在這裏可能有很多值。它適用於這種情況,但在大多數其他情況下不起作用。有沒有可能改變這個工作,沒有一堆'CASE'?無論如何,這個工作,所以我已經爲它+1了。 – David542

+0

如果將組更改爲:'group by provider,hd_or_sd,title',則可以使用ROLLUP – cha