SQL日期编号

时间:2013-06-05 21:54:15

标签: sql

我想做以下事情:

Select 
username,
(select min(sub.date) from data sub where sub.username = outer.username) as firstDate,
date,
? as dateSequence
from data as outer;

我不知道为dateSequence放什么。此值应表示“日期”值在客户在数据库中的所有日期的顺序中的位置(因此firstDate的值将为1等)。

我有什么方法可以做到这一点?客户错过了日期,每个日期有多个条目。 提前谢谢。

1 个答案:

答案 0 :(得分:4)

Select username,
       select min(date) over (partition by username) as firstDate,
       date,
       row_number() over (partition by username order by date) as date_sequence
from data as outer;