尝试每天选择一行表格的想法如下:
ID|PageID|DateTime |Value|SiteID
1 | 1 |2021/03/06| 2001| 1
1 | 1 |2021/03/06| 2002| 1
1 | 2 |2021/03/06| 2003| 1
1 | 3 |2021/03/06| 2004| 1
1 | 4 |2021/03/06| 2004| 1
1 | 1 |2021/03/07| 2004| 1
1 | 2 |2021/03/07| 2005| 1
1 | 3 |2021/03/07| 2006| 1
所以该站点有多个带有单独链接的链接,但是 PageID 在这里有点多余我正在尝试按 SiteID 分组并为按 SiteID 分组的每一天检索一行 该值并不重要,只要每天一次即可。
尝试:
SELECT [DateTime], [Followers], [SiteID]
FROM test
WHERE datetime IN (
SELECT MAX(datetime)
FROM test Where SiteID = 1
GROUP BY datetime
);
答案 0 :(得分:1)
直到您提供所需的输出,这是一种方法:
select *
from (select * , row_number() over (partition by SiteId,Date) rn from table) tt
where tt.rn = 1
答案 1 :(得分:1)
您可以使用窗口函数。但如果值为 datetime
,则需要提取日期:
select *
from (select t.* ,
row_number() over (partition by SiteId, convert(date, datetime) order by (select null)) as seqnum
from t
) t
where seqnum = 1;
编辑:
如果性能是一个问题,那么相关子查询可能具有更好的性能:
select t.*
from t
where t.datetime = (select min(t2.datetime)
from t t2
where t2.siteId = t.siteId and
t2.date >= convert(date, t.date) and
t2.date < dateadd(day, 1, convert(date, t2.date))
);
注意:这里假设 datetime
是唯一的。您可以使用任何唯一的列进行比较。
答案 2 :(得分:0)
它可能对您有所帮助:
select top (1) with ties t.*
from table t
order by Row_Number() Over(Partition By SiteId,convert(date, datetime) Order by (select null))