求一掉SQL
表结构
ID NAME
1 ABC
2 ABC
3 ABC
4 BBB
....
最后想要的结果是
ID NAME PID PNAME
2 ABC 1 ABC
3 ABC 1 ABC
4不要
求语句
[解决办法]
DECLARE @a TABLE(ID int,NAME VARCHAR(20))
INSERT @a SELECT 1 ,'ABC'
UNION ALL SELECT 2 ,'ABC'
UNION ALL SELECT 3 ,'ABC'
UNION ALL SELECT 4 ,'BBB'
SELECT a.*,b.* FROM @a a,@a b WHERE a.id>(SELECT MIN(id) FROM @a WHERE NAME=a.name)
AND b.id=(SELECT MIN(id) FROM @a WHERE NAME=a.name)
--result
/*ID NAME ID NAME
----------- -------------------- ----------- --------------------
2 ABC 1 ABC
3 ABC 1 ABC
(所影响的行数为 2 行)
*/