0

我有一个看起来像的表(A):

ID     Date
1      2012/01/12
2      2012/01/01
3      2012/01/03
4      2012/03/12

如果我想获取MIN此查询的日期,我会group by吗?

select
  a.ID,
  MIN(a.DATE),
  b.name,
  c.price
FROM
  tablea a inner join tableb b on a.ID = b.ID
  inner join tablec c b.ID = c.ID
4

3 回答 3

2

你想要一个窗口函数。正确的表达是:

select a.id,
       min(a.date) over () as mindate,
       b.name, c.price
. . .

这表示要获取数据中日期的最小值。没有分区,因此它可以覆盖所有数据。

于 2013-02-22T15:45:44.370 回答
1

如果您正在寻找那些有最短日期的人,那么您可以这样做:

select
  a.ID,
  a.DATE,
  b.name,
  c.price
FROM tablea a 
INNER JOIN
(
   SELECT Id, MIN(Date) AS MinDate 
   FROM tablea
   GROUP BY Id
) As minA ON a.date = mina.mindate AND a.id = mina.id
inner join tableb b on a.ID = b.ID
inner join tablec c b.ID = c.ID
于 2013-02-22T15:45:31.767 回答
0
WITH recordList
as
(
    select  a.ID,
            a.DATE,
            b.name,
            c.price,
            DENSE_RANK() OVER (PARTITION BY a.ID
                                ORDER BY a.Date ASC) rn
    FROM    tablea a 
            inner join tableb b on a.ID = b.ID
            inner join tablec c b.ID = c.ID
)
SELECT  ID, DATE, name, Price
FROM    recordList
WHERE   rn = 1
于 2013-02-22T15:46:11.310 回答