2013-10-07 66 views
0

我如何加入兩個查詢是波紋管:我怎麼合併兩個查詢

Required output query: 
Query#1_Col_1, Query#1_Col_2, 
Query#1_Col3, Query#2_Col_1, 
Query#2_Col_2, Query#2_Col_3 

查詢#01

;With RnkItems As 
    (
SELECT  
dbo.TblGuarantorInfo.AppID, dbo.TblGuarantorInfo.GName, dbo.TblGuarantorInfo.GRelationWithCustomer, 
dbo.TblGuarantorInfo.GFahterSpoueName, dbo.TblGuarantorInfo.GMotherName, 
dbo.TblGuarantorInfo.GBusinessName, dbo.TblGuarantorInfo.GDesig, 
dbo.TblGuarantorInfo.GBusinessAddressLine1, dbo.TblGuarantorInfo.GBusinessAddressLine2, 
dbo.TblGuarantorInfo.GBusinessAddressLine3, dbo.TblGuarantorInfo.ResidenceAddressLIne1, 
dbo.TblGuarantorInfo.ResidenceAddressLIne2, dbo.TblGuarantorInfo.ResidenceAddressLIne3, 
dbo.TblGuarantorInfo.GPrePhone, dbo.TblGuarantorInfo.GResPhone, dbo.TblGuarantorInfo.GMobile, 
dbo.TblGuarantorInfo.GResStatus, dbo.TblGuarantorInfo.GPermanentAddress1, 
dbo.TblGuarantorInfo.GPermanentAddress2, dbo.TblGuarantorInfo.GID, 
ROW_NUMBER() Over (Partition By dbo.TblGuarantorInfo.AppID Order By GID, dbo.TblGuarantorInfo.GName) As Rnk 
FROM dbo.TblGuarantorInfo) 

SELECT AppID 
,MIN (Case When Rnk=1 Then GName End) As G1_Name 
,MIN (Case When Rnk=1 Then GRelationWithCustomer End) As G1_Relation 
,MIN (Case When Rnk=1 Then GFahterSpoueName end) As G1_FatherName 
,MIN (case when rnk=1 then GMotherName end) AS G1_MotherName 
,MIN (case when rnk=1 then GBusinessName end) AS G1_BusinessName 
,MIN (case when rnk=1 then GDesig end) AS G1_Desig 
,MIN (case when rnk=1 then GBusinessAddressLine1 end) AS G1_BusLine1 
,MIN (case when rnk=1 then GBusinessAddressLine2 end) AS G1_BusLine2 
,MIN (case when rnk=1 then GBusinessAddressLine3 end) AS G1_BusLine3 
,MIN (case when rnk=1 then ResidenceAddressLIne1 end) AS G1_Res1 
,MIN (case when rnk=1 then ResidenceAddressLIne2 end) AS G1_Res2 
,MIN (case when rnk=1 then ResidenceAddressLIne3 end) AS G1_Res3 
,MIN (case when rnk=1 then GPrePhone end) AS G1_PrePhone 
,MIN (case when rnk=1 then GResPhone end) AS G1_ResPhone 
,MIN (case when rnk=1 then GMobile end) AS G1_MobileNo 
,MIN (case when rnk=1 then GResStatus end) AS G1_ResStatus 
,MIN (case when rnk=1 then GPermanentAddress1 end) AS G1_PerAddress1 
,MIN (case when rnk=1 then GPermanentAddress2 end) AS G1_perAddress2 

,MIN (Case When Rnk=2 Then GName End) As G1_Name2 
,MIN (Case When Rnk=2 Then GRelationWithCustomer End) As G2_Relation 
,MIN (Case When Rnk=2 Then GFahterSpoueName end) As G2_FatherName 
,MIN (case when rnk=2 then GMotherName end) AS G2_MotherName 
,MIN (case when rnk=2 then GBusinessName end) AS G2_BusinessName 
,MIN (case when rnk=2 then GDesig end) AS G2_Desig 
,MIN (case when rnk=2 then GBusinessAddressLine1 end) AS G2_BusLine1 
,MIN (case when rnk=2 then GBusinessAddressLine2 end) AS G2_BusLine2 
,MIN (case when rnk=2 then GBusinessAddressLine3 end) AS G2_BusLine3 
,MIN (case when rnk=2 then ResidenceAddressLIne1 end) AS G2_Res1 
,MIN (case when rnk=2 then ResidenceAddressLIne2 end) AS G2_Res2 
,MIN (case when rnk=2 then ResidenceAddressLIne3 end) AS G2_Res3 
,MIN (case when rnk=2 then GPrePhone end) AS G2_PrePhone 
,MIN (case when rnk=2 then GResPhone end) AS G2_ResPhone 
,MIN (case when rnk=2 then GMobile end) AS G2_MobileNo 
,MIN (case when rnk=2 then GResStatus end) AS G2_ResStatus 
,MIN (case when rnk=2 then GPermanentAddress1 end) AS G2_PerAddress1 
,MIN (case when rnk=2 then GPermanentAddress2 end) AS G2_perAddress2 

