我对 SQL 比较陌生。我相信我需要在我的 Oracle 查询中进行多个子选择才能获得我感兴趣的结果,但我一直无法让查询正常工作。
我需要从“客户”表中提取基本数据,并从第二个表“客户问题”中提取附加数据。还有其他表包含我需要的其他字段,但我已通过连接到“客户”表成功地连接/选择了该数据
我需要的“Customer_Issue”数据位于“Service”、“Service_Issue”和“Service Version”字段中。每个服务有多个服务问题,每个服务问题有多个服务版本。
对于每个服务,我只需要选择最大服务问题,对于选择的服务问题,我只需要选择最大服务版本。
通过子选择,我已经能够选择最大服务问题,如下所示:
Select c.customer_id, ci.service, ci.service_issue
from customer c
left outer join CUSTOMER_issue ci on c.CUSTOMER_ID = ci.CUSTOMER_ID
join (select CUSTOMER_ID, service, max(service_ISSUE) as service_ISSUE
from CUSTOMER_issue
group by CUSTOMER_ID, service) ci1
on ci1.CUSTOMER_ID = ci.CUSTOMER_ID and ci1.service = ci.service
and ci1.service_issue = ci.service_issue
但是,我无法获得后续的子选择来获得最大服务版本。这是我尝试过的(在查询中,它直接在上面的代码下方)
join (select CUSTOMER_ID, service, service_ISSUE, max (service_VERSION) as service_VERSION
from CUSTOMER_issue
group by CUSTOMER_ID, service, service_ISSUE) ci2
on ci1.CUSTOMER_ID = ci2.CUSTOMER_ID and ci1.service = ci2.service and
ci1.service_ISSUE = ci2.service_ISSUE and ci1.service_VERSION = ci2.service_VERSION