选择最早时间的用户并与其他数据连接
我有一个 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
字段的选择,即我想要id
和value
为time
字段的所有 值选择。
我想我可以保留上面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)