,MIN (Case When Rnk=3 Then GName End) As G3_Name 
,MIN (Case When Rnk=3 Then GRelationWithCustomer End) As G3_Relation 
,MIN (Case When Rnk=3 Then GFahterSpoueName end) As G3_FatherName 
,MIN (case when rnk=3 then GMotherName end) AS G3_MotherName 
,MIN (case when rnk=3 then GBusinessName end) AS G3_BusinessName 
,MIN (case when rnk=3 then GDesig end) AS G3_Desig 
,MIN (case when rnk=3 then GBusinessAddressLine1 end) AS G3_BusLine1 
,MIN (case when rnk=3 then GBusinessAddressLine2 end) AS G3_BusLine2 
,MIN (case when rnk=3 then GBusinessAddressLine3 end) AS G3_BusLine3 
,MIN (case when rnk=3 then ResidenceAddressLIne1 end) AS G3_Res1 
,MIN (case when rnk=3 then ResidenceAddressLIne2 end) AS G3_Res2 
,MIN (case when rnk=3 then ResidenceAddressLIne3 end) AS G3_Res3 
,MIN (case when rnk=3 then GPrePhone end) AS G3_PrePhone 
,MIN (case when rnk=3 then GResPhone end) AS G3_ResPhone 
,MIN (case when rnk=3 then GMobile end) AS G3_MobileNo 
,MIN (case when rnk=3 then GResStatus end) AS G3_ResStatus 
,MIN (case when rnk=3 then GPermanentAddress1 end) AS G3_PerAddress1 
,MIN (case when rnk=3 then GPermanentAddress2 end) AS G3_perAddress2 
,Min(Case When Rnk=4 Then GRelationWithCustomer End) As G4_Relation 

,MIN (Case When Rnk=4 Then GName End) As G4_Name 
,MIN (Case When Rnk=4 Then GFahterSpoueName end) As G4_FatherName 
,MIN (case when rnk=4 then GMotherName end) AS G4_MotherName 
,MIN (case when rnk=4 then GBusinessName end) AS G4_BusinessName 
,MIN (case when rnk=4 then GDesig end) AS G4_Desig 
,MIN (case when rnk=4 then GBusinessAddressLine1 end) AS G4_BusLine1 
,MIN (case when rnk=4 then GBusinessAddressLine2 end) AS G4_BusLine2 
,MIN (case when rnk=4 then GBusinessAddressLine3 end) AS G4_BusLine3 
,MIN (case when rnk=4 then ResidenceAddressLIne1 end) AS G4_Res1 
,MIN (case when rnk=4 then ResidenceAddressLIne2 end) AS G4_Res2 
,MIN (case when rnk=4 then ResidenceAddressLIne3 end) AS G4_Res3 
,MIN (case when rnk=4 then GPrePhone end) AS G4_PrePhone 
,MIN (case when rnk=4 then GResPhone end) AS G4_ResPhone 
,MIN (case when rnk=4 then GMobile end) AS G4_MobileNo 
,MIN (case when rnk=4 then GResStatus end) AS G4_ResStatus 
,MIN (case when rnk=4 then GPermanentAddress1 end) AS G4_PerAddress1 
,MIN (case when rnk=4 then GPermanentAddress2 end) AS G4_perAddress2 

From RnkItems 
Group By AppID 

查詢#02

