Sql 仅选择一个具有最新登录日期的组

Sql 仅选择一个具有最新登录日期的组,sql,oracle,select,group-by,Sql,Oracle,Select,Group By,我正在尝试只获得一个具有最大登录日期的组 user_ID Group Login_Date Mulan Flower 4/4/2017 Mulan Badminton 11/20/2015 Mulan Flower 11/20/2015 我的代码 select distinct group from table t where t.user_ID = 'Mulan' and t.Login_Date = (select

我正在尝试只获得一个具有最大登录日期的组

user_ID    Group       Login_Date
Mulan      Flower        4/4/2017
Mulan      Badminton   11/20/2015
Mulan      Flower      11/20/2015
我的代码

select distinct group from table t
where t.user_ID = 'Mulan'
and t.Login_Date = (select max (t2.Login_Date)
                      from table t2
                      where t.user_ID = t2.user_ID
                        and t2.Login_date <= sysdate)
期望输出

Flower

虽然有2朵鲜花,但我想确保我得到的鲜花具有最近的登录日期,即2017年4月4日,并且输出应仅返回1个值。

只需删除
和t2。登录日期请尝试此操作。您的系统日期可能会回到当前日期

select distinct top 1 group from table t
where t.user_ID = 'Mulan'
and t.Login_Date = (select max (t2.Login_Date)
                      from table t2
                      where t.user_ID = t2.user_ID
                        and t2.Login_date <= sysdate)
对补充问题的答复:

with s (user_ID, grp, Login_Date) as (
select 'Mulan', 'Flower'   , to_date('4/4/2017'  , 'mm/dd/yyyy') from dual union all
select 'Mulan', 'Badminton', to_date('11/20/2015', 'mm/dd/yyyy') from dual union all
select 'Mulan', 'Flower'   , to_date('11/20/2015', 'mm/dd/yyyy') from dual)
select
max(grp) keep (dense_rank last order by login_date) as grp
from s
group by user_id;

GRP
---------
Flower

这应该是一个简单的解决方案

  select  top 1 max(login_date),login_group  from tableName group by login_group  order 
  by COUNT(user_id) desc

TOP 1对我不起作用,它给了我一个错误-我正在使用OraclePlease在where条件下添加rownum=1。谢谢,你的成功了!但快速提问,我只想返回“GRP”。是否可以选择max(GRP)keep(按登录日期排列最后一个订单)作为GRP?它会给我正确的花吗?@BFF检查这个link@BFF检查此链接是否正常工作fine@BFF两个版本都有效
  with cte as
  (
  select t.*,row_number()over(partition by user_ID order by Login_Date desc) rn
  from table t
 ) select * from cte where rn=1
USER_ID     GRP     LOGIN_DATE
Mulan   Flower  04-APR-17
select distinct top 1 group from table t
where t.user_ID = 'Mulan'
and t.Login_Date = (select max (t2.Login_Date)
                      from table t2
                      where t.user_ID = t2.user_ID
                        and t2.Login_date <= sysdate)
 select distinct top 1 group from table t
    where t.user_ID = 'Mulan'
    and t.Login_Date = (select max (t2.Login_Date)
                          from table t2
                          where t.user_ID = t2.user_ID
                        )
-- Oracle: first/last functions
with s (user_ID, grp, Login_Date) as (
select 'Mulan', 'Flower'   , to_date('4/4/2017'  , 'mm/dd/yyyy') from dual union all
select 'Mulan', 'Badminton', to_date('11/20/2015', 'mm/dd/yyyy') from dual union all
select 'Mulan', 'Flower'   , to_date('11/20/2015', 'mm/dd/yyyy') from dual)
select
user_id,
max(grp) keep (dense_rank last order by login_date) as grp,
max(login_date) as log_date
from s
group by user_id;

USER_ GRP       LOG_DATE
----- --------- -------------------
Mulan Flower    2017-04-04 00:00:00
with s (user_ID, grp, Login_Date) as (
select 'Mulan', 'Flower'   , to_date('4/4/2017'  , 'mm/dd/yyyy') from dual union all
select 'Mulan', 'Badminton', to_date('11/20/2015', 'mm/dd/yyyy') from dual union all
select 'Mulan', 'Flower'   , to_date('11/20/2015', 'mm/dd/yyyy') from dual)
select
max(grp) keep (dense_rank last order by login_date) as grp
from s
group by user_id;

GRP
---------
Flower
  select  top 1 max(login_date),login_group  from tableName group by login_group  order 
  by COUNT(user_id) desc