连接表上的最大日期问题

发布于 2024-10-22 16:20:49 字数 1729 浏览 1 评论 0原文

例如,使用日期:03-16-2011 作为今天的日期

表记录:record_id、record_status、record_subscribed_date

表 record_change_log:change_log_id、record_id、new_record_status、record_change_date、old_record_status

(将只有一个 record_id) 表记录数据:(

1, key, 2011-03-14 08:24:12.04051-04
2, manual, 2011-03-15 09:56:32.04051-04
3, entered, 2011-03-16 07:44:01.04051-04

将有多个record_id和record_change_date) 表 record_change_log 数据:

100, 3, entered, 2011-03-16 09:54:01.04051-04, ''
101, 3, updated, 2011-03-16 09:55:01.04051-04, entered
102, 2, manual, 2011-03-15 09:56:32.04051-04, ''
103, 3, valid, 2011-03-16 10:00:01.04051-04, updated
104, 2, updated, 2011-03-15 10:01:51.04051-04, manual
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
107, 23, manual, ...
108, 100, test, ...

我需要的是每天返回所有 record_id 以及最后输入的更改日志。 因此,record_id 1 将返回:

106, 1, updated, 2011-03-16 11:11:11.04051-04, key

并且 record_id 2 不会返回,因为日期不是今天:

104, 2, updated, 2011-03-15 10:01:51.04051-04, manual

并且 record_id 3 将返回:

105, 3, returned, 2011-03-16 10:11:22.04051-04, valid

但在查询中,我只需要返回的 record_id 和 new_record_status 已随今天的日期更改,因此 record_id 2 将不会在结果中返回,但 1 和 3 会返回。

现在查询的另一部分是我只需要记录表中的 record_id,因此更改日志表中的其他记录也不会返回:

107, 23, manual, ...
108, 100, test, ...

因此,所需的结果是这些是更改日志中的最后一个条目表以及它们是记录表中的记录:

105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key

我尝试连接表,但我似乎无法限制更改日志表中的结果。我可以限制更改日志表中的结果,但无法选择我需要的所有记录,这会导致 GROUP BY 抱怨某些事情

For the example use date: 03-16-2011 as today's date

table record: record_id, record_status, record_submitted_date

table record_change_log: change_log_id, record_id, new_record_status, record_change_date, old_record_status

(Will only have one record_id)
table record data:

1, key, 2011-03-14 08:24:12.04051-04
2, manual, 2011-03-15 09:56:32.04051-04
3, entered, 2011-03-16 07:44:01.04051-04

(Will have multiple record_id's and record_change_date's)
table record_change_log data:

100, 3, entered, 2011-03-16 09:54:01.04051-04, ''
101, 3, updated, 2011-03-16 09:55:01.04051-04, entered
102, 2, manual, 2011-03-15 09:56:32.04051-04, ''
103, 3, valid, 2011-03-16 10:00:01.04051-04, updated
104, 2, updated, 2011-03-15 10:01:51.04051-04, manual
105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key
107, 23, manual, ...
108, 100, test, ...

What I need is to return all record_id's daily with the last entered change log.
So record_id 1 would return:

106, 1, updated, 2011-03-16 11:11:11.04051-04, key

and record_id 2 would not be return as the date is not today:

104, 2, updated, 2011-03-15 10:01:51.04051-04, manual

and record_id 3 would return:

105, 3, returned, 2011-03-16 10:11:22.04051-04, valid

But in the query I only need the record_id and the new_record_status returned that have changed with today's date, so record_id 2 would not be returned in the results but 1 and 3 would.

Now the other part of the query is I only need record_id's that are in the records table, so these other records in the change log table would not get returned as well:

107, 23, manual, ...
108, 100, test, ...

So the desired results are as these are the last entries in the change log table as well as they are records in the record table:

105, 3, returned, 2011-03-16 10:11:22.04051-04, valid
106, 1, updated, 2011-03-16 11:11:11.04051-04, key

I have tried to join the tables but I can't seem to limit the results in the change log table. And I can limit the results in the change log table but not able to select all the records I need would out the GROUP BY complaining about something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

往昔成烟 2024-10-29 16:20:49

您可以加入相关的子查询,例如...

SELECT
  *
FROM
  record
INNER JOIN
  record_change_log
    ON record_change_log.change_log_id = (
      SELECT
        change_log_id
      FROM
        record_change_log
      WHERE
        record_id = record.record_id
      ORDER BY
        record_change_date DESC
      LIMIT
        1
    )

(我将“仅限今天”部分留给您,因为我认为“更改日志中只有一条记录”是您需要帮助的部分。 )

You could join on a correlated sub_query, comething such as...

SELECT
  *
FROM
  record
INNER JOIN
  record_change_log
    ON record_change_log.change_log_id = (
      SELECT
        change_log_id
      FROM
        record_change_log
      WHERE
        record_id = record.record_id
      ORDER BY
        record_change_date DESC
      LIMIT
        1
    )

(I've left the "today only" parts up to you, as I think the "just one record from the change log" is the part you wanted help on.)

<逆流佳人身旁 2024-10-29 16:20:49
--
-- Pull today's most recent change log entries for every known "record_id",
-- if any.
--
SELECT *
  FROM record_change_log
 INNER JOIN (
             -- Find the timestamp of the most recent change log entry
             -- for each known "record_id" (in the "record" table),
             -- which change occurred today.
             SELECT record_id,
                    max(record_change_date) as changed
               FROM record_change_log
              INNER JOIN
                    record USING (record_id)
              WHERE record_change_date::DATE = CURRENT_DATE
              GROUP BY 1
            ) most_recent ON most_recent.record_id = record_change_log.record_id
                             AND
                             record_change_date = most_recent.changed;
--
-- Pull today's most recent change log entries for every known "record_id",
-- if any.
--
SELECT *
  FROM record_change_log
 INNER JOIN (
             -- Find the timestamp of the most recent change log entry
             -- for each known "record_id" (in the "record" table),
             -- which change occurred today.
             SELECT record_id,
                    max(record_change_date) as changed
               FROM record_change_log
              INNER JOIN
                    record USING (record_id)
              WHERE record_change_date::DATE = CURRENT_DATE
              GROUP BY 1
            ) most_recent ON most_recent.record_id = record_change_log.record_id
                             AND
                             record_change_date = most_recent.changed;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文