MySQL销售报告

问题描述 投票:0回答:1

我有下表:

users
+-------+-----------+-----------+
| id    | source    | age_group |
+-------+-----------+-----------+
| 1     | google    | 18 - 22   |
+-------+-----------+-----------+
| 2     | facebook  | 22 - 25   |
+-------+-----------+-----------+
| 3     | site      | 25 - 35   |
+-------+-----------+-----------+
| 4     | google    | 25 - 35   |
+-------+-----------+-----------+
| 5     | google    | 18 - 22   |
+-------+-----------+-----------+

orders
+-------+-----------+
| id    | userId    |
+-------+-----------+
| 1     | 3         |
+-------+-----------+
| 2     | 1         |
+-------+-----------+
| 3     | 3         |
+-------+-----------+
| 4     | 2         |
+-------+-----------+
| 5     | 4         |
+-------+-----------+
| 6     | 5         |
+-------+-----------+

orders_payments:
+-------+-----------+-----------+
| id    | orderId   | amount    |
+-------+-----------+-----------+
| 1     | 1         | 10        |
+-------+-----------+-----------+
| 2     | 2         | 0         |
+-------+-----------+-----------+
| 3     | 3         | 40        |
+-------+-----------+-----------+
| 4     | 5         | 0         |
+-------+-----------+-----------+
| 5     | 6         | 0         |
+-------+-----------+-----------+

还有3种可能性:

  1. 用户下达的订单和已付款(orders_payments金额> 0)
  2. 用户免费试用了一项服务(orders_payments金额= 0)
  3. 用户下达订单但未显示(订单付款中没有记录)

我需要一个看起来像这样的销售报告:

+-----------+-----------+-----------+-----------+-----------+
| source    | age_group | paid      | tried     | no_show   |
+-----------+-----------+-----------+-----------+-----------+
| google    | 18 - 22   | 0         | 2         | 0         |
+-----------+-----------+-----------+-----------+-----------+
| google    | 25 - 35   | 0         | 1         | 0         |
+-----------+-----------+-----------+-----------+-----------+
| facebook  | 22 - 25   | 0         | 0         | 1         |
+-----------+-----------+-----------+-----------+-----------+
| site      | 25 - 35   | 1         | 0         | 0         |
+-----------+-----------+-----------+-----------+-----------+

后三列表示至少支付一次,免费试用服务或未参加约会的用户数量。

报告按来源分组,然后按age_group分组。所以像这样]

SELECT source, age_group, ... FROM users GROUP BY source, age_group

[我一生无法解决。

任何想法都将不胜感激。

谢谢!

我有下表:用户+ ------- + ------------- + ----------- + | id |来源| age_group | + ------- + ----------- + ----------- + | 1 |谷歌| 18-22 | + ------- + ----------- + --------...

mysql report
1个回答
0
投票

我将在付款表上输入left join,然后进行条件汇总。

© www.soinside.com 2019 - 2024. All rights reserved.