簡體   English   中英

查找每個組中最接近給定日期的記錄-SQL

[英]Find record closest to a given date for each group - SQL

我是sql新手。 假設我們有一個這樣的表:

+-------+----------+-----------+
|userid | statusid |   date    |
+-------+----------+-----------+
| 1     |  1       | 2018-10-10| 
| 1     |  2       | 2018-10-12|
| 2     |  1       | 2018-09-25|
| 2     |  1       | 2018-10-01|
+-------+----------+-----------+

我需要獲取每個用戶ID的stateid,使其日期盡可能接近給定的日期。 說我的給定日期是2018-10-01。 我該怎么做? 我嘗試了各種groupby和partition by,但是沒有任何效果。 有人可以幫忙嗎?

編輯:我的數據庫是亞馬遜redshift

您可以使用row_number()窗口分析函數,按日期差的絕對值排序。

(請注意, row_number()MySQL 8-不起作用,因此未使用該函數,但使用了abs()函數。)

我不知道您的DBMS

該解決方案適用於Oracle

with tab(userid, statusid, "date") as
(
 select 1,1,date'2018-10-10' from dual union all
 select 1,2,date'2018-10-12' from dual union all
 select 2,1,date'2018-09-25' from dual union all
 select 2,1,date'2018-10-02' from dual
)
select tt.userid, tt.statusid, tt."date"
  from
(
select t.userid, t.statusid , t."date",
       row_number() over (partition by t.userid 
                          order by abs("date" - date'2018-10-01')) as rn
  from tab t
) tt
where tt.rn = 1

Oracle演示

此解決方案適用於SQL Server

with tab([userid], [statusid], [date]) as
(
 select 1,1,'2018-10-10' union all
 select 1,2,'2018-10-12' union all
 select 2,1,'2018-09-25' union all
 select 2,1,'2018-10-02' 
)
select tt.[userid], tt.[statusid], tt.[date]
  from
(
select t.[userid], t.[statusid] , t.[date], 
       row_number() over (partition by t.[userid] 
                          order by abs(datediff(day,[date],'2018-10-01'))) as rn
  from tab t
) tt
where tt.rn = 1

SQL Server演示

該解決方案適用於My SQL

select tt.userid, tt.statusid, tt.date
  from
  (
   select t.userid, t.statusid , t.date,
          @rn := if(@iter = t.userid, @rn + 1, 1) as rn,
          @iter := t.userid, 
          abs(date - date'2018-10-01') as df
     from tab t
     join (select @iter := 0, @rn := 0) as q_iter
    order by t.userid, abs(date - date'2018-10-01') 
  ) tt
where tt.rn = 1

我的SQL演示

此解決方案適用於PostGRES

with tab(userid, statusid, date) as
(
 select 1,1,'2018-10-10' union all
 select 1,2,'2018-10-12' union all
 select 2,1,'2018-09-25' union all
 select 2,1,'2018-10-02' 
)
select tt.userid, tt.statusid, tt.date
  from
(
select t.userid, t.statusid , t.date, 
       row_number() over (partition by t.userid
                          order by abs(date::date-'2018-10-01'::date)) as rn
  from tab t
) tt
where tt.rn = 1

演示PostGRESql

通常對於這種類型的問題,您希望日期在給定日期或之前。

如果是這樣的話:

select t.*
from t
where t.date = (select max(t2.date)
                from t t2
                where t2.userid = t.userid and t2.date <= '2018-10-01'
               );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM