好吧,如果它是 MS SQL 服务器,我可以建议使用row_number over()
但是在你的情况下,我可以建议试试这个(我想你的表中有一些 id)
select
    A.sub,
    case when A.frst = 1 then A.user else null end as user,
    case when A.frst = 1 then A.versionid else null end as versionid,
    case when A.frst = 1 then A.hard else null end as hard,
    case when A.frst = 1 then A.active else null end as active,
    case when A.frst = 1 then A.res else null end as res
from
(
    select
        *,
        case
            when exists
            (
                select * 
                from buy as tt
                where
                    tt.sub = t.sub and tt.res = t.res and
                    tt.user = t.user and tt.versionid = t.versionid and
                    tt.hard = t.hard and tt.active = t.active and
                    tt.id < t.id
            ) then 0
            else 1
        end as frst
    from buy as t
) as A
order by A.sub, A.frst desc
SQL 小提琴