如何加入两个查询

时间:2013-10-07 10:13:43

标签: sql-server

如何加入以下两个查询:

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')

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果要加入两个表,请使用内连接

对于例子 选择e.coloumn1,e.coloumn2,l.coloumn3来自员工e inner join leavedetails l on e.coloumn1 = l.coloumn3

如果你想使用where where

从e.coloumn1 = l.coloumn3的员工e内联接leavedetails l中选择e.coloumn1,e.coloumn2,l.coloumn3,其中e.coloumn1 ='xxxx'