DB2 中的分组依据
我有一个查询要为 DB2 中的用户获取最近的未来医生预约。
SELECT user_id,
appointment_id,
date
FROM (SELECT user_id,
appointment_id,
date
FROM doctors_appointment
WHERE date > current_timestamp
AND user_id IN (23, 24, 25)
ORDER BY date ASC ) as tmp_appointment
GROUP BY user_id
但是,在 group by 语句中包含 Appointment_id 和 date 会引发错误,这意味着它要求在 group by 中包含所有选择字段。
如果我将所有字段包含在分组中,结果将如下所示:
user_id appointment_id date
-------- ---------------- -------
23 450 2011-07-20
23 451 2011-07-26
25 452 2011-07-18
但我需要如下结果:
user_id appointment_id date
-------- -------------- -------
23 450 2011-07-20
25 452 2011-07-18
I have a query to fetch the nearest future doctor appointment for the users in DB2.
SELECT user_id,
appointment_id,
date
FROM (SELECT user_id,
appointment_id,
date
FROM doctors_appointment
WHERE date > current_timestamp
AND user_id IN (23, 24, 25)
ORDER BY date ASC ) as tmp_appointment
GROUP BY user_id
But It throws error to include appointment_id and date in group by statement which means it asks to include all select fields in group by.
If I include all fields in group by the result will be like this:
user_id appointment_id date
-------- ---------------- -------
23 450 2011-07-20
23 451 2011-07-26
25 452 2011-07-18
But I need the result like below:
user_id appointment_id date
-------- -------------- -------
23 450 2011-07-20
25 452 2011-07-18
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可以解决问题:
如果给定用户在同一天有两个约会,它将为该用户生成两行。
I think this does the trick:
It would produce two rows for a given user if said user has two appointments on the same day.