我有一张参赛表。我想只选择过去12个月中每个月都有条目的记录。

时间:2017-05-02 20:33:32

标签: sql sql-server tsql sql-server-2012

我有一个表格,我只想从滚动日期的过去12个月中选择每个月都有条目的记录。表的格式为:Record#,Date,LocationName。

我知道我可以进行子查询以获取包含开始日期和结束日期之间日期的所有内容。但是,我需要确保记录中每个月都有一个条目,而不是仅仅检索它们之间的值。

我从

开始
select Record#, Date from Table where datediff(mm,Date,getdate()) < 12 order by Record#

对于滚动日期,我切换到:

select Record#, Date from Table where Date between start_date and end_date order by Record#

有人有任何建议吗?

1 个答案:

答案 0 :(得分:0)

使用带有exists()的{​​{1}}子句将having与聚合查询结合使用,以确保给定count(distinct ...)

存在12个月的记录
Record#

日期范围查询参考:

相关问题