我有兩個表:Customer和ParkingTransaction。我想向最常使用該地段的前10名顧客展示。 ParkingTrasaction表中的'CustomerKey'是將ParkingTransaction連接到客戶的FK。我寫了下面的代碼計算在ParkingTransaction表中最常用的CustomerKey,它工作正常...在表中重複計算名稱
SELECT TOP 10 CustomerKey
, count(*) as 'Usage'
FROM ParkingTransaction
GROUP BY CustomerKey
HAVING (count(CustomerKey) > 0)
ORDER BY 'Usage' DESC
這是我的輸出
我現在面臨的問題是這個:我想從Customer表中拉出FirstName和LastName字段,而不是僅僅通過CustomerKey進行排序。我已經搞亂了JOINS,但還沒有提出解決方案。
謝謝!
'HAVING(計數(CustomerKey)> 0)'是多餘的。您通過'CustomerKey'進行分組,因此每個組將至少包含1行。它不能是0或更少。 –
MySQL什麼時候開始支持SQL Server風格的'SELECT TOP n ...'? (哎呀,這是個問題,而不是評論。) – spencer7593