我正在尝试获取所有记录,如果其中1个包含n,那么如果我有3条记录,并且其中任何一个的disposition_id = 1并且它们的time_sup总计为20或更多并且属于同一个Patient_id
Table: Transactions
+----+------------+----------------+------------+------------+
| id | patient_id | disposition_id | time_spent | created_at |
+----+------------+----------------+------------+------------+
| 1 | 10 | 25 | 10 | NOW() |
+----+------------+----------------+------------+------------+
| 2 | 10 | 1 | 2 | NOW() |
+----+------------+----------------+------------+------------+
| 3 | 10 | 12 | 9 | NOW() |
+----+------------+----------------+------------+------------+
| 4 | 12 | 1 | 8 | NOW() |
+----+------------+----------------+------------+------------+
预期结果
Table: Transactions
+------------+------------+
| patient_id | time_spent |
+------------+------------+
| 10 | 21 |
+------------+------------+
Patient_id 12不会显示,即使它的disposition_id为1,也不会花费20或更多的时间。但是Patient_id 10确实有两者。
到目前为止,我的查询是:
SELECT t.patient_id, t.disposition_id AS did, SUM(t.time_spent) AS sum_time
FROM transactions AS t
WHERE
YEAR(t.created_at) = YEAR(CURDATE()) AND MONTH(t.created_at) = MONTH(CURDATE())
GROUP BY
t.patient_id
HAVING sum_time >= 20
我尝试在where和have子句中使用IN(1,t.disposition_id),但返回0条记录。我感到自己接近了,但似乎无法将它们放在一起。
答案 0 :(得分:1)
通过student s
进行汇总,然后声明您的两个要求:
patient_id