我正在尝试为我的应用做一个预先报告。现在我遇到了一些问题。
以下是详细信息:
table_product
id | name
-----+---------
1 | Book
2 | Pen
3 | Ruler
4 | Calendar
5 | Scissors
table_event
id | name
-----+---------
1 | Launching
2 | Sale
3 | Roadshow
4 | Presentation
5 | Normal
table_transaction
---+------------+-------+-----------+---------+-----------+---------+-----------+---------+----------+
id | date | event | product_1 | share_1 | product_2 | share_2 | product_3 | share_3 | amount |
---+------------+-------+-----------+---------+-----------+---------+-----------+---------+----------+
1 | 2013-01-12 | 1 | 3 | 50 | 2 | 50 | | | 5000 |
2 | 2013-02-13 | 3 | 2 | 50 | 1 | 25 | 4 | 25 | 10000 |
3 | 2013-01-13 | 5 | 5 | 50 | 4 | 50 | | | 7000 |
4 | 2013-04-13 | 2 | 1 | 100 | | | | | 5000 |
---+------------+-------+-----------+---------+-----------+---------+-----------+---------+----------+
我想要显示的是这样的
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
Product Name | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
Book | | 2500 | | 5000 | | | | | | | | |
Pen | 2500 | 5000 | | | | | | | | | | |
Ruler | 2500 | | | | | | | | | | | |
Calendar | 3500 | 2500 | | | | | | | | | | |
Scissors | 3500 | | | | | | | | | | | |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
我尝试过: 我使用以下查询
成功生成事件名称分组的输出SELECT t1.name as sort_name,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 1, t2.amount, 0)) as total_jan,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 2, t2.amount, 0)) as total_feb,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 3, t2.amount, 0)) as total_mar,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 4, t2.amount, 0)) as total_apr,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 5, t2.amount, 0)) as total_may,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 6, t2.amount, 0)) as total_jun,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 7, t2.amount, 0)) as total_jul,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 8, t2.amount, 0)) as total_aug,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 9, t2.amount, 0)) as total_sep,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 10, t2.amount, 0)) as total_oct,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 11, t2.amount, 0)) as total_nov,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 12, t2.amount, 0)) as total_dec,
sum(IF(YEAR(t2.date) = 2013, t2.amount, 0)) as event_total
FROM events t1
LEFT JOIN transactions t2 ON t2.event_id = t1.id
GROUP BY sort_name
但我不知道按产品分组。我尝试过使用union但仍然返回SQL语法错误。
答案 0 :(得分:0)
使用相同的查询,并使用子查询创建table_product
的规范化版本:
SELECT t1.name as sort_name,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 1, t2.amount, 0)) as total_jan,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 2, t2.amount, 0)) as total_feb,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 3, t2.amount, 0)) as total_mar,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 4, t2.amount, 0)) as total_apr,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 5, t2.amount, 0)) as total_may,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 6, t2.amount, 0)) as total_jun,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 7, t2.amount, 0)) as total_jul,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 8, t2.amount, 0)) as total_aug,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 9, t2.amount, 0)) as total_sep,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 10, t2.amount, 0)) as total_oct,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 11, t2.amount, 0)) as total_nov,
sum(IF(YEAR(t2.date) = 2013 AND MONTH(t2.date) = 12, t2.amount, 0)) as total_dec
FROM table_product t1
LEFT OUTER JOIN
(
SELECT product_1 AS product_id,
amount * share_1 / (share_1 + COALESCE(share_2,0) + COALESCE(share_3,0))
AS amount
FROM table_transaction
WHERE product_1 IS NOT NULL
UNION ALL
SELECT product_2 AS product_id,
amount * share_2 / (share_2 + COALESCE(share_1,0) + COALESCE(share_3,0))
AS amount
FROM table_transaction
WHERE product_2 IS NOT NULL
UNION ALL
SELECT product_3 AS product_id,
amount * share_3 / (share_3 + COALESCE(share_2,0) + COALESCE(share_1,0))
AS amount
FROM table_transaction
WHERE product_3 IS NOT NULL
) t2 ON t2.product_id = t1.id
GROUP BY sort_name
答案 1 :(得分:0)
您必须加入产品表而不是事件