简体   繁体   English

使用MySql透视数据

[英]Pivoting Data Using MySql

Objective 1: Your sales data is stored in the Purchases table. 目标1:您的销售数据存储在“购买”表中。 Your sales staff wants to see the sales data in a pivoted form, broken down by quarter. 您的销售人员希望以透视形式查看销售数据,按季度细分。

If your Purchases table doesn't have sales data, create some. 如果您的购买表没有销售数据,请创建一些。 Be sure the data spans four quarters. 确保数据跨越四个季度。 Next, write a query to pivot the data as follows: 接下来,编写查询以按如下所示枢转数据:

Album              Q1   Q2   Q3   Q4

OK Computer        2    5    3    7

 Sea Change        8    6    2    1 

Do not create a separate table or a view. 不要创建单独的表或视图。 Do not alter any tables. 请勿更改任何表格。

Save your query as dba1lesson10project1.sql and hand in the project. 将查询另存为dba1lesson10project1.sql并提交项目。

This is What I need to do. 这就是我需要做的。 But, the table it wants me to work with looks like this. 但是,它要我使用的表如下所示。 And it states in the assignment I cannot alter it at all. 它指出作业中我根本无法更改它。

CustomerID    DateOfPurchase   SongID

1              2007-03-31        3
3              2007-06-30        4
4              2007-09-30        4
5              2007-12-31        5

I have tried 我努力了

  SELECT SongID,
  SUM(CASE WHEN DateOfPurchase = '2007-03-31' THEN DateOfPurchase ElSE 0 END) AS 'Q1',
  SUM(CASE WHEN DateOfPurchase = '2007-06-30' THEN DateOfPurchase ELSE 0 END) AS 'Q2',
  SUM(CASE WHEN DateOfPurchase = '2007-09-30' THEN DateOfPurchase ELSE 0 END) AS 'Q3',
  SUM(CASE WHEN DateOfPurchase = '2007-12-31' THEN DateOfPurchase ELSE 0 END) AS 'Q4'
  FROM Purchases
  GROUP BY SongID;

Along with other variants: 连同其他变体:

  SELECT SongID,
  SUM(CASE WHEN DateOfPurchase = '2007-03-31' THEN CustomerID ElSE 0 END) AS 'Q1',
  SUM(CASE WHEN DateOfPurchase = '2007-06-30' THEN CustomerID ELSE 0 END) AS 'Q2',
  SUM(CASE WHEN DateOfPurchase = '2007-09-30' THEN CustomerID ELSE 0 END) AS 'Q3',
  SUM(CASE WHEN DateOfPurchase = '2007-12-31' THEN CustomerID ELSE 0 END) AS 'Q4'
  FROM Purchases
  GROUP BY SongID;

And: 和:

  SELECT SongID,
  SUM(CASE WHEN DateOfPurchase = '2007-03-31' THEN SongID ElSE 0 END) AS 'Q1',
  SUM(CASE WHEN DateOfPurchase = '2007-06-30' THEN SongID ELSE 0 END) AS 'Q2',
  SUM(CASE WHEN DateOfPurchase = '2007-09-30' THEN SongID ELSE 0 END) AS 'Q3',
  SUM(CASE WHEN DateOfPurchase = '2007-12-31' THEN SongID ELSE 0 END) AS 'Q4'
  FROM Purchases
  GROUP BY SongID;

Which, the last 2 almost get me what I need. 其中,最后2个几乎可以满足我的需求。 But, there is only one purchase per SongID and Quarter. 但是,每个SongID和Quarter仅购买一次。 They show me either the CustomerID or SongID instead. 他们向我显示CustomerID或SongID。 I have a basic understand of what I need to do. 我对需要做的事情有基本的了解。 But without being able to alter the table to show how many purchases there have actually been I'm not sure what to do. 但是,由于无法更改表格以显示实际上已经进行了多少次购买,因此我不确定该怎么做。 Any suggestions? 有什么建议么?

Your current query is very close, I would suggest a few minor changes. 您当前的查询非常接近,我建议您进行一些小的更改。 You are hard-coding the date but what happens if you have a date in quarter one that is not equal to 2007-03-31 it won't show up. 您正在对日期进行硬编码,但是如果您在第一季度中有一个不等于2007-03-31 ,它将不会显示,该怎么办。

I would use the QUARTER() function in MySQL, this will return the quarter 1-4 based on the date value. 我将在MySQL中使用QUARTER()函数,这将基于日期值返回四分之一至四分之一。

Second, I don't think you want to sum the SongID , in your CASE expression I would replace the SongID with 1 similar to the following: 第二,我不认为你需要sumSongID ,你的情况我表达将取代SongID 1类似以下内容:

SELECT SongID,
  SUM(CASE WHEN Quarter(DateOfPurchase) = 1 THEN 1 ElSE 0 END) AS Q1,
  SUM(CASE WHEN Quarter(DateOfPurchase) = 2 THEN 1 ELSE 0 END) AS Q2,
  SUM(CASE WHEN Quarter(DateOfPurchase) = 3 THEN 1 ELSE 0 END) AS Q3,
  SUM(CASE WHEN Quarter(DateOfPurchase) = 4 THEN 1 ELSE 0 END) AS Q4
FROM Purchases
GROUP BY SongID;

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

You can do this quarterly report easily using MySQL Pivot table generator . 您可以使用MySQL Pivot表生成器轻松地执行此季度报告。 While creating your report, you will be asked to add the "Column settings " 创建报告时,系统会要求您添加“列设置” 列设置 Please choose the "Purchasing table" as the " Table ", the column that contains the date values as the " Field " . 请选择“采购表”作为“ ”,将包含日期值的列作为“ 字段 ”。 Once selecting a date value, a function menu should appear, please select " Quarter " to get your pivot table divided in quarters as you requested. 选择日期值后,应该会出现一个功能菜单,请选择“ Quarter ”,以按照您的要求将数据透视表划分为四分之一。 please note that you have the option to create a report for one specific year if you like and in this case you should check the " Exact Year " Box and add a specific year otherwise leave the " Exact year " box unchecked . 请注意,如果愿意,您可以选择创建特定年份的报告,在这种情况下,应选中“ 精确年份 ”框并添加特定年份,否则不要选中“ 精确年份 ”框。

The final report will be able to see something like this report : 最终报告将能够看到类似此报告的内容:

http://mysqlpivottable.net/MySQL-Pivot-Table-Demo/tables/Quarterly_Sales_Report/ http://mysqlpivottable.net/MySQL-Pivot-Table-Demo/tables/Quarterly_Sales_Report/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM