【发布时间】: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