<p>假設我有一個帶有DateTime列的表,名為purchase_time,還有其他訂單細節(store_id、buyer_id、item_id、value),我想找到買家在他們第一次購買時最常購買的商品名稱。 </p>
<p>到目前為止,我已經做到了這一點,但如何找到最受歡迎的商品? </p>
<pre class="brush:php;toolbar:false;">select store_id, from transactions
where purchase_time in (select min(purchase_time)
from transactions c1 group by c1.store_id);</pre></p>
SELECT TOP 1 i.item_name
FROM Transactions t
INNER JOIN Items i ON i.item_id = t.item_id
WHERE purchase_time = (select *, MIN(purchase_time) FROM Transactions)
GROUP BY 1
ORDER BY COUNT(*) DESC