Oracle sql 基于日期的连接

问题描述 投票:0回答:1

我有下表,其中包含以下内容

表-1

Id 
Error
Start Date
End Date

表-2

Id 
Address 
Start Date
End Date

需要sql查询-

这里的ID是table1中的主键,table2中的外键。 我想从 table1 中选择一个 id,然后在 table2 中查找为给定 id 创建了多少个 id,例如

ID=1 从表1中选取 根据 1 的日期查找表 2 中创建了多少个新 ID。

最终结果是

ID count_of_id_from_table2
1  5
2  10
2  300

注意 - 记录以百万计

sql oracle count
1个回答
0
投票

您所描述的听起来像是(外部)连接(外部,因为可能存在

table1.id
值在 table2 中没有
match
):

select a.id, 
       count(b.rowid) count_of_id_from_table2
from table_1 a left join table_2 b on b.id = a.id
group by a.id

为了获得更好的性能,我认为两个

ID
列上的索引可能/应该有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.