I have a table with only 3 columns that I am trying to select only the most recent/newer date:
account_id - event_time - sign 2251 2017-03-28 19:00:04 YES 2250 2017-03-28 15:45:11 NO 2250 2017-03-28 01:01:45 NO 2249 2017-03-24 21:00:03 YES 2248 2017-03-24 05:45:10 NO 2247 2017-03-19 21:00:05 YES 2246 2017-03-19 05:45:10 NO 2245 2017-03-22 21:15:05 YESI am trying to get back these values because they are the newest values in the table:
2251 2017-03-28 19:00:04 YES 2250 2017-03-28 15:45:11 NO 2250 2017-03-28 01:01:45 NOI tried:
SELECT account_id, max(event_time) as event_time, sign FROM mytable group by account_id,event_time, sign order by event_time descBut its bring all the records instead. Has anyone done anything like that? Thanks for looking!
Using top with ties to return all records for the latest date:
select top 1 with ties account_id , event_time , sign from mytable order by convert(date,event_time) descrextester demo: http://rextester.com/OBPT49409
returns:
+------------+---------------------+------+ | account_id | event_time | sign | +------------+---------------------+------+ | 2251 | 2017-03-28 19:00:04 | YES | | 2250 | 2017-03-28 15:45:11 | NO | | 2250 | 2017-03-28 01:01:45 | NO | +------------+---------------------+------+For all records for the top 3 dates:
select t.account_id , t.event_time , t.sign from mytable t inner join ( select distinct top 3 convert(date,event_time) as event_time from mytable order by event_time desc ) topthree on convert(date,t.event_time) = topthree.event_timereturns:
+------------+---------------------+------+ | account_id | event_time | sign | +------------+---------------------+------+ | 2251 | 2017-03-28 19:00:04 | YES | | 2250 | 2017-03-28 15:45:11 | NO | | 2250 | 2017-03-28 01:01:45 | NO | | 2249 | 2017-03-24 21:00:03 | YES | | 2248 | 2017-03-24 05:45:10 | NO | | 2245 | 2017-03-22 21:15:05 | YES | +------------+---------------------+------+