简体   繁体   English

使用MySQL联接表和数据

[英]Joining Tables and Pivoting Data with 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 know I need to join three tables together so I can group by the title. 我知道我需要将三个表连接在一起,以便可以按标题分组。 Which are my Songs, Albums, and Purchases tables. 这是我的歌曲,专辑和购买表。

 SELECT Albums.Title FROM Albums
 LEFT JOIN Songs
 INNER JOIN Purchases
 ON Songs.SongID = Purchases.SongID
 ON Albums.Title = Purchases.SongID,
 SELECT Title,
 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 Title;

I'm kind of at a loss here when it comes to Joining three separate tables then pivoting the data 在连接三个单独的表然后旋转数据时,我有点不知所措

I've tried the code above in multiple other variants which has failed me past the table joining portion. 我已经在其他多个变体中尝试了上面的代码,这使我无法通过表连接部分。

Any help would be much appreciated. 任何帮助将非常感激。

My suggestion before attempting to PIVOT the data would be to first, write the query to return the columns that you need, this will involve joining your tables. 我的建议是在尝试PIVOT数据之前首先是编写查询以返回所需的列,这将涉及联接表。 You didn't provide your table definitions so I am making a few guesses on the structure. 您没有提供表定义,所以我在结构上做了一些猜测。 If your tables are structured similar to the following: 如果表的结构类似于以下内容:

CREATE TABLE Purchases
    (`CustomerID` int, `DateOfPurchase` datetime, `SongID` int)
;

CREATE TABLE Albums
    (`AlbumId` int, `Title` varchar(11))
;

CREATE TABLE Songs
    (`SongID` int, `AlbumID` int)
;

Then you would SELECT from the tables using a JOIN similar to this code: 然后,您将使用类似于以下代码的JOIN从表中进行选择:

select a.title,
  p.DateOfPurchase
from albums a
inner join songs s
  on a.albumid = s.albumId
inner join purchases p
  on s.songid = p.songid

This query will return to you the album Title as well as the DateOfPurchase which you need to pivot the data. 此查询将返回给您专辑Title以及需要透视数据的DateOfPurchase Once you have the JOINS worked out, then you can replace the p.DateOfPurchase with your aggregate function and CASE expression and add the GROUP BY clause to get the final result: 一旦确定了JOINS,就可以用聚合函数和CASE表达式替换p.DateOfPurchase ,并添加GROUP BY子句以获得最终结果:

select a.title,
  SUM(CASE WHEN Quarter(p.DateOfPurchase) = 1 THEN 1 ElSE 0 END) AS Q1,
  SUM(CASE WHEN Quarter(p.DateOfPurchase) = 2 THEN 1 ELSE 0 END) AS Q2,
  SUM(CASE WHEN Quarter(p.DateOfPurchase) = 3 THEN 1 ELSE 0 END) AS Q3,
  SUM(CASE WHEN Quarter(p.DateOfPurchase) = 4 THEN 1 ELSE 0 END) AS Q4
from albums a
inner join songs s
  on a.albumid = s.albumId
inner join purchases p
  on s.songid = p.songid
group by a.title;

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle This gives a result: 结果如下:

|       TITLE | Q1 | Q2 | Q3 | Q4 |
| OK Computer |  1 |  0 |  0 |  0 |
|  Sea Change |  0 |  1 |  1 |  0 |

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

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