AuditDetails
字段 id ,AuditType (类别),AssocId (关联项),AuditIndex(索引)
我要列出 类别的某个关联项的最大索引的信息
就是说想取到 id的值
我sql语句只能写到下面这个程度
select max(AuditIndex),AuditType,AssocId from AuditDetails group by AuditType,AssocId
Create table AuditDetails
(Aid int not null identity(1,1),
AuditType int null,
AssocId int null,
AuditIndex int null,
constraint PK_AuditDetails primary key (Aid)
)
insert into AuditDetails
select 1,1,3
union all select 1,1,4
union all select 2,1,4
union all select 2,2,5
union all select 3,1,4
union all select 4,1,7
union all select 5,1,5
union all select 5,1,7
union all select 3,1,8
union all select 4,1,10
select * from AuditDetails
select AID,a.AuditIndex,a.AuditType,a.AssocId from AuditDetails a
inner join
(
select max(AuditIndex)as maxAuditIndex ,AuditType,AssocId from AuditDetails
group by AuditType,AssocId
) as t on t.AuditType=a.AuditType and t.AssocId=a.AssocId and t.maxAuditIndex=a.AuditIndex
order by a.AuditType desc
可以试试, 也可以用CTE