应该很简单的,请各位大神指教,第一次遇到这类要求,急,在线等
SELECT * FROM dbo.DP_CommonSmsEntries WHERE Instruction='JH' AND RecvTime>='2013-2-21' AND RecvTime<'2013-2-22'
执行这个语句结果是这样的:
我想要的效果是,大家注意看结果的参数,param1,这个参数的值有两个,我想把相同的去掉,只剩下1和2的,但是这个输出的1对应的那条结果是要取CommanId最大的那条,也就是如下图的红框的结果:
求解,急,在线等
SQL
select
------解决方案--------------------;with f as
(SELECT * FROM dbo.DP_CommonSmsEntries WHERE Instruction='JH' AND RecvTime>='2013-2-21' AND RecvTime<'2013-2-22')
select * from f t where CommanId=(select max(CommanId) from f where param1=t.param1)
------解决方案--------------------SELECT *
FROM dbo.DP_CommonSmsEntries AS A
WHERE Instruction='JH'
AND RecvTime>='2013-2-21'
AND RecvTime<'2013-2-22'
AND NOT EXISTS (
SELECT 1
FROM dbo.DP_CommonSmsEntries AS B
WHERE B.Instruction='JH'
AND B.RecvTime>='2013-2-21'
AND B.RecvTime<'2013-2-22'
AND B.param1 = A.param1
AND B.CommanId > A.CommanId
)