我有一个表,每个日期有多个条目,例如
01-may-17
01-may-17
02-may-17
02-may-17
02-may-17
03-may-17
03-may-17
我想为每个日期返回一行以及相关列,而不是同一日期的所有多个条目。
我该怎么做? 我有
select trunc(colTime) from table
Where colTime >= ('01-jan-2017', 'DD-MON-YYYY') and colTime <=('20-mar-2017', 'DD-MON-YYYY')
但是这会返回一个日期的多个条目,而不是每天只返回一个条目。
答案 0 :(得分:3)
您可以使用ANSI标准函数row_number()
:
select t.*
from (select t.*,
row_number() over (partition by date order by date) as seqnum
from t
) t
where seqnum = 1;
您的代码看起来像Oracle。对于完整查询,我建议:
select t.*
from (select t.*,
row_number() over (partition by trunc(colTime) order by colTime) as seqnum
from table t
Where colTime >= date '2017-01-01' and coltime <= date '2017-03-20'
) t
where seqnum = 1;
注意:
date
关键字。它允许您输入ISO标准日期格式。order by
as states返回每天的第一条记录。您可以使用colTime desc
获取最后一条记录。答案 1 :(得分:0)
简单使用 GROUP BY
select * from table
Where colTime >= ('01-jan-2017', 'DD-MON-YYYY') and colTime <=('20-mar-2017', 'DD-MON-YYYY')
group by colTime;