加入表格 - 有点复杂

时间:2013-07-10 15:19:48

标签: mysql join group-by

每周,我的客户通过第三方营销团队开展广播节目和电子邮件活动,以宣传他的产品。他想知道他每周有多少赚钱/消费,并为他分析数据。表数据如下所示:

tablename: orders
+----------+--------------+---------------------+------------+
| order_id | order_amount | referral            | order_date |
+----------+--------------+---------------------+------------+
| 1        | 16.00        | Radio: Jack Johnson | 2013-07-01 |
| 2        | 50.00        | Radio: Jack Johnson | 2013-07-02 |
| 3        | 16.00        | Radio: Jack Johnson | 2013-07-02 |
| 4        | 50.00        | Radio: Thabo Mbeki  | 2013-07-03 |
| 5        | 16.00        | Email: Renewal      | 2013-07-08 |
| 6        | 50.00        | Other               | 2013-07-08 |
| 7        | 16.00        | Email: Renewal      | 2013-07-08 |
| 8        | 50.00        | Radio: Thabo Mbeki  | 2013-07-09 |
+----------+--------------+--------------+-------------------+

tablename: media
+----------+-------+-------+--------------+-------+------------+
| media_id | spots | cost  | name         | type  | date       |
+----------+-------+-------+--------------+-------+------------+
| 1        | 10    | 10000 | Jack Johnson | radio | 2013-07-01 |
| 2        | 5     | 5000  | Thabo Mbeki  | radio | 2013-07-01 |
| 3        | 10    | 10000 | Jack Johnson | radio | 2013-07-08 |
| 4        | 5     | 5000  | Thabo Mbeki  | radio | 2013-07-08 |
| 5        | 1     | 250   | Renewal      | email | 2013-07-08 |
+----------+---------------+--------------+--------------------+

我需要生成一个这样的HTML表:

+------------+--------------+-------+------+----------+---------+--------------+----------------+
| week       | name         | type  |spots | cost     | revenue | total orders | avg. per order |
+------------+--------------+-------+------+----------+---------+--------------+----------------+
| 2013-07-01 | Jack Johnson | radio | 10   | 10000    | 82.00   | 3            | 27.33          |
| 2013-07-01 | Thabo Mbeki  | radio | 5    | 5000     | 50.00   | 1            | 50.00          |
+------------+--------------+-------+------+----------+---------+--------------+----------------+

对于我来说,我试图计算如何加入表格,并通过订单的推荐字段对它们进行分组,这很复杂。由于orders.referral与media.name不匹配,我猜测有WHERE referral LIKE %name,但我不知道该怎么做。

3 个答案:

答案 0 :(得分:0)

如果忽略“其他”,则更好的连接WHERE子句将是

WHERE orders.order_date = media.date
 AND  LOWER(orders.referral)   = LOWER(media.type + ": " + media.name)

这是Sybase语法,如果您需要为MySQL调整一下,我道歉

要处理“其他”,只需对“媒体”表进行外部联接。

像(没有检查正确性)的东西会做一个每日报告;如果你愿意的话,你需要按周进行按摩(这并不难,但我觉得这不在你问题的主要范围内)

SELECT SUM(order.amount) AS revenue, COUNT(order.amount) as total_orders
      ,SUM(order.amount)/COUNT(order.amount) as average_order
      ,SUM(spots) spots, media.type, media.name, order_date
FROM   order
LEFT OUTER JOIN media 
ON        orders.order_date = media.date
     AND  LOWER(orders.referral)   = LOWER(media.type + ": " + media.name)
GROUP BY order.referral, order.date
-- May be add to group by:    , COALESCE(media.type,media.name,"Other")

答案 1 :(得分:0)

我相信这应该有效:

SELECT m.name, [other needed columns]
FROM media m JOIN orders o ON o.referral LIKE '%' + m.name +'%'
WHERE m.date between [date] and [date]
GROUP BY m.name, [other groupings]

希望这有帮助!

答案 2 :(得分:0)

这里帮助很大。这是我最终使用的查询:

SELECT m.date, m.name, m.type, m.spots, m.cost, 
       SUM(o.order_amount) as revenue, 
       COUNT(o.order_amount) as orders,
       SUM(o.order_amount)/COUNT(o.order_amount) as average_order 
FROM   media m
JOIN   orders o
ON     LOWER(o.referral) = CONCAT(LOWER(m.type), ': ' ,LOWER(m.name))
WHERE  (DATE(o.order_date) BETWEEN '2013-06-24' AND '2013-06-30')
GROUP BY m.name, m.type