DB2 中的分组依据

发布于 2024-11-19 22:05:05 字数 973 浏览 5 评论 0原文

我有一个查询要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭了丶谁疼 2024-11-26 22:05:05

我认为这可以解决问题:

  SELECT D.User_ID, D.Appointment_ID, D.Date 
    FROM Doctors_Appointment AS D
    JOIN (SELECT User_ID, MIN(Date) AS Apt_Date
            FROM Doctors_Appointment 
           WHERE Date > current_timestamp 
             AND User_ID IN (23, 24, 25)
           GROUP BY User_ID) AS T 
      ON T.User_ID = D.User_ID AND T.Apt_Date = D.Date;

如果给定用户在同一天有两个约会,它将为该用户生成两行。

I think this does the trick:

  SELECT D.User_ID, D.Appointment_ID, D.Date 
    FROM Doctors_Appointment AS D
    JOIN (SELECT User_ID, MIN(Date) AS Apt_Date
            FROM Doctors_Appointment 
           WHERE Date > current_timestamp 
             AND User_ID IN (23, 24, 25)
           GROUP BY User_ID) AS T 
      ON T.User_ID = D.User_ID AND T.Apt_Date = D.Date;

It would produce two rows for a given user if said user has two appointments on the same day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文