开发者

MySQL subquery in same table

I have one table that collect payment from client.I want to get from top 3 client开发者_如何转开发 that make payment for each client.

The query below gets total payment for each client for each hour in a day.

SELECT
    hour(received) as hr,
    login,
    count(*) as total 
FROM
    payment
WHERE
    received >= date("2011-02-24")
AND
    received <date("2011-02-25")  
AND
    PAYMENT_BATCH_FILE_ID is null
GROUP BY
    hr,
    client_id 
ORDER BY
    hr,
    total DESC

The query below get top 3 client for each client in a hour

SELECT
    hour(received) as hr,
    login,
    count(*) as total 
FROM 
    payment
WHERE 
    received >=date("2011-02-24") 
AND 
    received < date("2011-02-25")  
AND 
    PAYMENT_BATCH_FILE_ID is null 
AND
    hour(received)="3"
GROUP BY
    hr,
    client_id 
ORDER BY
    hr,
    total DESC
LIMIT 3

How to combine the both queries to get top 3 client that make payment for each hour?

Thanks.


This is a complicated problem. refer to this link http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

Good luck !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