1

目标 1:您的销售数据存储在 Purchases 表中。您的销售人员希望以透视的形式查看按季度细分的销售数据。

如果您的 Purchases 表没有销售数据,请创建一些。确保数据跨越四个季度。接下来,编写一个查询来透视数据,如下所示:

Album              Q1   Q2   Q3   Q4

OK Computer        2    5    3    7

 Sea Change        8    6    2    1 

不要创建单独的表或视图。不要更改任何表格。

将您的查询保存为 dba1lesson10project1.sql 并提交项目。

这是我需要做的。但是,它希望我使用的表看起来像这样。它在作业中指出我根本无法更改它。

CustomerID    DateOfPurchase   SongID

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

我努力了

  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;

连同其他变体:

  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;

和:

  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;

其中,最后两个几乎可以满足我的需求。但是,每个 SongID 和 Quarter 只有一次购买。他们向我展示了 CustomerID 或 SongID。我对我需要做什么有一个基本的了解。但是由于无法更改表格以显示实际购买了多少,我不知道该怎么做。有什么建议么?

4

2 回答 2

0

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

于 2013-09-15T14:04:50.667 回答
0

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/

于 2015-01-17T19:21:12.130 回答