例如,两个连接表:
ID Name Date
1 a 4-1-2010
1 z 3-3-1999
1 c 1-1-2012
2 e 7-9-2005
2 r 5-1-2000
我想查询表格:
ID Name Date MinDate
1 a 4-1-2010 3-3-1999
1 z 3-3-1999 3-3-1999
1 c 1-1-2012 3-3-1999
2 e 7-9-2005 5-1-2000
2 r 5-1-2000 5-1-2000
这可能吗?
My query is as following:
Select *, Min(Date) AS MinDate
From table1
Join table1 on table1.ID = table2.ID
Group By ID
答案 0 :(得分:0)
如果没有您的表格结构,这有点难以理解,但您可以使用min
在不使用window functions
的情况下获得每个组的group by
日期。以下是使用min() over
的示例:
select id, name, date,
min(date) over (partition by id) mindate
from table1
Join table2 on table1.ID = table2.ID
答案 1 :(得分:0)
这是一个有效的。执行子查询(sub)以按ID查找所有最小日期值。然后将这些最小值连接到主表(T)中的相应ID:
SELECT T.id, T.name, T.date, sub.mindate
FROM T
LEFT JOIN
(SELECT id, min(date) AS mindate
FROM T
GROUP BY id) sub
ON T.id = sub.id