日期:2014-05-18  浏览次数:20498 次

SQL语句怎样去除重复、并且保留XX较小的一条数据?(在线等)
表数据结构如下: 

cardId price name
-------------------------------------------
 001 0.9 aaa
 002 1.5 bbb
 001 0.5 ccc
 003 1.2 ddd 


SQL语句怎样去除重复(cardId)并且保留price较小的一条数据?


------解决方案--------------------
SQL code
select * from tb t where price=(select min(price) from tb where cardid=t.cardid)

------解决方案--------------------
SQL code

DELETE  a
FROM    你的表名 a
WHERE   EXISTS ( SELECT TOP 1
                        *
                 FROM   你的表名
                 WHERE  cardId = a.cardId
                        AND price > a.price )

------解决方案--------------------
SQL code

create table dashanliu
(cardId char(5), price decimal(3,1), name char(5))

insert into dashanliu
select '001', 0.9, 'aaa' union all
select '002', 1.5, 'bbb' union all
select '001', 0.5, 'ccc' union all
select '003', 1.2, 'ddd'


select cardId,price,name
from
(select row_number() over(partition by cardId order by price) rn,
cardId,price,name from dashanliu) t
where t.rn=1

cardId price                                   name
------ --------------------------------------- -----
001    0.5                                     ccc  
002    1.5                                     bbb  
003    1.2                                     ddd