【问题标题】:full outer self join with count for both alias mysql两个别名 mysql 的完全外部自连接计数
【发布时间】:2017-01-25 06:31:55
【问题描述】:

我有一个表 diagnoses 有 3 个字段

 id(PK), created_date(DateTime), doctor_id(Foreign Key).
 doctor1 = 4 (doctor_id), 
 and 
 doctor2= 11(doctor_id). 

医生 1 早上到达,医生 2 晚上到达。众所周知,当我们运行 Count(*) 函数时,到达的日期是第一次找到记录的日期。 所以我想打印这样的东西

    Date | Am-TimeIn | Doctor1 (Count) | Pm-TimeIn | Doctor2 (Count)

所以如果doctor1 不存在而doctor2 存在,则打印行,对于医生2 的缺席也是如此。

它需要 full outer self join ,在 Date(created_date) 上按子句分组,以便每天汇总每位医生的所有记录。目前我有这个,但它不工作

    SELECT Date(CASE WHEN (a.created_date IS NOT NULL) THEN a.created_date ELSE b.created_date END) 'Date', a.`doctor1`, TIME(a.created_date) 'AM-TimeIn', b.`doctor2`,  TIME(b.created_date) 'PM-TimeIn'
    From (select created_date, count(*) doctor1 FROM diagnoses WHERE  doctor_id = 4 GROUP BY DATE(created_date)) a
    full join
    (select created_date, count(*) doctor2 FROM diagnoses WHERE doctor_id = 11 GROUP BY DATE(created_date)) b
    ON DATE(b.created_date) = DATE(a.created_date);

【问题讨论】:

  • 给出你的样本数据和期望的输出
  • drive.google.com/file/d/… 这是带有样本数据的报告,这是所需的输出日期 |上午时间 | Doctor1 (计数) |下午时间在 |医生2(计数)
  • 图像报告与您的问题(或您的查询)中所需的输出格式不匹配 - 您想要哪种?
  • 这是包含实际数据的现有报告。这是您需要的虚拟数据drive.google.com/file/d/0B6pa5Jz8TJ3fVHNEbW1Ia0Vrb1k/…
  • 我想要这个“日期 | Am-TimeIn | Doctor1 (Count) | Pm-TimeIn | Doctor2 (Count)”。而 Am-TimeIn = 从 created_date 提取的时间,是医生 1 早上检查的第一位患者的时间。 Doctor1 (Count) = 每天由 docotr1 检查的所有患者的计数。 Pm-TimeIn = 从 created_date 中提取的时间,是医生 2 在下午诊断出的第一个患者的时间。反之亦然。

标签: mysql count group-by self-join full-outer-join


【解决方案1】:
MariaDB [sandbox]> drop table if exists diagnosis;
Query OK, 0 rows affected (0.09 sec)

MariaDB [sandbox]> create table diagnosis(id int, dt datetime,doctor_id int);
Query OK, 0 rows affected (0.22 sec)

MariaDB [sandbox]> insert into diagnosis values
    -> (1,'2017-01-01 07:00:00',1),(1,'2017-01-01 08:00:00',1),(1,'2017-01-01 13:00:00',2),
    -> (1,'2017-01-02 07:00:00',1),(1,'2017-01-02 08:00:00',1),
    -> (1,'2017-01-03 13:00:00',2);
Query OK, 6 rows affected (0.02 sec)
Records: 6  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select   min(case when doctor_id = 1 then doctor_id end) doc1,
    -> min(case when doctor_id = 1 then dt end) am_timein,
    -> sum(case when doctor_id = 1 then 1 else 0 end)  doc1cases,
    -> min(case when doctor_id = 2 then doctor_id end) doc2,
    -> min(case when doctor_id = 2 then dt end) pm_timein,
    -> sum(case when doctor_id = 2 then 1 else 0 end)  doc2cases,
    -> count(*) as Total
    -> from diagnosis
    -> group by date(dt);
+------+---------------------+-----------+------+---------------------+-----------+-------+
| doc1 | am_timein           | doc1cases | doc2 | pm_timein           | doc2cases | Total |
+------+---------------------+-----------+------+---------------------+-----------+-------+
|    1 | 2017-01-01 07:00:00 |         2 |    2 | 2017-01-01 13:00:00 |         1 |     3 |
|    1 | 2017-01-02 07:00:00 |         2 | NULL | NULL                |         0 |     2 |
| NULL | NULL                |         0 |    2 | 2017-01-03 13:00:00 |         1 |     1 |
+------+---------------------+-----------+------+---------------------+-----------+-------+
3 rows in set (0.00 sec)

【讨论】:

  • 它有效。非常感谢。很高兴:) 再次感谢。选择日期(创建日期)Date,分钟(医生 ID = 4 时的情况,然后时间(创建日期)结束)Am-TimeIn,总和(医生 ID = 4 时的情况,然后 1 否则 0 结束)Doc1 Cases,分钟(医生 ID = 时的情况11 然后 Time(created_date) end) Pm-TimeIn, sum(case when doctor_id = 11 then 1 else 0 end) Doc2 Cases, count(*) as Total from diagnostics where doctor_id in (4,11) group by date(created_date );
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 2021-10-12
  • 2014-08-08
相关资源
最近更新 更多