本文介绍了在sql Server-2005中查找Pramary ID中的最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨......我在每张身份证记录中都找到了最后一条记录。 Hi....I have Find last record in each ID records.IDSIDAmountPayDue111000020008000221000030007000331000050005000411000030005000521000050002000631000020003000711000010004000821000010001000 例如:Out Put。 For Eg:Out Put.SID Due1 40002 10003 3000 推荐答案 SELECT t1.SID, t1.DUEFROM YourTable AS t1 RIGHT JOIN ( SELECT SID, MAX(ID) AS ID FROM YourTable GROUP BY SID) AS t2 ON t1.SID =t2.SID AND t1.ID=t2.IDORDER BY t1.SID DECLARE @AmtDtls TABLE(ID INT,SID INT, Amount NUMERIC(9,0), Pay NUMERIC(9,0), Due NUMERIC(9,0))INSERT INTO @AmtDtls (ID, SID, Amount, Pay , Due )SELECT 1, 1, 10000, 2000, 8000UNION ALLSELECT 2, 2, 10000, 3000, 7000UNION ALL SELECT 3, 3, 10000, 5000, 5000UNION ALLSELECT 4, 1, 10000, 3000, 5000UNION ALLSELECT 5, 2, 10000, 5000, 2000UNION ALLSELECT 6, 3, 10000, 2000, 3000UNION ALLSELECT 7, 1, 10000, 1000, 4000UNION ALLSELECT 8, 2, 10000, 1000, 1000SELECT A.SID, A.Due FROM @AmtDtls AINNER JOIN (SELECT MAX(ID) ID, SID FROM @AmtDtls GROUP BY SID) T ON T.ID=A.IDORDER BY A.SID 问候, GVPrabu Regards,GVPrabu 这篇关于在sql Server-2005中查找Pramary ID中的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 21:18