开发者

SELECTing user with earliest time and joining with other data

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 sel开发者_Python百科ected 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?


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