doumi9618 2018-09-28 10:50
浏览 76
已采纳

从多个表中按列SELECT和按ID分组

I have the following data

purchase_rec_a

+----------+------------+----------------+
| user     | date       | total          |
+----------+------------+----------------+
| 1510     | 2018-08-08 | 5              |
| 1510     | 2018-09-12 | 10             |
| 1510     | 2018-09-19 | 15             |
+----------+------------+----------------+

purchase_rec_b

+----------+------------+----------------+
| user     | date       | total          |
+----------+------------+----------------+
| 1510     | 2018-05-08 | 10             |
| 1510     | 2018-07-02 | 15             |
+----------+------------+----------------+

I can LEFT JOIN both tables by following sql

SELECT u.id, a.date, a.total AS total_a, b.date, b.total AS 
total_b
FROM users AS u
LEFT JOIN purchase_rec_a AS a ON u.id = a.user
LEFT JOIN purchase_rec_b AS b ON u.id = b.user
WHERE u.id =1510

and I get the following

+-------+------------+---------+------------+---------+
| id    | date       | total_a | date       | total_b |
+-------+------------+---------+------------+---------+
| 1510  | 2018-08-08 | 5       | 2018-05-08 | 10      |
| 1510  | 2018-08-08 | 5       | 2018-07-02 | 15      |
| 1510  | 2018-09-12 | 10      | 2018-05-08 | 10      |
| 1510  | 2018-09-12 | 10      | 2018-07-02 | 15      |
| 1510  | 2018-09-19 | 15      | 2018-05-08 | 10      |
| 1510  | 2018-09-19 | 15      | 2018-07-02 | 15      |
+-------+------------+---------+------------+---------+

so what I'm trying to do here is to group them in distinctive way by total_a and total_b and group by user so I tried

SELECT u.id, a.date, SUM( a.total ) AS total_a, 
b.date, SUM( b.total ) AS total_b
FROM users AS u
LEFT JOIN purchase_rec_a AS a ON u.id = a.user
LEFT JOIN purchase_rec_b AS b ON u.id = b.user
WHERE u.id =1510

and the result is

+------+------------+---------+------------+---------+
| id   | date       | total_a | date       | total_b |
+------+------------+---------+------------+---------+
| 1510 | 2018-08-08 | 60      | 2018-05-08 | 75      |
+------+------------+---------+------------+---------+

but the problem here is some data are duplicated and they are added as total

my expected result are below

+------+------------+---------+------------+---------+
| id   | date       | total_a | date       | total_b |
+------+------------+---------+------------+---------+
| 1510 | 2018-08-08 | 30      | 2018-05-08 | 25      |
+------+------------+---------+------------+---------+

I can get the above result by multiple SQL but I want it in single SQL, is it possible?

  • 写回答

2条回答 默认 最新

  • du4822 2018-09-28 10:53
    关注

    If you want to put the data "side-by-side", I would suggest union all and group by:

    select user, date, sum(a_total) as a_total, sum(b_total) as b_total
    from ((select user, date, total as a_total, 0 as b_total
           from purchase_rec_a
          ) union all
          (select user, date, 0 as a_total, total as b_total
           from purchase_rec_b
          )
         ) ab
    group by user, user_date;
    

    If you don't want date in the result, you can use the same structure, just changing the outer query:

    select user, sum(a_total) as a_total, sum(b_total) as b_total
    from ((select user, date, total as a_total, 0 as b_total
           from purchase_rec_a
          ) union all
          (select user, date, 0 as a_total, total as b_total
           from purchase_rec_b
          )
         ) ab
    group by user;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