谢谢,在线等,有额外赠品啊。。。 ------最佳解决方案-------------------- 修改下:
select product_id,yh
from (
select product_id,yh,row_number() over(order by product_id) rowid from TB group by product_id,yh) as a
where rowid<3 ------其他解决方案--------------------
假如每个PRODUCT_ID 有三个呢,这样写就不好办了,我感觉,得先去一下重,然后再选两条数据
SELECT?DISTINCT?product_id,yh into #temp
FROM?TB
SELECT *
FROM (SELECT *,
Row_number()OVER (partition BY product_id ORDER BY yh ) AS id
FROM #temp) a
WHERE id <= 1
------其他解决方案-------------------- 刚才的只是对你的数据来说而已
SELECT product_id ,
yh
FROM ( SELECT product_id ,
yh ,
row_number() OVER (PARTITION BY product_id ORDER BY product_id ) rowid
FROM TB
GROUP BY product_id ,
yh
) AS a
WHERE rowid <=2
------其他解决方案--------------------
if not object_id('tempdb..#Testtb') is null
begin
drop table #Testtb
end
create table #Testtb
(
Product_id nvarchar(36),