日期:2014-05-18 浏览次数:20632 次
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([SMT_id] INT,[dyid] INT,[SMT_data] DATETIME) INSERT [tb] SELECT 1,9,'2012-4-12 14:33:36' UNION ALL SELECT 2,11,'2012-4-18 19:23:36' UNION ALL SELECT 3,9,'2012-4-12 14:33:36' UNION ALL SELECT 4,9,'2012-4-12 14:33:36' UNION ALL SELECT 5,12,'2012-5-19 19:23:36' UNION ALL SELECT 6,11,'2012-3-17 19:23:36' --------------开始查询-------------------------- --1 SELECT * FROM [tb] t WHERE NOT EXISTS(SELECT 1 FROM [tb] WHERE [SMT_data]=t.[SMT_data] AND [SMT_id]>t.[SMT_id]) AND NOT EXISTS(SELECT 1 FROM [tb] WHERE [dyid]=t.[dyid] AND [SMT_data]>t.[SMT_data]) --2 SELECT * FROM [tb] t WHERE [SMT_id]=(SELECT MAX([SMT_id]) FROM [tb] WHERE [SMT_data]=t.[SMT_data]) AND [SMT_data]=(SELECT MAX([SMT_data]) FROM [tb] WHERE [dyid]=t.[dyid]) ----------------结果---------------------------- /* SMT_id dyid SMT_data ----------- ----------- ----------------------- 2 11 2012-04-18 19:23:36.000 4 9 2012-04-12 14:33:36.000 5 12 2012-05-19 19:23:36.000 (3 行受影响) */
------解决方案--------------------