求一复杂Sql语句?
Sql语句:
if exists (select * from sysobjects where id = OBJECT_ID( '[clerk] ') and OBJECTPROPERTY(id, 'IsUserTable ') = 1) DROP TABLE [clerk]
CREATE TABLE [clerk] ( [id] [int] IDENTITY (1, 1) NOT NULL , [tk_cl_id] [int] NULL , [clerk_id] [int] NULL , [cj] [int] NULL )
SET IDENTITY_INSERT [clerk] ON
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 1 , 1 , 1 , 80 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 2 , 1 , 1 , 54 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 3 , 1 , 2 , 50 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 4 , 1 , 4 , 20 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 5 , 1 , 4 , 78 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 6 , 2 , 1 , 12 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 7 , 2 , 3 , 12 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 8 , 4 , 3 , 29 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 9 , 4 , 3 , 98 )
INSERT [clerk] ( [id] , [tk_cl_id] , [clerk_id] , [cj] ) VALUES ( 10 , 4 , 3 , 70 )
SET IDENTITY_INSERT [clerk] OFF
实现为:将相同tk_cl_id,clerk_id记录,则取Cj最高的。
使之结果为:
id cj
1 80
6 12
3 50
7 12
9 98
5 78
实现了部分:select max(cj) from clerk
group by tk_cl_id,c