我正在尝试编写一个SQL查询,该查询仅返回在同一日期(count(TransactionDate)> 1)具有多个事务的雇员,但事务发生在不同的商店ID内。我试图使用计数聚合和有,但似乎无法返回正确的值的组合。临时表是一个更好的方法来做到这一点,或者一个子查询?我下面的查询没有返回准确的记录。任何帮助表示赞赏。谢谢!具有多个条件的SQL计数聚合
EmployeeID | StoreID | TransactionDate
--------------------------------------
1 | 1 | 2016-09-09 --should be returned
--------------------------------------
1 | 2 | 2016-09-09 --should be returned
--------------------------------------
1 | 3 | 2016-09-09 --should be returned
--------------------------------------
1 | 1 | 2016-09-18 --should not be returned
--------------------------------------
2 | 1 | 2016-09-09 --should not be returned
--------------------------------------
2 | 1 | 2016-09-09 --should not be returned
--------------------------------------
3 | 1 | 2016-09-09 --should not be returned
--------------------------------------
4 | 5 | 2016-09-09 --should be returned
--------------------------------------
4 | 6 | 2016-09-09 --should be returned
select top 1000 EmployeeID, StoreID, TransactionDate, count(StoreID)[StoreCount], count(TransactionDate)[Transaction Count]
from myTable
group by EmployeeID, StoreID, TransactionDate
having count(StoreID) > 1 and count(TransactionDate) > 1
order by TransactionDate desc
其中rdbms? sql-sever,mysql,oracle,...?它有所作为,因为这是窗口功能的完美例子 – Matt