2013-10-07 70 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升