数据表:db_id
字段:id char(10),code char(5)
要求是根据id排序,取code的前三个不重复值
sql:select TOP 3 code from db_id group by code order by id
报错信息:字段id不存在于聚合函数或group by条件中。求助
字段:id char(10),code char(5)
要求是根据id排序,取code的前三个不重复值
sql:select TOP 3 code from db_id group by code order by id
报错信息:字段id不存在于聚合函数或group by条件中。求助
order by id
select top 3 code from db_id where not exists(select 1 from db_id where code = db_id.code and id > db_id.id)
where not exists(select 1 from db_id where code=t.code and id<t.id)
where a.rn<=3
select top 3 code from(select distinct code from db_id ) as bb
order by id
order by id
WITH temp AS(
SELECT ROW_NUMBER()OVER(PARTITION BY code ORDER BY ID DESC) AS rowId, ID, code
FROM dbo.[db_id])
SELECT TOP(3) ID,CODE
WHERE RowID =1
ORDER BY ID DESC