简体   繁体   中英

How to do I do this SQL query?

I have a set of data with fields SUBJECT, PRICE, ISBN and DATE. I want to return all results with the minimum PRICE where SUBJECT is "math" and ISBN is unique. If there the price is the same for a particular ISBN, I want the one with the earliest date.

SUBJECT PRICE ISBN DATE    
math    10    1    1-10    
math    20    2    1-11    
math    10    1    1-9

It would return math 20 2 1-11 and math 10 1 1-9.

SELECT 
A.SUBJECT,
MIN(A.PRICE) PRICE,
A.ISBN,
(
    SELECT MIN(B.DATE) 
    FROM  your_table B 
    WHERE   B.SUBJECT = 'math' 
        AND B.ISBN = A.ISBN 
        AND B.PRICE = MIN(A.PRICE) 
) Date
FROM your_table A
WHERE SUBJECT = 'math'
GROUP BY  A.SUBJECT,A.ISBN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM