# 求一sql的算法解决思路(2)

www.MyException.Cn  网友分享于：2013-03-24  浏览：10次

union all
select 4, 'Sam ',20,14,304, '2006-04-01 '
union all
select 5, 'Jion ',21,14,305, '2006-05-01 '
union all
select 6, 'Carry ',20,15,306, '2006-06-01 '
union all
select 7, 'Apple ',21,15,307, '2006-01-11 '
union all
select 8, 'Cenny ',23,15,308, '2007-01-01 '
union all
select 9, 'Lucy ',20,15,309, '2006-02-01 '
union all
select 10, 'Sruory ',23,15,400, '2007-01-01 '

SELECT * FROM @tbPerson

SELECT * FROM @tbPerson t1
WHERE NOT EXISTS
(SELECT 1
FROM @tbPersON t2
WHERE t2.departmentID=t1.departmentID
AND
((t2.age> t1.age) OR (t2.age=t1.age AND t2.InputDate <t1.InputDate)
OR (t2.age=t1.age AND t2.InputDate=t1.InputDate AND t2.id <t1.id)) )

(10 row(s) affected)

ID Name age departmentID ImageID InputDate
----------- -------------------- ----------- ------------ ----------- ------------------------------------------------------
1 Jim 20 13 301 2006-01-01 00:00:00.000
2 Tom 22 13 302 2006-05-01 00:00:00.000
3 Ben 22 13 303 2006-03-01 00:00:00.000
4 Sam 20 14 304 2006-04-01 00:00:00.000
5 Jion 21 14 305 2006-05-01 00:00:00.000
6 Carry 20 15 306 2006-06-01 00:00:00.000
7 Apple 21 15 307 2006-01-11 00:00:00.000
8 Cenny 23 15 308 2007-01-01 00:00:00.000
9 Lucy 20 15 309 2006-02-01 00:00:00.000
10 Sruory 23 15 400 2007-01-01 00:00:00.000

(10 row(s) affected)

ID Name age departmentID ImageID InputDate
----------- -------------------- ----------- ------------ ----------- ------------------------------------------------------
3 Ben 22 13 303 2006-03-01 00:00:00.000
5 Jion 21 14 305 2006-05-01 00:00:00.000
8 Cenny 23 15 308 2007-01-01 00:00:00.000

(3 row(s) affected)