【发布时间】:2016-09-29 00:10:08
【问题描述】:
我正在尝试编写一个 SQL 查询,它只返回在同一日期有多个交易的员工(count(TransactionDate) > 1),但交易发生在不同的商店 ID 中。我正在尝试使用计数聚合和拥有的组合,但似乎无法返回正确的值。临时表会是更好的方法,还是子查询?我的以下查询未返回准确的记录。任何帮助表示赞赏。谢谢!
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,...?它有所不同,因为这是窗口函数的完美示例
标签: sql count having-clause