如何select每个月的最新记录?
How to select the latest record of each month?
我想和sql做朋友真的很难。请帮忙!
我有历史 table,其中包含 3 年的客户主记录。
table 看起来像这样:
Table答:
| ID | Name | Number|Date(in Timestamp)|
|:--- |:----:|:-----:|-----------------:|
|123 | John | 101 |20210101 01:11:15 |
|123 | John | 102 |20210103 01:11:15 |
|123 | John | 103 |20210301 01:11:15 |
|123 | John | 104 |20210325 01:11:15 |
|123 | John | 105 |20210415 01:11:15 |
|123 | John | 106 |20210416 01:11:15 |
|124 | Mary | 201 |20210101 01:11:15 |
|124 | Mary | 202 |20210103 01:11:15 |
|124 | Mary | 203 |20210201 01:11:15 |
|124 | Mary | 204 |20210225 01:11:15 |
|124 | Mary | 204 |20210315 01:11:15 |
|124 | Mary | 205 |20210416 01:11:15 |
我想知道每个月末每个客户的记录是多少。
预期结果是:
| ID | Name | Number|Date |
|123 | John | 102 |20210131|
|123 | John | 104 |20210331|
|123 | John | 106 |20210430|
|124 | Mary | 202 |20210131|
|124 | Mary | 204 |20210228|
|124 | Mary | 204 |20210331|
|124 | Mary | 205 |20210430|
我在 sql 下面创建了,但看起来我不能将 substr 用于时间戳。
还有其他方法吗?非常感谢任何帮助!
select * from (select t1.id, t1.name, t1.number, t1.date,
ROW_NUMBER() over (partition by substr(date,1,6) order by date desc) as Rowrank
from tableA t1)sub where Rowrank = 1
试试这个答案:
根据Id, year and Month
.
从时间戳值和分区中获取Year
和Month
值
select *
from (
select t1.id, t1.name, t1.number, t1.date,
ROW_NUMBER() over (partition by t1.id,YEAR(date_format(str_to_date(t1.date, '%Y%m%d'),'%Y-%m-%d')),MONTH(date_format(str_to_date(t1.date, '%Y%m%d'),'%Y-%m-%d')) order by date desc) as Rowrank
from tableA t1
)sub where Rowrank = 1
在 Teradata 中,您可能会发现 trunc()
是一个简单的方法:
select a.id, a.name, a.number, a.date
from (select a.*,
row_number() over (partition by trunc(date, 'MON') order by date desc) as seqnum
from tableA a
) a
where seqnum = 1;
Teradata 还支持限定:
select a.id, a.name, a.number, a.date
from tableA a
qualify row_number() over (partition by trunc(date, 'MON') order by date desc) = 1
我想和sql做朋友真的很难。请帮忙!
我有历史 table,其中包含 3 年的客户主记录。
table 看起来像这样:
Table答:
| ID | Name | Number|Date(in Timestamp)|
|:--- |:----:|:-----:|-----------------:|
|123 | John | 101 |20210101 01:11:15 |
|123 | John | 102 |20210103 01:11:15 |
|123 | John | 103 |20210301 01:11:15 |
|123 | John | 104 |20210325 01:11:15 |
|123 | John | 105 |20210415 01:11:15 |
|123 | John | 106 |20210416 01:11:15 |
|124 | Mary | 201 |20210101 01:11:15 |
|124 | Mary | 202 |20210103 01:11:15 |
|124 | Mary | 203 |20210201 01:11:15 |
|124 | Mary | 204 |20210225 01:11:15 |
|124 | Mary | 204 |20210315 01:11:15 |
|124 | Mary | 205 |20210416 01:11:15 |
我想知道每个月末每个客户的记录是多少。
预期结果是:
| ID | Name | Number|Date |
|123 | John | 102 |20210131|
|123 | John | 104 |20210331|
|123 | John | 106 |20210430|
|124 | Mary | 202 |20210131|
|124 | Mary | 204 |20210228|
|124 | Mary | 204 |20210331|
|124 | Mary | 205 |20210430|
我在 sql 下面创建了,但看起来我不能将 substr 用于时间戳。 还有其他方法吗?非常感谢任何帮助!
select * from (select t1.id, t1.name, t1.number, t1.date,
ROW_NUMBER() over (partition by substr(date,1,6) order by date desc) as Rowrank
from tableA t1)sub where Rowrank = 1
试试这个答案:
根据Id, year and Month
.
Year
和Month
值
select *
from (
select t1.id, t1.name, t1.number, t1.date,
ROW_NUMBER() over (partition by t1.id,YEAR(date_format(str_to_date(t1.date, '%Y%m%d'),'%Y-%m-%d')),MONTH(date_format(str_to_date(t1.date, '%Y%m%d'),'%Y-%m-%d')) order by date desc) as Rowrank
from tableA t1
)sub where Rowrank = 1
在 Teradata 中,您可能会发现 trunc()
是一个简单的方法:
select a.id, a.name, a.number, a.date
from (select a.*,
row_number() over (partition by trunc(date, 'MON') order by date desc) as seqnum
from tableA a
) a
where seqnum = 1;
Teradata 还支持限定:
select a.id, a.name, a.number, a.date
from tableA a
qualify row_number() over (partition by trunc(date, 'MON') order by date desc) = 1