如何按条件分组?

时间:2019-03-15 13:15:50

标签: sql oracle

我必须做一组项目。

例如:

Item 1:
name: test1
date: 2018-01-01
column1: column1
column2: column2
column1: column3
column2: column4

Item 2:
name: test1
date: 2018-01-03
column1: column5
column2: column6
column1: column7
column2: column8

Item 3:
name: test2
date: 2018-01-01
column1: column9
column2: column10
column1: column11
column2: column12

Item 4:
name: test3
date: 2018-01-05
column1: column13
column2: column14
column1: column15
column2: column16

我想做一个sql,以按日期分组的项目,但每个名称只有一个,例如:

name: test1 
date: 2018-01-03
column1: column5
column2: column6
column1: column7
column2: column8

name: test2
date: 2018-01-01
column1: column9
column2: column10
column1: column11
column2: column12

name: test3
date: 2018-01-05
column1: column13
column2: column14
column1: column15
column2: column16

您有一个实现该想法的想法吗? 问候

编辑:我添加了更多属性

2 个答案:

答案 0 :(得分:2)

我认为您需要在子查询中进行这种分组

select *
  from tab
 where ( name, "date" ) in
     ( select name, max("date")
         from tab
        group by name 
      )

答案 1 :(得分:0)

尝试使用max()

select name, max(date) as date
from tablename
group by name