select dbo.TblMasterInfo.AppID , dbo.TblMasterInfo.AppAsignTo, 
    dbo.TblMasterInfo.AppName, dbo.TblMasterInfo.AppLoanType, dbo.TblMasterInfo.AppLoanStatus, 
    dbo.TblClientInfo.ClFatherName, dbo.TblClientInfo.ClMotherName, dbo.TblClientInfo.ClDOB, 
    dbo.TblClientInfo.ClPrePhone, dbo.TblClientInfo.ClPreMobile, dbo.TblClientInfo.ClResidentStatus, 
    dbo.TblClientInfo.ClPreAddressLine1, dbo.TblClientInfo.ClPreAddressLine2, 
    dbo.TblClientInfo.ClPreAddressLine3, dbo.TblClientInfo.ClPreAddressLine4, 
    dbo.TblClientInfo.ClPerAddressLine1, dbo.TblClientInfo.ClPerAddressLine2, 
    dbo.TblClientInfo.ClPerAddressLine3, dbo.TblClientInfo.ClPerAddressLine4, 
    dbo.TblProfessionalInfo.ProfOccupation, dbo.TblProfessionalInfo.ProfCompanyName, 
    dbo.TblProfessionalInfo.ProfDesig, dbo.TblProfessionalInfo.ProfAddressLine1, 
    dbo.TblProfessionalInfo.ProfAddressLine2, dbo.TblProfessionalInfo.ProfAddressLine3, 
    dbo.TblProfessionalInfo.ProfAddressLine4 
    FROM dbo.TblMasterInfo INNER JOIN 
    dbo.TblClientInfo ON dbo.TblMasterInfo.AppID = dbo.TblClientInfo.AppID INNER JOIN 
    dbo.TblProfessionalInfo ON dbo.TblMasterInfo.AppID = dbo.TblProfessionalInfo.APPID 
    WHERE (dbo.TblMasterInfo.AppLoanType = 'Auto Loan') AND (dbo.TblMasterInfo.AppLoanStatus = 'Disbursed') 
+0

這是太多..你應該提供初始和期望的輸出 – zxc

+0

這兩個查詢之間的關係是什麼?您能否簡化查詢以解決主要問題?任何人都難以理解所有這些代碼。 –

+0

dbo.TblMasterInfo.AppID和dbo.TblGuarantorInfo.AppID是這兩個表的關係。 –

回答

1

這些都是長的查詢,我不會直接寫在這裏,但可以使用該模式鏈接到內部查詢:

select x.column1, x.column2, y.column3, y.column4 
from 
(
    -- your whole query 1 
) x 
inner join 
(
    -- your whole query 2 
) y 
on x.id = y.id 

編輯

希望這個作品,這些查詢太大檢查:

;With RnkItems As 
    (
SELECT  
dbo.TblGuarantorInfo.AppID, dbo.TblGuarantorInfo.GName, dbo.TblGuarantorInfo.GRelationWithCustomer, 
dbo.TblGuarantorInfo.GFahterSpoueName, dbo.TblGuarantorInfo.GMotherName, 
dbo.TblGuarantorInfo.GBusinessName, dbo.TblGuarantorInfo.GDesig, 
dbo.TblGuarantorInfo.GBusinessAddressLine1, dbo.TblGuarantorInfo.GBusinessAddressLine2, 
dbo.TblGuarantorInfo.GBusinessAddressLine3, dbo.TblGuarantorInfo.ResidenceAddressLIne1, 
dbo.TblGuarantorInfo.ResidenceAddressLIne2, dbo.TblGuarantorInfo.ResidenceAddressLIne3, 
dbo.TblGuarantorInfo.GPrePhone, dbo.TblGuarantorInfo.GResPhone, dbo.TblGuarantorInfo.GMobile, 
dbo.TblGuarantorInfo.GResStatus, dbo.TblGuarantorInfo.GPermanentAddress1, 
dbo.TblGuarantorInfo.GPermanentAddress2, dbo.TblGuarantorInfo.GID, 
ROW_NUMBER() Over (Partition By dbo.TblGuarantorInfo.AppID Order By GID, dbo.TblGuarantorInfo.GName) As Rnk 
FROM dbo.TblGuarantorInfo) 

