日期:2014-05-18 浏览次数:20496 次
SELECT a.ID,a.PID,a.NAME FROM TB a JOIN ( SELECT min(ID) ID FROM TB GROUP BY PID ) b ONa.ID=b.ID ORDER BY a.ID --这个不?
------解决方案--------------------
select ID, PID, NAME from ( select ID, PID, NAME,row_number() over(partition by PID order by ID) rn from tb )where rn=1
------解决方案--------------------
if OBJECT_ID('tb') is not null drop table tb go create table tb (ID INT ,PID INT,NAME VARCHAR(50)) INSERT INTO tb VALUES(1,1,'小王') INSERT INTO tb VALUES(2,3,'小李') INSERT INTO tb VALUES(3,1,'小赵') select ID,PID,NAME from (select *,rn=ROW_NUMBER()over(partition BY PID order by GETDATE()) from tb)t WHERE rn=1 ID PID NAME ----------- ----------- -------------------------------------------------- 1 1 小王 2 3 小李 (2 行受影响)