日期:2014-05-17 浏览次数:20555 次
SELECT * FROM product WHERE productid IN (
SELECT MAX(productid) FROM product GROUP BY dealerid
)
if object_id('[product]') is not null drop table [product]
go
create table [product] (productid int,dealerid int,pic nvarchar(8),productname nvarchar(2))
insert into [product]
select 1,1,'url1','a' union all
select 2,1,'url2','b' union all
select 3,2,'url3','c'
select * from [product]
SELECT *
FROM product P
WHERE NOT EXISTS ( SELECT 1
FROM product M
WHERE P.dealerid = M.dealerid
AND P.pic > M.pic ) --选最大或最小,lz可以从这里调整
/*
productid dealerid pic productname
----------- ----------- -------- -----------
1 1 url1 a
3 2 url3 c
(2 行受影响)
*/
;
WITH huang
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY dealerid ORDER BY productid ) pid ,