2013-10-02 105 views
0

我的表有3個字段,我想獲得最重複的ticket_id我有裏面,這個查詢工作,但我需要添加一個where子句的「is_winner」,任何幫助?感謝Sqlite查詢最大和where子句

ID TICKET_ID IS_WINNER 
1 2   1 
2 3   1 
3 2   1 
4 3   0 
5 2   1 
6 2   0 
7 3   0 
8 2   1 
9 2   0 


select query1.* FROM (Select ticket_id, Count(*) As order_count 
FROM ticket_log 
GROUP BY ticket_log.ticket_id) query1,(Select Max(query2.order_count) As highest_count 
FROM (Select ticket_id, Count(*) As order_count 
FROM ticket_log 
GROUP BY ticket_log.ticket_id) query2) query3 where query1.order_count = query3.highest_count 
+0

你怎麼裝?贏家一排?獲取優勝者門票的列表? –

回答

1

只需添加WHERE子句:

SELECT query1.* FROM (Select ticket_id, Count(*) As order_count 
FROM ticket_log WHERE is_winner = 1 
GROUP BY ticket_log.ticket_id) query1,(Select Max(query2.order_count) As highest_count 
FROM (Select ticket_id, Count(*) As order_count 
FROM ticket_log WHERE is_winner = 1 
GROUP BY ticket_log.ticket_id) query2) query3 where query1.order_count = query3.highest_count 

(編輯,因爲我注意到你需要所有名)

+0

以前試過你的解決方案,但我錯過了第二個where子句。它的工作原理非常感謝。 – im7xs

1
SELECT ticket_id, COUNT() AS wins 
FROM ticket_log 
WHERE is_winner=1 
GROUP BY ticket_id 
ORDER BY wins DESC; 

是這樣嗎?