获取满足条件的列的ID时分组

时间:2013-09-04 06:49:51

标签: mysql sql sql-server oracle oracle11g

我想写这样的东西

SELECT MIN(date),
       id 
   FROM test 
  WHERE t.SITE_ID=SITE_id 
  GROUP BY SITE_ID

但是在逐个电话之后无法获得id

然后我想出了这个

SELECT t.id 
  FROM test t 
 WHERE t.date IN(SELECT MIN(date) 
                   FROM test 
                  WHERE t.SITE_ID=SITE_id 
                  GROUP BY SITE_ID)

这个选择应该让我获得与每个站点的最小日期具有相同日期的测试行的id,以便更简单?

2 个答案:

答案 0 :(得分:1)

尝试

SELECT t1.id 
FROM test t1
inner join
(
   select site_id, min(date) as mdate
   from test
   GROUP BY site_id
) t2 on t1.site_id = t2.site_id and t1.date = t2.mdate

答案 1 :(得分:-2)

SELECT t1.id 
FROM test t1
inner join
(
   select site_id, min(date) as mdate
   from test
   GROUP BY site_id, id
) t2 on t1.site_id = t2.site_id and t1.date = t2.mdate