SELECT AppID 
,MIN (Case When Rnk=1 Then GName End) As G1_Name 
,MIN (Case When Rnk=1 Then GRelationWithCustomer End) As G1_Relation 
,MIN (Case When Rnk=1 Then GFahterSpoueName end) As G1_FatherName 
,MIN (case when rnk=1 then GMotherName end) AS G1_MotherName 
,MIN (case when rnk=1 then GBusinessName end) AS G1_BusinessName 
,MIN (case when rnk=1 then GDesig end) AS G1_Desig 
,MIN (case when rnk=1 then GBusinessAddressLine1 end) AS G1_BusLine1 
,MIN (case when rnk=1 then GBusinessAddressLine2 end) AS G1_BusLine2 
,MIN (case when rnk=1 then GBusinessAddressLine3 end) AS G1_BusLine3 
,MIN (case when rnk=1 then ResidenceAddressLIne1 end) AS G1_Res1 
,MIN (case when rnk=1 then ResidenceAddressLIne2 end) AS G1_Res2 
,MIN (case when rnk=1 then ResidenceAddressLIne3 end) AS G1_Res3 
,MIN (case when rnk=1 then GPrePhone end) AS G1_PrePhone 
,MIN (case when rnk=1 then GResPhone end) AS G1_ResPhone 
,MIN (case when rnk=1 then GMobile end) AS G1_MobileNo 
,MIN (case when rnk=1 then GResStatus end) AS G1_ResStatus 
,MIN (case when rnk=1 then GPermanentAddress1 end) AS G1_PerAddress1 
,MIN (case when rnk=1 then GPermanentAddress2 end) AS G1_perAddress2 

,MIN (Case When Rnk=2 Then GName End) As G1_Name2 
,MIN (Case When Rnk=2 Then GRelationWithCustomer End) As G2_Relation 
,MIN (Case When Rnk=2 Then GFahterSpoueName end) As G2_FatherName 
,MIN (case when rnk=2 then GMotherName end) AS G2_MotherName 
,MIN (case when rnk=2 then GBusinessName end) AS G2_BusinessName 
,MIN (case when rnk=2 then GDesig end) AS G2_Desig 
,MIN (case when rnk=2 then GBusinessAddressLine1 end) AS G2_BusLine1 
,MIN (case when rnk=2 then GBusinessAddressLine2 end) AS G2_BusLine2 
,MIN (case when rnk=2 then GBusinessAddressLine3 end) AS G2_BusLine3 
,MIN (case when rnk=2 then ResidenceAddressLIne1 end) AS G2_Res1 
,MIN (case when rnk=2 then ResidenceAddressLIne2 end) AS G2_Res2 
,MIN (case when rnk=2 then ResidenceAddressLIne3 end) AS G2_Res3 
,MIN (case when rnk=2 then GPrePhone end) AS G2_PrePhone 
,MIN (case when rnk=2 then GResPhone end) AS G2_ResPhone 
,MIN (case when rnk=2 then GMobile end) AS G2_MobileNo 
,MIN (case when rnk=2 then GResStatus end) AS G2_ResStatus 
,MIN (case when rnk=2 then GPermanentAddress1 end) AS G2_PerAddress1 
,MIN (case when rnk=2 then GPermanentAddress2 end) AS G2_perAddress2 

,MIN (Case When Rnk=3 Then GName End) As G3_Name 
,MIN (Case When Rnk=3 Then GRelationWithCustomer End) As G3_Relation 
,MIN (Case When Rnk=3 Then GFahterSpoueName end) As G3_FatherName 
,MIN (case when rnk=3 then GMotherName end) AS G3_MotherName 
,MIN (case when rnk=3 then GBusinessName end) AS G3_BusinessName 
,MIN (case when rnk=3 then GDesig end) AS G3_Desig 
,MIN (case when rnk=3 then GBusinessAddressLine1 end) AS G3_BusLine1 
,MIN (case when rnk=3 then GBusinessAddressLine2 end) AS G3_BusLine2 
,MIN (case when rnk=3 then GBusinessAddressLine3 end) AS G3_BusLine3 
,MIN (case when rnk=3 then ResidenceAddressLIne1 end) AS G3_Res1 
,MIN (case when rnk=3 then ResidenceAddressLIne2 end) AS G3_Res2 
,MIN (case when rnk=3 then ResidenceAddressLIne3 end) AS G3_Res3 
,MIN (case when rnk=3 then GPrePhone end) AS G3_PrePhone 
,MIN (case when rnk=3 then GResPhone end) AS G3_ResPhone 
,MIN (case when rnk=3 then GMobile end) AS G3_MobileNo 
,MIN (case when rnk=3 then GResStatus end) AS G3_ResStatus 
,MIN (case when rnk=3 then GPermanentAddress1 end) AS G3_PerAddress1 
,MIN (case when rnk=3 then GPermanentAddress2 end) AS G3_perAddress2 
,Min(Case When Rnk=4 Then GRelationWithCustomer End) As G4_Relation 

