选择最早时间的用户并与其他数据连接

发布于 2024-08-12 05:40:00 字数 1141 浏览 4 评论 0原文

我有一个 Oracle 数据库,我试图从满足某些条件的最早行(基于 time 字段)中选择一个 user 字段,但我不这样做不知道该怎么做。这是我的查询的要点:

SELECT id,
       CASE WHEN value = 'xyz'
            THEN 'Pending'
            ELSE 'Not Pending'
       END AS status,
       time
FROM table1
INNER JOIN ...
WHERE subject IN (...) AND
      field = 'Status'

我的问题是我不知道如何 SELECT user 并仅从最早 time 值与 WHERE 条件匹配的行中获取值。我认为我不能使用 HAVING 因为我没有在 SELECT 中执行任何聚合函数。 “最早时间值”条件需要应用于user字段的选择,即我想要idvaluetime 字段的所有 值选择。

我想我可以保留上面的 SELECT 语句,然后加入另一个获取我想要的特定用户的 SELECT 语句。

SELECT id, status, time, user
FROM (
       ...query above...
     ),
     (
       SELECT user
       FROM table1
       WHERE subject in (...) AND
             field = 'Status' AND
             ROWNUM = 1
       ORDER BY time ASC
     )

但是,这只能为 user 整体获取一个值,并且对于我的第一个查询中选择的每个 id 应该有一个单独的 user 值。如何通过其他查询的 id 字段限制我的 SELECT user 查询?这是否是获得我想要的东西的正确方法?

I have an Oracle database where I'm trying to select a user field from the earliest row (based on a time field) where certain conditions are met, and I don't know how to do it. Here's the gist of my query:

SELECT id,
       CASE WHEN value = 'xyz'
            THEN 'Pending'
            ELSE 'Not Pending'
       END AS status,
       time
FROM table1
INNER JOIN ...
WHERE subject IN (...) AND
      field = 'Status'

My problem is I don't know how to SELECT user and get only the value from the row with the earliest time value matching the WHERE conditions. I don't think I can use HAVING since I'm not doing any aggregate functions in my SELECT. The 'earliest time value' condition needs to apply only to the selection of the user field, i.e. I want id and value to be selected for all values of the time field.

I was thinking I could keep the SELECT statement I have above and then JOIN with another SELECT statement that gets the particular user I want.

SELECT id, status, time, user
FROM (
       ...query above...
     ),
     (
       SELECT user
       FROM table1
       WHERE subject in (...) AND
             field = 'Status' AND
             ROWNUM = 1
       ORDER BY time ASC
     )

However, this only gets one value for user overall, and there should be a separate user value for each id SELECTed in my first query. How do I limit my SELECT user query by the other query's id field? Is this even the right approach to get what I want?

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

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

发布评论

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

评论(1

明媚殇 2024-08-19 05:40:00
SELECT  id,
        CASE WHEN value = 'xyz'
             THEN 'Pending'
             ELSE 'Not Pending'
        END AS status,
        time,
        FIRST_VALUE(user) OVER (PARTITION BY id ORDER BY time) AS first_user
FROM    table1
INNER JOIN
        ...
WHERE   subject IN (...) AND
        field = 'Status'
SELECT  id,
        CASE WHEN value = 'xyz'
             THEN 'Pending'
             ELSE 'Not Pending'
        END AS status,
        time,
        FIRST_VALUE(user) OVER (PARTITION BY id ORDER BY time) AS first_user
FROM    table1
INNER JOIN
        ...
WHERE   subject IN (...) AND
        field = 'Status'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文