美好的一天!我是初学者,在做查询时我还在学习它,任何人都可以帮助我如何加入两个不同的选择查询,这是我的查询代码。
提前感谢您帮助我。
Select * from
( select
mm.MemberId CLIENT_REFERENCE,
mm.LastNM LAST_NAME,
mm.FirstNm FIRST_NAME,
mm.MidNm MIDDLE_NAME,
mm.Address NO_STREET_SITIO_PUROK,
vm.VillageName BARANGAY_DISTRICT,
cm.City CITY_MUNICIPALITY,
pm.Province PROVINCE,
'0000' ZIP_CODE,
mm.MemDOB BIRTHDATE,
mm.MemGen GENDER,
mm.ContNo CONTACT_NO,
'' ID_TYPE,
'' ID_NO,
mm.SSS [SSS/GSIS],
'' PAGIBIG,
'' PHILHEALTH,
mm.TIN TIN,
lm.LoanNo LOAN_REFERENCE,
'10' CONTRACT_TYPE,
'AC' CONTRACT_PHASE,
'NA' TRANSACTION_TYPE,
lm.LoanAmt LOAN_PRINCIPAL,
wom.PrinAmt LOAN_BALANCE,
lm.LoanDt DATE_GRANTED,
''DATE_DUE,
lm.IntRate INTEREST_RATE,
'Weekly' PAY_FREQ,
lm.TotalInstNo TERM,
'PHP' CURRENCY,
'ET' LOAN_PURPOSE,
'04-01' BAR_TYPE,
cd.IntCollAmt + cd.PrinCollAmt TOTAL_LOAN_BALANCE,
'' CONTRACT_ACTUAL_END_DATE,
'0' OVERDUE_DAYS,
'' MONTHLY_PAYMENT_AMOUNT,
'0'NO_OF_OUTSTANDING_PAYMENTS,
cd.NoofInst - 1 AMOUNT_OF_LAST_PAYMENT,
'Written Off Clients' Remarks
from MemberMst mm
inner join VillageMst vm
on vm.VillageId = mm.Barangay
inner join CityMst cm
on vm.CityId = cm.CityId
inner join ProvinceMst pm
on cm.ProvinceId = pm.ProvinceId
---------------------------------
inner join LoanMst lm
on lm.MemberId = mm.MemberId
inner join CollectionDtl cd
on cd.LoanId = lm.LoanId
inner join WriteOffMst wom
on wom.LoanId = lm.LoanId
inner join BranchMst bm
on bm.BranchCode = wom.BranchCode
inner join LoanSubPurposeMst lsm
on lsm.LSubPurposeId = lm.SubPurposeId
where wom.WriteoffDate between '2016-01-01' and '2016-12-31' and cd.AccDate = wom.WriteoffDate
) as q1
inner join
(
SELECT *
FROM
(
SELECT cd.NoofInst,cd.CollAmt, ROW_NUMBER() OVER (ORDER BY cd.NoofInst DESC) rn
FROM MemberMst mm
INNER JOIN LoanMst lm
ON lm.MemberId = mm.MemberId
INNER JOIN CollectionDtl cd
ON cd.LoanId = lm.LoanId
INNER JOIN WriteOffMst wom
ON wom.LoanId = cd.LoanId
WHERE
mm.MemberId = '0050000404' AND
lm.WriteoffDate = wom.WriteOffDate
) t
where t.rn = 2
) as q2
嗨,美好的一天!我是初学者,在做查询时我还在学习它,任何人都可以帮助我如何加入两个不同的选择查询,这是我的查询代码。
答案 0 :(得分:0)
只是为了解决错误而不是实际结果。
Missing ON clause to join q2 and q1.
应该是
where t.rn = 2
) as q2 on q2.<field> = q1.<field>
答案 1 :(得分:0)
我将提出建议,但可能会产生意外结果,请使用cross join
:
SELECT
*
FROM (
SELECT
mm.MemberId client_reference
, mm.LastNM last_name
, mm.FirstNm first_name
, mm.MidNm middle_name
, mm.Address no_street_sitio_purok
, vm.VillageName barangay_district
, cm.City city_municipality
, pm.Province province
, '0000' zip_code
, mm.MemDOB birthdate
, mm.MemGen gender
, mm.ContNo contact_no
, '' id_type
, '' id_no
, mm.SSS [sss/gsis]
, '' pagibig
, '' philhealth
, mm.TIN tin
, lm.LoanNo loan_reference
, '10' contract_type
, 'AC' contract_phase
, 'NA' transaction_type
, lm.LoanAmt loan_principal
, wom.PrinAmt loan_balance
, lm.LoanDt date_granted
, '' date_due
, lm.IntRate interest_rate
, 'Weekly' pay_freq
, lm.TotalInstNo term
, 'PHP' currency
, 'ET' loan_purpose
, '04-01' bar_type
, cd.IntCollAmt + cd.PrinCollAmt total_loan_balance
, '' contract_actual_end_date
, '0' overdue_days
, '' monthly_payment_amount
, '0' no_of_outstanding_payments
, cd.NoofInst - 1 amount_of_last_payment
, 'Written Off Clients' remarks
FROM MemberMst mm
INNER JOIN VillageMst vm ON vm.VillageId = mm.Barangay
INNER JOIN CityMst cm ON vm.CityId = cm.CityId
INNER JOIN ProvinceMst pm ON cm.ProvinceId = pm.ProvinceId
---------------------------------
INNER JOIN LoanMst lm ON lm.MemberId = mm.MemberId
INNER JOIN CollectionDtl cd ON cd.LoanId = lm.LoanId
INNER JOIN WriteOffMst wom ON wom.LoanId = lm.LoanId
INNER JOIN BranchMst bm ON bm.BranchCode = wom.BranchCode
INNER JOIN LoanSubPurposeMst lsm ON lsm.LSubPurposeId = lm.SubPurposeId
WHERE wom.WriteoffDate BETWEEN '2016-01-01' AND '2016-12-31'
AND cd.AccDate = wom.WriteoffDate
) AS q1
CROSS JOIN (
SELECT
*
FROM (
SELECT
cd.NoofInst
, cd.CollAmt
, ROW_NUMBER() OVER (ORDER BY cd.NoofInst DESC) rn
FROM MemberMst mm
INNER JOIN LoanMst lm ON lm.MemberId = mm.MemberId
INNER JOIN CollectionDtl cd ON cd.LoanId = lm.LoanId
INNER JOIN WriteOffMst wom ON wom.LoanId = cd.LoanId
WHERE mm.MemberId = '0050000404'
AND lm.WriteoffDate = wom.WriteOffDate
) t
WHERE t.rn = 2
) AS q2
但是一旦看到结果,您就会注意到第二个查询的3列的所有行都具有相同的值。
这是第二个建议,基于我认为可能的意图。第二个查询现在返回引用MemberId的多行,因此我们现在可以使用该列进行连接:
SELECT
*
FROM (
SELECT
mm.MemberId client_reference
, mm.LastNM last_name
, mm.FirstNm first_name
, mm.MidNm middle_name
, mm.Address no_street_sitio_purok
, vm.VillageName barangay_district
, cm.City city_municipality
, pm.Province province
, '0000' zip_code
, mm.MemDOB birthdate
, mm.MemGen gender
, mm.ContNo contact_no
, '' id_type
, '' id_no
, mm.SSS [sss/gsis]
, '' pagibig
, '' philhealth
, mm.TIN tin
, lm.LoanNo loan_reference
, '10' contract_type
, 'AC' contract_phase
, 'NA' transaction_type
, lm.LoanAmt loan_principal
, wom.PrinAmt loan_balance
, lm.LoanDt date_granted
, '' date_due
, lm.IntRate interest_rate
, 'Weekly' pay_freq
, lm.TotalInstNo term
, 'PHP' currency
, 'ET' loan_purpose
, '04-01' bar_type
, cd.IntCollAmt + cd.PrinCollAmt total_loan_balance
, '' contract_actual_end_date
, '0' overdue_days
, '' monthly_payment_amount
, '0' no_of_outstanding_payments
, cd.NoofInst - 1 amount_of_last_payment
, 'Written Off Clients' remarks
FROM MemberMst mm
INNER JOIN VillageMst vm ON vm.VillageId = mm.Barangay
INNER JOIN CityMst cm ON vm.CityId = cm.CityId
INNER JOIN ProvinceMst pm ON cm.ProvinceId = pm.ProvinceId
---------------------------------
INNER JOIN LoanMst lm ON lm.MemberId = mm.MemberId
INNER JOIN CollectionDtl cd ON cd.LoanId = lm.LoanId
INNER JOIN WriteOffMst wom ON wom.LoanId = lm.LoanId
INNER JOIN BranchMst bm ON bm.BranchCode = wom.BranchCode
INNER JOIN LoanSubPurposeMst lsm ON lsm.LSubPurposeId = lm.SubPurposeId
WHERE wom.WriteoffDate BETWEEN '2016-01-01' AND '2016-12-31'
AND cd.AccDate = wom.WriteoffDate
) AS q1
INNER JOIN (
SELECT
*
FROM (
SELECT
mm.MemberId
, cd.NoofInst
, cd.CollAmt
, ROW_NUMBER() OVER (ORDER BY cd.NoofInst DESC) rn
FROM MemberMst mm
INNER JOIN LoanMst lm ON lm.MemberId = mm.MemberId
INNER JOIN CollectionDtl cd ON cd.LoanId = lm.LoanId
INNER JOIN WriteOffMst wom ON wom.LoanId = cd.LoanId
WHERE wom.WriteoffDate BETWEEN '2016-01-01' AND '2016-12-31'
) t
WHERE t.rn = 2
) AS q2 ON q1.MemberId = q2.MemberId