我在Oracle 10g中有两个表。结合两个查询(子查询)
表1:
loan_id installment
1 500
2 600
3 800
表2:
loan_id amount date
1 200 5/Jan/2014
3 800 20/Jan/2014
第一步:选择特定的日期范围之内的所有的行。
Select * from table2 where date>a and date<b
第2步:选择表2中loan_id不在步骤1的结果集中的所有行。
Select * from table1 where loan_id NOT IN (Select loan_id from table2 where date>a and date<b)
第三步:我也想选择那些loan_id,其数量(表2)小于分期付款(表2)。
我的问题可以我们结合步骤2和步骤3在单个查询?
谢谢。
编辑:
下面的组合查询给我输出。
Select alias.loan_id from ((Select * from
transactions where date>2 and date<6)as alias) inner join loansapproved l on
alias.loan_id = l.loan_id where alias.amount<l.installment
UNION
Select l.loan_id from loansapproved l left join transactions t
on l.loan_id=t.loan_id where l.loan_id not in(Select loan_id from
transactions where date>2 and date<6) ;
任何人都可以简化这个吗?
据我理解你的问题,你可以做到这一点。你尝试使用连接吗?类似如下:从table1中选择t1.loan_id t1在t1.loan_id = t2.loan_id中将左连接table2 t2在其中t1.loan_id NOT IN(从table2中选择loan_id,其中date> a和date Gayathri
这不会将表2中的loan_ids提取出来,其金额小于分期付款。 – ArunKumar