简体   繁体   English

GROUP BY订购

[英]GROUP BY with an ordering

SELECT * FROM title gives me the following output: SELECT * FROM title给出了以下输出:

title_number    version_type    hd_sd
1000046         Standard    SD
1000046         Standard    HD
1000050         Standard    SD
1000093         Standard    SD
1000093         Standard    HD
1000125         Standard    SD
1000269         Standard    SD

I need to get all distinct title_numbers and then hd_sd will = HD if that exists, otherwise, it will = SD . 我需要得到所有不同的title_numbers,然后hd_sd将= HD如果存在,否则,它将= SD The correct output would be: 正确的输出是:

title_number    version_type    hd_sd
1000046         Standard    HD
1000050         Standard    SD
1000093         Standard    HD
1000125         Standard    SD
1000269         Standard    SD

The closest I got to this was SELECT * FROM title GROUP BY title_number , but that does not give me the correct hd_sd column value. 我得到的最接近的是SELECT * FROM title GROUP BY title_number ,但这并没有给我正确的hd_sd列值。 How would I correctly do this query? 我如何正确地执行此查询?

SELECT title_NUMBer, Version_type,
        MIN(hd_sd)
FROM tableName
GROUP BY title_NUMBer

for improvements, 为了改善,

SELECT a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT title_NUMBer, MIN(hd_sd) minCol
            FROM tableName
            GROUP BY title_NUMBer
        ) b ON a.title_number = b.title_Number AND
                a.hd_sd = b.mincol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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