,MIN (Case When Rnk=4 Then GName End) As G4_Name 
,MIN (Case When Rnk=4 Then GFahterSpoueName end) As G4_FatherName 
,MIN (case when rnk=4 then GMotherName end) AS G4_MotherName 
,MIN (case when rnk=4 then GBusinessName end) AS G4_BusinessName 
,MIN (case when rnk=4 then GDesig end) AS G4_Desig 
,MIN (case when rnk=4 then GBusinessAddressLine1 end) AS G4_BusLine1 
,MIN (case when rnk=4 then GBusinessAddressLine2 end) AS G4_BusLine2 
,MIN (case when rnk=4 then GBusinessAddressLine3 end) AS G4_BusLine3 
,MIN (case when rnk=4 then ResidenceAddressLIne1 end) AS G4_Res1 
,MIN (case when rnk=4 then ResidenceAddressLIne2 end) AS G4_Res2 
,MIN (case when rnk=4 then ResidenceAddressLIne3 end) AS G4_Res3 
,MIN (case when rnk=4 then GPrePhone end) AS G4_PrePhone 
,MIN (case when rnk=4 then GResPhone end) AS G4_ResPhone 
,MIN (case when rnk=4 then GMobile end) AS G4_MobileNo 
,MIN (case when rnk=4 then GResStatus end) AS G4_ResStatus 
,MIN (case when rnk=4 then GPermanentAddress1 end) AS G4_PerAddress1 
,MIN (case when rnk=4 then GPermanentAddress2 end) AS G4_perAddress2 
into #temp 
From RnkItems 
Group By AppID 

select x.*, t.* from #temp t 
inner join 
(select dbo.TblMasterInfo.AppID , dbo.TblMasterInfo.AppAsignTo, 
    dbo.TblMasterInfo.AppName, dbo.TblMasterInfo.AppLoanType, dbo.TblMasterInfo.AppLoanStatus, 
    dbo.TblClientInfo.ClFatherName, dbo.TblClientInfo.ClMotherName, dbo.TblClientInfo.ClDOB, 
    dbo.TblClientInfo.ClPrePhone, dbo.TblClientInfo.ClPreMobile, dbo.TblClientInfo.ClResidentStatus, 
    dbo.TblClientInfo.ClPreAddressLine1, dbo.TblClientInfo.ClPreAddressLine2, 
    dbo.TblClientInfo.ClPreAddressLine3, dbo.TblClientInfo.ClPreAddressLine4, 
    dbo.TblClientInfo.ClPerAddressLine1, dbo.TblClientInfo.ClPerAddressLine2, 
    dbo.TblClientInfo.ClPerAddressLine3, dbo.TblClientInfo.ClPerAddressLine4, 
    dbo.TblProfessionalInfo.ProfOccupation, dbo.TblProfessionalInfo.ProfCompanyName, 
    dbo.TblProfessionalInfo.ProfDesig, dbo.TblProfessionalInfo.ProfAddressLine1, 
    dbo.TblProfessionalInfo.ProfAddressLine2, dbo.TblProfessionalInfo.ProfAddressLine3, 
    dbo.TblProfessionalInfo.ProfAddressLine4 
    FROM dbo.TblMasterInfo INNER JOIN 
    dbo.TblClientInfo ON dbo.TblMasterInfo.AppID = dbo.TblClientInfo.AppID INNER JOIN 
    dbo.TblProfessionalInfo ON dbo.TblMasterInfo.AppID = dbo.TblProfessionalInfo.APPID 
    WHERE (dbo.TblMasterInfo.AppLoanType = 'Auto Loan') AND (dbo.TblMasterInfo.AppLoanStatus = 'Disbursed')) x 
on x.AppID = t.AppID 
+0

我使用像你:選擇Q1 *,Q2 * 從 (查詢#1報表)Q1 內部聯接上q1.AppID = q2.AppID 但沒有按 (查詢#2聲明)Q2 不工作 –

+0

你的意思是給你一個語法錯誤或不返回正確的數據? – Szymon

+0

是的,提供語法錯誤 –

0

如果你想加入兩個表使用內部聯接

對於Eample 選擇e.coloumn1,e.coloumn2,l.coloumn3來自員工e內部聯接leavedetails l on e.coloumn1 = l.coloumn3

如果你想使用

where條件

選擇e.coloumn1,e.coloumn2,從員工él.coloumn3內部聯接上e.coloumn1 = l.coloumn3其中e.coloumn1 = 'XXXX'

leavedetails升