我的Oracle 9i数据库中有这些行。这些行已经过了' group'列。
ticket_id | datestamp | type | group
1-7323887901 | 22-Apr-14 3:38:14 PM | create | A
1-7323889625 | 22-Apr-14 3:43:39 PM | create | A
1-7323889625 | 05-May-14 5:38:37 PM | resolve | A
1-7323889628 | 05-May-14 5:38:37 PM | create | B
1-7323887901 | 05-May-14 6:13:29 PM | resolve | A
我想创建一个查询来查找带有max created datestamp&的票证ID。具有最小解决日期戳的票证ID。
基本上我的例外输出如下。 (总是期望返回两行)
ticket_id | datestamp | type | group
1-7323889625 | 22-Apr-14 3:43:39 PM | create | A
1-7323889625 | 05-May-14 5:38:37 PM | resolve | A
THX。
[edit]添加了另一个我要排除
的群组的记录答案 0 :(得分:1)
select * from (
select *
from ticket
where type = 'create'
order by datestamp desc
) where rownum = 1
union all
select * from (
select *
from ticket
where type = 'resolved'
order by datestamp
) where rownum = 1
答案 1 :(得分:0)
你可以简单地使用两个不同的选择查询,一个是MAX()聚合函数,另一个是MIN()聚合函数,然后用UNION运算符组合两个结果。
或试试这个,
SELECT * FROM table_name
WHERE datestamp = (SELECT MAX(datestamp) FROM table_name)
OR
WHERE datestamp = (SELECT MIN(datestamp) FROM table_name);
希望这有帮助。
答案 2 :(得分:0)
您可以加入子查询。这样的事情会让你开始
select ticketid, type, mindate thedate
from ticket join
(select min(datestamp) mindate
from ticket
where type = 'create' ) t1 on ticket.datestamp = t1.datestamp
where type = 'create'
union
select ticketid, type, mindate thedate
from ticket join
(select min(datestamp) mindate
from ticket
where type = 'resolve' ) t2 on ticket.datestamp = t2.datestamp
where type = 'resolve'
如果你有联系,你必须在尝试编写代码之前决定你想要的东西。
答案 3 :(得分:0)
SELECT * FROM Tablename
WHERE (date_stamp = (SELECT MAX(date_stamp) FROM Tablename WHERE TYPE='CREATE')
OR
date_stamp = (SELECT MIN(date_stamp) FROM Tablename WHERE TYPE='RESOLVE'))
答案 4 :(得分:0)
试试这个:
select t.id from ticket t
where datestamp = (select max(datestamp) from ticket where type = 'create')
or datestamp = (select min(datestamp) from ticket where type = 'resolve')
如果您有两个完全相同的日期戳,则无效。你可以使用rownum,但这不是一个好主意。
select t.id from ticket t
where datestamp = (select max(datestamp) from ticket where type = 'create' and rownum =1)
or datestamp = (select min(datestamp) from ticket where type = 'resolve' and rownum =1)