我如何查询每个用户的最新loggingdate的SQL
我有一个表是用户login时的集合条目。
username, date, value -------------------------- brad, 1/2/2010, 1.1 fred, 1/3/2010, 1.0 bob, 8/4/2009, 1.5 brad, 2/2/2010, 1.2 fred, 12/2/2009, 1.3 etc..
如何创build一个查询,以便为每个用户提供最新的date?
更新:我忘了我需要有一个价值随着最新的date。
select t.username, t.date, t.value from MyTable t inner join ( select username, max(date) as MaxDate from MyTable group by username ) tm on t.username = tm.username and t.date = tm.MaxDate
SQL Server 2005和…
select * from ( select username, date, value, row_number() over(partition by username order by date desc) as rn from yourtable ) t where t.rn = 1
要获取包含用户最大date的整行:
select username, date, value from tablename where (username, date) in ( select username, max(date) as date from tablename group by username )
我看到大多数开发人员使用内联查询,而不考虑其对大数据的影响。
简而言之,您可以通过以下方式实现
SELECT a.username, a.date, a.value FROM myTable a LEFT OUTER JOIN myTable b ON a.username = b.username AND a.date < b.date WHERE b.username IS NULL ORDER BY a.datedesc;
这一个应该给你正确的结果为您编辑的问题。
子查询确保只查找最新date的行,而外部的GROUP BY
将处理关系。 当同一用户的同一date有两个条目时,它将返回最高value
条目。
SELECT t.username, t.date, MAX( t.value ) value FROM your_table t JOIN ( SELECT username, MAX( date ) date FROM your_table GROUP BY username ) x ON ( x.username = t.username AND x.date = t.date ) GROUP BY t.username, t.date
SELECT * FROM MyTable T1 WHERE date = ( SELECT max(date) FROM MyTable T2 WHERE T1.username=T2.username )
SELECT Username, date, value from MyTable mt inner join (select username, max(date) date from MyTable group by username) sub on sub.username = mt.username and sub.date = mt.date
将解决更新的问题。 即使索引良好,在大型桌面上也可能无法很好地工作。
SELECT * FROM ReportStatus c inner join ( SELECT MAX(Date) AS MaxDate FROM ReportStatus ) m on c.date = m.maxdate
对于Oracle按降序对结果集进行sorting并获取第一条logging,所以您将获得最新logging:
select * from mytable where rownum = 1 order by date desc
SELECT t1.username, t1.date, value FROM MyTable as t1 INNER JOIN (SELECT username, MAX(date) FROM MyTable GROUP BY username) as t2 ON t2.username = t1.username AND t2.date = t1.date
Select * from table1 where lastest_date=(select Max(latest_date) from table1 where user=yourUserName)
Inner Query将返回当前用户的最新date,Outer查询将根据内部查询结果提取所有数据。
我用这种方式为我桌上的每个用户logging最后的logging。 这是一个查询,以获得在PDA设备上检测到的最近时间的推销员的最后位置。
CREATE FUNCTION dbo.UsersLocation() RETURNS TABLE AS RETURN Select GS.UserID, MAX(GS.UTCDateTime) 'LastDate' From USERGPS GS where year(GS.UTCDateTime) = YEAR(GETDATE()) Group By GS.UserID GO select gs.UserID, sl.LastDate, gs.Latitude , gs.Longitude from USERGPS gs inner join USER s on gs.SalesManNo = s.SalesmanNo inner join dbo.UsersLocation() sl on gs.UserID= sl.UserID and gs.UTCDateTime = sl.LastDate order by LastDate desc
我的小编辑
- 自我
join
比嵌套select
更好 - 但是
group by
不会给你join
primary key
- 这个密钥可以通过与
first_value
( docs )
所以,这是一个查询:
select 吨。* 从 表t内连接( select不同的first_value(ID)(按GroupColumn的顺序按DateColumn desc分区)作为ID 从表 其中FilterColumn ='value' )j on t.ID = j.ID
优点:
- 使用
where
列过滤数据 - 从筛选的行中
select
任何列
缺点:
- 需要从2012年开始的MS SQL Server。
你可以使用聚合函数MAX和GROUP BY
SELECT username, MAX(date), value FROM tablename GROUP BY username, value
SELECT DISTINCT Username, Dates,value FROM TableName WHERE Dates IN (SELECT MAX(Dates) FROM TableName GROUP BY Username) Username Dates value bob 2010-02-02 1.2 brad 2010-01-02 1.1 fred 2010-01-03 1.0
SELECT * FROM TABEL1 WHERE DATE= (SELECT MAX(CREATED_DATE) FROM TABEL1)
这也应该为了获得用户的所有最新条目而工作。
SELECT username, MAX(date) as Date, value FROM MyTable GROUP BY username