我对SQL相对较新。我相信在我的Oracle查询中需要多个子选择来实现我感兴趣的结果,但是我一直无法使查询正常工作。是否需要多个子查询才能从同一个表中的两个字段中选择最大值
我需要从“客户”表中提取基础数据,并从第二个表“Customer_Issue”中提取附加数据。还有其他表中包含我需要的其他字段,但我已通过加入“客户”表成功地加入/选择了该数据
我需要从“Customer_Issue”获得的数据位于“Service”,“Service_Issue “和”服务版本“。每个服务有多个服务问题,每个服务问题有多个服务版本。
对于每个服务,我只需要选择最大的服务问题,对于那个选定的服务问题,我只需要选择最大的服务版本。
通过一个子选择,我已经能够选择最大服务问题如下:
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
这是正确的解释。我会尝试 – 2013-02-26 15:59:54
工作 - 感谢您的建议 – 2013-02-26 16:37:07