Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在SQL中构建数据透视表:聚合排序_Mysql_Sql_Sql Server - Fatal编程技术网

Mysql 在SQL中构建数据透视表:聚合排序

Mysql 在SQL中构建数据透视表:聚合排序,mysql,sql,sql-server,Mysql,Sql,Sql Server,我正致力于在SQL(mysql)中构建一个透视表,我想一步一步地完成这个过程,所以这个问题将成为尝试在SQL中构建一个越来越复杂的透视表的系列文章的一部分 我有以下给出标题的模式: 提供者 头衔 收入 我想构建一个按提供者和标题分组的聚合,如下所示: provider title revenue Sony titanic 9.99 Paramount T2 14.99 Sony star wars 12.99 这很简单

我正致力于在SQL(mysql)中构建一个透视表,我想一步一步地完成这个过程,所以这个问题将成为尝试在SQL中构建一个越来越复杂的透视表的系列文章的一部分

我有以下给出标题的模式:

  • 提供者
  • 头衔
  • 收入
我想构建一个按提供者和标题分组的聚合,如下所示:

provider    title      revenue
Sony        titanic    9.99
Paramount   T2         14.99
Sony        star wars  12.99
这很简单,我们可以通过以下方式进行汇总:

SELECT provider, title, SUM(revenue) FROM table GROUP BY provider, title
接下来我要做的是,首先对提供者进行排序,根据该提供者的收入总额,然后根据标题,按字母顺序进行排序。例如,上述内容应分类为:

[-] Sony (12.99+9.99 = 22.98)
    - star wars (A-Z)
    - titanic (A-Z)
[-] Paramount (9.99)
    - T2
如何在SQL中的聚合中执行此排序?下面是一个处理示例数据的sql文件:

无法打开您的SQLfiddle,我已将您的SQL复制到我的本地站点,结果如下:

您所需要的只是一个用于查询的子句:

select provider, title, sum(customer_price) revenue from `100`
group by provider, title
with rollup
“总计”行在“提供程序”和“标题”列中具有空值(最后一行是总计行),如下所示:

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

如果您使用的是MySQL,为什么要将其标记为SQL Server和BigQuery?@ElliottBrossard我认为同样的SQL也适用(而且它不是特定于供应商的查询)。我认为使用所有标记可以显示查询适用于任何主sql数据库,但如果这是错误的,请纠正我。不过,这似乎不是按提供程序revenue desc排序的?我将把它作为一个练习留给您看一看SQLFiddle数据示例。上面的内容似乎没有正确排序。SQLFiddle不工作,但当我将您的SQL复制到本地计算机时,工作正常
|               provider |                      title | revenue |
|------------------------|----------------------------|---------|
|             DISTRIBBER |                Finding Joe |   16.99 |
|             DISTRIBBER |                     (null) |   16.99 |
|            Echo Bridge |               Do Something |    1.99 |
|            Echo Bridge |                 Down in LA |       0 |
|            Echo Bridge | The L.A. Complex, Season 1 |   19.99 |
|            Echo Bridge | The Other Side of the Door |    6.97 |
|            Echo Bridge |               Who You Know |    3.98 |
|            Echo Bridge |                     (null) |   32.93 |
| Electric Entertainment |         Leverage, Season 4 |   31.99 |
| Electric Entertainment |     The Cross My Heart Job |    2.99 |
| Electric Entertainment |             The Inside Job |    1.99 |
| Electric Entertainment |              The Radio Job |    1.99 |
| Electric Entertainment |       The Scheherazade Job |    2.99 |
| Electric Entertainment |                     (null) |   41.95 |
|               HALLMARK |      The Good Witch's Gift |    3.99 |
|               HALLMARK |                     (null) |    3.99 |
|            Quebec Inc. |        2 Frogs In the West |    5.99 |
|            Quebec Inc. |                     (null) |    5.99 |
|                 VIRGIL |         One Lucky Elephant |    3.99 |
|                 VIRGIL |                     (null) |    3.99 |
|                 (null) |                     (null) |  105.84 |