简体   繁体   中英

Pivoting Data Using MySql

Objective 1: Your sales data is stored in the Purchases table. 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.

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. But, there is only one purchase per SongID and Quarter. They show me either the CustomerID or SongID instead. 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.

I would use the QUARTER() function in MySQL, this will return the quarter 1-4 based on the date value.

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:

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

You can do this quarterly report easily using MySQL Pivot table generator . 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. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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