简体   繁体   中英

What's wrong in my query

I wonder what is wrong with this query, it return too much rows (non unique values)

SELECT tt.*
FROM Arkusz1 tt
INNER JOIN
(
    SELECT ONTSERIALNUMBER, MAX(ONTBEUP) AS MaxBEUP
    FROM Arkusz1
    GROUP BY ONTSERIALNUMBER
) groupedtt 
    ON tt.ONTSERIALNUMBER = groupedtt.ONTSERIALNUMBER AND
       tt.ONTBEUP = groupedtt.MaxBEUP

despite the fact that subquery working correctly:

SELECT ONTSERIALNUMBER,
       MAX(ONTBEUP) AS MaxBEUP
FROM Arkusz1
GROUP BY ONTSERIALNUMBER

If your query is returning records which appear to be duplicate, it simply means that more than one record can correspond to a given serial number and maximum beup value. The quick fix here is to just use SELECT DISTINCT :

SELECT DISTINCT tt.*
FROM Arkusz1 tt
INNER JOIN
(
    SELECT ONTSERIALNUMBER, MAX(ONTBEUP) AS MaxBEUP
    FROM Arkusz1
    GROUP BY ONTSERIALNUMBER
) groupedtt 
    ON tt.ONTSERIALNUMBER = groupedtt.ONTSERIALNUMBER AND
       tt.ONTBEUP = groupedtt.MaxBEUP

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