我有两个表A
(group_id, id, subject
)和B
(id, date
)。以下是ID上的表A和B的联合表。我尝试使用distinct和partition删除仅在group_id(field)中的重复项,但没有运气:
我的代码:
select
a.group_id, a.id, a.subject, b.date
from
A a
inner join
(select
b.*,
row_number() over (partition by group_id order by date asc) as seqnum
from
B b) b on a.id = b.id and seqnum = 1
order by
date desc;
运行代码时出现此错误:
不能在第1行的“从B到seqnum的按group_id的按日期排序asc)的查询附近单独使用分区
这是我的预期结果:
先谢谢您!
答案 0 :(得分:1)
您似乎想要显示的表中每一行的最早日期。您的问题提到了两个表,但您只显示了一个。
我建议在大多数数据库中使用相关子查询:
select b.*
from b
where b.date = (select min(b2.date)
from b b2
where b2.group_id = b.group_id
);
我明白了。您需要先join
,然后再使用row_number()
:
select ab.*
from (select a.group_id, a.id, a.subject, b.date,
row_number() over (partition by a.group_id order by b.date) as seqnum
from A a join
B b
on a.id = b.id
) ab
where seqnum = 1
order by date desc;
答案 1 :(得分:1)
您快到了。但是您尝试用于分区的列(即group_id
)来自表a
,该表在子查询中不可用。
您需要JOIN
并在子查询中分配行号,并在外部查询中过滤 then 。
select *
from (
select
a.group_id,
a.id,
a.subject,
b.date,
row_number() over (partition by a.group_id order by b.date asc) as seqnum
from a
inner join b on ON a.id = b.id
)
where seqnum = 1
ORDER BY date desc;
答案 2 :(得分:0)
另一种实现目标的方法,虽然可能不是有效的方法
SELECT
A.group_id, A.id, B.Date, A.subject
FROM A
INNER JOIN B
ON A.Id = B.Id
INNER JOIN
(
SELECT
A.Group_id, MIN(B.Date) AS Date
FROM A
INNER JOIN B
ON A.Id = B.Id
GROUP BY A.group_id
) AS supportTable
ON A.group_id = supportTable.group_id
AND B.Date = supportTable.Date