向汇总添加一个枢轴
Adding a pivot to a rollup
我有以下查询,它提供了我想要转换的聚合数据:
select provider, title, hd_or_sd, SUM(customer_price), count(distinct title)
from `100` group by provider, title, hd_or_sd with rollup
我想旋转 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 entertainment,它显示了 5 个标题,但具有 hd_or_sd 值 "HD",即使其中一些标题是 SD。为什么会出现这种情况?
这是数据示例:http://sqlfiddle.com/#!9/a9b5d9/1。这是最终结果应该是什么(减去奇怪的数字格式)——
请注意,我还想在不使用 CASE
语句的情况下完成此操作,因为旋转列可能具有许多不同的值。
您需要将 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 |
或者,您可以通过 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
我有以下查询,它提供了我想要转换的聚合数据:
select provider, title, hd_or_sd, SUM(customer_price), count(distinct title)
from `100` group by provider, title, hd_or_sd with rollup
我想旋转 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 entertainment,它显示了 5 个标题,但具有 hd_or_sd 值 "HD",即使其中一些标题是 SD。为什么会出现这种情况?
这是数据示例:http://sqlfiddle.com/#!9/a9b5d9/1。这是最终结果应该是什么(减去奇怪的数字格式)——
请注意,我还想在不使用 CASE
语句的情况下完成此操作,因为旋转列可能具有许多不同的值。
您需要将 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 |
或者,您可以通过 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