日期:2014-05-18  浏览次数:20421 次

取最新记录
SELECT   MAX(a.LogAddtime)   AS   LogAddtime,   a.LogUserID,  
            b.IpArea
FROM   ManViewLogs   a   INNER   JOIN
          ViewLogIPs   b   ON   a.IpId   =   b.IpId
GROUP   BY   a.LogUserID,   b.IpArea
ORDER   BY   MAX(a.LogAddtime)   DESC

结果如:  
LogUserID             IpArea                           LogAddtime
-1 四川省成都市电信       2007-04-02   15:14:00
36 本机地址                         2007-04-02   15:06:00
-1 北京市网通       2007-04-02   11:36:00
-1 本机地址                         2007-04-02   10:55:00
-1 北京市中国网通       2007-04-02   10:54:00
-1 广东省深圳市长城宽带   2007-04-02   10:43:00
-1 北京市电信       2007-03-30   17:19:00
-1 四川省成都市       2007-03-30   10:19:00
-1 广东省深圳市       2007-03-30   00:15:00
55 北京市中国网通       2007-03-30   00:15:00
555 北京市中国网通       2007-03-30   00:15:00
5555 北京市中国网通       2007-03-29   13:48:00

我只想根据LogAddtime取LogUserID最新的一条记录   该怎么取?
要的结果是
LogUserID             IpArea                           LogAddtime
-1 四川省成都市电信       2007-04-02   15:14:00
36 本机地址                         2007-04-02   15:06:00
55 北京市中国网通       2007-03-30   00:15:00
555 北京市中国网通       2007-03-30   00:15:00
5555 北京市中国网通       2007-03-29   13:48:00

分不多,还望见谅


------解决方案--------------------
SELECT MAX(a.LogAddtime) AS LogAddtime, a.LogUserID,b.IpArea
FROM ManViewLogs a INNER JOIN
ViewLogIPs b ON a.IpId = b.IpId
inner join (select LogUserID,MAX(LogAddtime) AS LogAddtime FROM ManViewLogs group by LogUserID) c
on a.LogUserID=c.LogUserID and a.LogAddtime=c.LogAddtime
GROUP BY a.LogUserID, b.IpArea
ORDER BY MAX(a.LogAddtime) DESC
------解决方案--------------------
---try
SELECT id identity(int,1,1),MAX(a.LogAddtime) AS LogAddtime, a.LogUserID,
b.IpArea into #tmp
FROM ManViewLogs a INNER JOIN
ViewLogIPs b ON a.IpId = b.IpId
GROUP BY a.LogUserID, b.IpArea

select * from #tmp
where a.id=(select top 1 id from #tmp where LogUserID=a.LogUserID order by LogAddtime desc )