目标:我想基于由创建ID列排序的订阅ID列分组的Id列的第一个值来联接两个表。
情况:
Table1看起来像这样:
id channel trx_date
123 organic 01/01/2019 05:00:00
234 direct 01/01/2019 05:01:00
987 add 01/01/2019 10:00:00
654 organic 01/01/2019 10:15:00
表2:
subscription_id id os created_at
sub890 123 mac 01/01/2019 05:00:01
sub890 234 mac 01/01/2019 05:01:01
sub111 987 windows 01/01/2019 10:00:01
sub111 654 mac 01/01/2019 10:20:01
我需要采用按预订ID分组的表2中的最早ID,并将其与表1进行内部连接。 因此,在此示例中,我的输出将是
subscription_id id os created_at id channel trx_date
sub890 123 mac 01/01/2019 05:00:01 organic 01/01/2019 05:00:00
sub111 987 windows 01/01/2019 10:00:01 add 01/01/2019 10:00:00
我尝试过的事情:我曾考虑过使用FIRST_VALUE,但对于如何连接它们却一无所知
SELECT t1.*,
t2.subscription_id,
t2.os,
t2.created_at,
FIRST_VALUE(t2.id) OVER (PARTITION BY t2.subscription_id ORDER BY t2.created_at ASC) as Min_Id
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.Min_id
小提琴信息:
CREATE TABLE table1
([id] varchar(13), [channel] varchar(50), [trx_date] Datetime)
INSERT INTO table1
VALUES
('123', 'organic', '2019-01-01 05:00:00'),
('234', 'direct', '2019-01-01 05:01:00'),
('987', 'add', '2019-01-01 10:00:00'),
('654', 'organic', '2019-01-01 10:15:00')
CREATE TABLE table2
([subscription_id] varchar(13),[id] varchar(13), [os] varchar(10), [created_at] Datetime)
INSERT INTO table2
VALUES
('sub890', '123', 'mac', '2019-01-01 05:00:01'),
('sub890', '234', 'mac', '2019-01-01 05:01:01'),
('sub111', '987', 'windows', '2019-01-01 10:00:01'),
('sub111', '654', 'mac', '2019-01-01 10:20:01')
由于ON子句,这显然不起作用。这种情况是否需要row_number函数加上叉号?有更好的方法吗? FIRST_VALUE是使用错误的函数吗?
答案 0 :(得分:2)
将apply
与top (1)
一起使用:
SELECT t1.*, t2.subscription_id, t2.id, t2.os, t2.created_at
FROM table1 t1 CROSS APPLY
(SELECT TOP (1) t2.*
FROM table2 t2
WHERE t1.id = t2.id
ORDER BY t2.created_at ASC
) t2
答案 1 :(得分:2)
您可以将row_number()
与create_at日期一起使用,该日期将采用第一个ID
with cte as
(
select *,row_number() over(partition by subscription_id order by created_at) rn
from tabl2
) select cte.*,t1.* from cte
join table1 t1 on cte.id =t1.id
where cte.rn=1
subscription_id id os created_at rn id channel trx_date
sub890 123 mac 01/01/2019 05:00:01 1 123 organic 01/01/2019 05:00:00
sub111 987 windows 01/01/2019 10:00:01 1 987 add 01/01/2019 10:00:00