我正在尝试从数据库中获取我们的用户列表,以及与他们来自同一队列的人数-在这种情况下,它们被定义为同时来自同一所医学院。
medical_school_id
存储在doctor_record
表中
graduation_dt
也存储在doctor_record
表中。
我设法使用子查询写出该查询,该查询执行一个select语句,计算每行中其他行的数量,但这要花很多时间。我的逻辑告诉我,我应该先运行一个简单的GROUP BY查询,然后再以某种方式将medical_school_id加入其中。
按查询分组如下
select count(ca.id) , cdr.medical_school_id, cdr.graduation_dt
from account ca
LEFT JOIN doctor cd on ca.id = cd.account_id
LEFT JOIN doctor_record cdr on cd.gmc_number = cdr.gmc_number
GROUP BY cdr.medical_school_id, cdr.graduation_dt
长选择查询为
select a.id, a.email , dr.medical_school_id,
(select count(ba.id) from account ba
LEFT JOIN doctor bd on ba.id = bd.account_id
LEFT JOIN doctor_record bdr on bd.gmc_number = bdr.gmc_number
WHERE bdr.medical_school_id = dr.medical_school_id AND bdr.graduation_dt = dr.graduation_dt) AS med_count,
from account a
LEFT JOIN doctor d on a.id = d.account_id
LEFT JOIN doctor_record dr on d.gmc_number = dr.gmc_number
如果您可以将我推向正确的方向,那就太好了
答案 0 :(得分:0)
我认为您只需要窗口功能:
select a.id, a.email, dr.medical_school_id, dr.graduation_dt,
count(*) over (partition by dr.medical_school_id, dr.graduation_dt) as cohort_size
from account a left join
doctor d
on a.id = d.account_id left join
doctor_record dr
on d.gmc_number = dr.gmc_number;
答案 1 :(得分:0)
对分组依据使用相同的代码:
SELECT * FROM (
(
SELECT acc.[id]
, acc.[email]
FROM
account acc
LEFT JOIN
doctor doc
ON
acc.id = doc.account_id
LEFT JOIN
doctor_record doc_rec
ON
doc.gmc_number = doc_rec.gmc_number
) label
LEFT JOIN
(
SELECT count(acco.id)
, doc_reco.medical_school_id
, doc_reco.graduation_dt
FROM
account acco
LEFT JOIN
doctor doct
ON
acco.id = doct.account_id
LEFT JOIN
doctor_record doc_reco
ON
doct.gmc_number = doc_reco.gmc_number
GROUP BY
doc_reco.medical_school_id,
doc_reco.graduation_dt
) count
ON
count.[medical_school_id]=label.[medical_school_id]
AND
count.[graduation_dt]=label.[graduation_date]
)
答案 2 :(得分:0)
这样的事情怎么样?
val df = (p1, p2, p3) match {
case (Some(x), Some(y), Some(z)) => getRules(x, y, z)
case _ => throw new Exception("One or more values were NONE!")
}
从计数中减去1,这样就不会在“同一队列中的其他人”中计数医生了
我将“相同的队列”定义为“相同的医学院和毕业日期”。
我不清楚GMC号码是什么以及如何关联。与队列有关吗?