日期:2014-05-17 浏览次数:20721 次
WITH test (Cus, [Date], Caption, Price ,Memo) AS ( SELECT 'A' ,'2012-02-26' ,'K1', 18 ,'A1' union all SELECT 'A', '2012-04-15', 'K2', 19 ,'A2' union all SELECT 'B', '2012-01-13', 'K1', 18 ,'A3' union all SELECT 'B' ,'2012-02-27', 'K1', 18 ,'A4' union all SELECT 'B' ,'2012-05-06', 'K1', 18 ,'A5' union all SELECT 'B' ,'2012-05-07', 'K1', 18.5 ,'A6' union all SELECT 'B', '2012-01-13', 'K2', 20 ,'A7' union all SELECT 'B', '2012-05-16', 'K2', 19.5 ,'A8' union all SELECT 'B', '2012-05-30', 'K2', 19.5 ,'A9' union all SELECT 'B', '2012-08-15', 'K2', 19.5 ,'A10' union all SELECT 'C', '2012-01-17', 'K3',25 ,'A11' union all SELECT 'C', '2012-03-12', 'K3', 25 ,'A12' union all SELECT 'C', '2012-04-17', 'K3', 25 ,'A13' union all SELECT 'C', '2012-05-18', 'K3', 26 ,'A14' union all SELECT 'C', '2012-02-15', 'K1', 18 ,'A15' union all SELECT 'C', '2012-07-09', 'K2', 19 ,'A16' union all SELECT 'C', '2012-05-13', 'K5', 22 ,'A17' union all SELECT 'C', '2012-03-04', 'K4', 10 ,'A18' union all SELECT 'C', '2012-05-13', 'K4', 11 ,'A19' union all SELECT 'C', '2012-05-14', 'K4', 11 ,'A20' ) SELECT * FROM test a WHERE EXISTS (SELECT 1 FROM ( SELECT cus,MAX([date])[date] ,Caption FROM test GROUP BY cus,Caption) b WHERE a.cus=b.cus AND a.[date]=b.[date] AND a.Caption=b.Caption) /* Cus Date Caption Price Memo ---- ---------- ------- --------------------------------------- ---- A 2012-02-26 K1 18.0 A1 A 2012-04-15 K2 19.0 A2 B 2012-05-07 K1 18.5 A6 B 2012-08-15 K2 19.5 A10 C 2012-05-18 K3 26.0 A14 C 2012-02-15 K1 18.0 A15 C 2012-07-09 K2 19.0 A16 C 2012-05-13 K5 22.0 A17 C 2012-05-14 K4 11.0 A20 (9 行受影响) */
------解决方案--------------------
SELECT * FROM test AS t1 WHERE NOT EXISTS(SELECT 1 FROM Test AS t2 WHERE t1.cus = t2.cus AND t