首页 新闻 会员 周边

求一个 sql语句

0
悬赏园豆:50 [已解决问题] 解决于 2010-01-25 13:19

AuditDetails

字段 id ,AuditType (类别),AssocId (关联项),AuditIndex(索引)

我要列出 类别的某个关联项的最大索引的信息

 就是说想取到 id的值

我sql语句只能写到下面这个程度

select max(AuditIndex),AuditType,AssocId from AuditDetails group by  AuditType,AssocId

 

小眼睛老鼠的主页 小眼睛老鼠 | 老鸟四级 | 园豆:2731
提问于:2010-01-25 11:19
< >
分享
最佳答案
0

代码
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

 


AID    AuditIndex    AuditType    AssocId
8    7    5    1
10    10    4    1
9    8    3    1
4    5    2    2
3    4    2    1
2    4    1    1

可以试试, 也可以用CTE

收获园豆:50
邀月 | 高人七级 |园豆:25475 | 2010-01-25 12:59
谢了 哥们 :)
小眼睛老鼠 | 园豆:2731 (老鸟四级) | 2010-01-25 13:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册