sql - 如何查找两个日期之间的重复值
问题描述
我在 Excel 电子表格中有一个大型数据集。我将数据导入到 SQL Server,日期在 2016 年 1 月 1 日到 2017 年 12 月 31 日之间。我想获取重复 2016 年和 2017 年的数据。我的数据表如下所示:
Date Type Customer
01/01/2016 Invoice 1036 Name1
01/01/2016 Invoice 1036 Name4
01/01/2016 Invoice 1036 Name5
01/09/2017 Invoice 1036 Name3
01/20/2017 Invoice 1036 Name6
01/12/2017 Invoice 1036 Name1
01/11/2017 Invoice 1036 Name7
我希望哪些客户在比较他们的日期后会重复。我真的很感激这方面的任何帮助。
解决方案
您可以使用子查询并加入
with t1 as
( select * from t where year(date)='2016'
), t2 as
(
select * from t where year(date)='2017'
) select t1.* from t1 join t2
on t1.Customer=t2.Customer and t1.type=t2.type