Get Latest Record of the day for a user in SQL

时间:2018-12-27 13:02:30

标签: sql sql-server

How to get latest record of the day for a user if there is multiple entries of same user in table of the same per day.

For-example:

Id      UserName               CreatedOn
1       B               2018-11-20
2       A               2018-12-20
3       A               2018-11-19
4       B               2018-11-18

I want result For User A,B Like:

Id      UserName               CreatedOn
2       A               2018-12-20
1       B               2018-11-20

9 个答案:

答案 0 :(得分:3)

Create Table #tbl
(
Id Int,
UserName VarChar(25),
CreatedOn Date
)

Insert Into #tbl Values
(1,'B','2018-11-20'),
(2,'A','2018-12-20'),
(3,'A','2018-11-19'),
(4,'B','2018-11-18')


Select Username, Max(CreatedOn) As LastDT from #tbl
Group By UserName

结果:

Username    LastDT
A           2018-12-20
B           2018-11-20

更新:如果按日分组,则可以使用:在[DateTime]中使用年份选择

Create Table #tbl
(
Id Int,
UserName VarChar(25),
CreatedOn DateTime
)

Insert Into #tbl Values
(1,'B','2018-11-20 10:45:12.000'),
(2,'A','2018-12-20 07:45:12.000'),
(3,'A','2018-11-19 09:45:12.000'),
(4,'B','2018-11-18 11:45:12.000'),
(5,'B','2018-11-20 01:50:12.000')

 Select  Username, Max(CreatedOn) As LastDT from #tbl
Where DatePart(year,CreatedOn) = '2018'
Group By UserName, DatePart(dayofyear ,CreatedOn)
Order By UserName

每天的结果:

Username    LastDT
A           2018-11-19 09:45:12.000
A           2018-12-20 07:45:12.000
B           2018-11-18 11:45:12.000
B           2018-11-20 10:45:12.000

答案 1 :(得分:1)

我想您只是想要每个用户的最新记录

select  *
from    (
            select  *, rn = row_number() over (partition by UserName order by CreatedOn desc)
            from    yourtable
        ) d
where   d.rn = 1
order by CreatedOn desc

答案 2 :(得分:1)

您可以像下面的sql语句一样使用dense_rank

  with t(Id, UserName, CreatedOn) as
  (
   select 1,'B',date'2018-11-20' from dual union all
   select 2,'A',date'2018-12-20' from dual union all
   select 3,'A',date'2018-11-19' from dual union all
   select 4,'B',date'2018-11-18' from dual
  )
  select Id, UserName, CreatedOn
    from
    (
    select *,
           dense_rank() over ( partition by username order by CreatedOn desc) as
           max_CreatedOn 
      from t 
    ) q
   where max_CreatedOn = 1;

Id  UserName    CreatedOn
--  --------    ----------
2   A           2018-12-20
1   B           2018-11-20

Rextester Demo

答案 3 :(得分:0)

如果您想要每个用户的最新记录,则应该可以:

SELECT UserName, MAX(CreatedOn) FROM yourTable GROUP BY UserName

它将为每个用户返回最新条目

输出:

UserName       CreatedOn
    A          2018-12-20
    B          2018-11-20

答案 4 :(得分:0)

如果只想单独记录 A 的结果,则以下查询

SELECT * FROM myTable WHERE UserName = 'A' ORDER BY CreatedOn DESC

如果您想要特定数量的 A 行表示,

SELECT top 2 * FROM myTable WHERE UserName = 'A' ORDER BY CreatedOn DESC

答案 5 :(得分:0)

通过在SQL Server中使用WITH TIES函数

SELECT TOP 2 WITH TIES UserName,CreatedOn  
FROM #tbl
ORDER BY ( ROW_NUMBER()OVER(PARTITION BY UserName ORDER BY CreatedOn DESC ))

结果

UserName    CreatedOn
-----------------------
  A         2018-12-20
  B         2018-11-20

答案 6 :(得分:0)

您可以使用TOP关键字和inner join

尝试以下查询
Create Table #temp
(
Id Int,
UserName VarChar(25),
CreatedOn Date
)

Insert Into #temp Values
(1,'B','2018-11-20'),
(2,'A','2018-12-20'),
(3,'A','2018-11-19'),
(4,'B','2018-11-18')

Select #temp.Id, #temp.UserName, #temp.CreatedOn from #temp 
inner join
(
Select Top 2 MAX(Id) as Id, UserName, CreatedOn from #temp 
group by UserName,CreatedOn ORDER BY CreatedOn DESC
)a on a.Id = #temp.Id

输出如下所示

Id  UserName    CreatedOn
-------------------------
2   A          2018-12-20
1   B          2018-11-20

答案 7 :(得分:0)

根据示例数据,您似乎想要top (1) with ties

select top (1) with ties t.*
from t
order by t.createdon desc;

如果您的日期确实包含时间成分,那么您要提取日期:

select top (1) with ties t.*
from t
order by convert(date, t.createdon) desc;

答案 8 :(得分:0)

数据准备:

Create Table #tbl
(
Id Int,
UserName VarChar(25),
CreatedOn Date
)

Insert Into #tbl Values
(1,'B','2018-11-20'),
(2,'A','2018-12-20'),
(3,'A','2018-11-19'),
(4,'B','2018-11-18')

解决方案:尝试使用这种简单方法:

    SELECT #tbl.Id, t.UserName, t.CreatedOn
    FROM   (
             SELECT Username  AS UserName, MAX(CreatedOn)     CreatedOn
             FROM   #tbl
             GROUP BY UserName
           ) t
    INNER JOIN #tbl ON  #tbl.CreatedOn = t.CreatedOn
    ORDER BY  #tbl.UserName

结果:

Id  UserName    CreatedOn
2     A         2018-12-20
1     B         2018-11-20