SQL对JOIN使用两个查询

时间:2019-03-14 11:11:05

标签: sql sql-server tsql

我对SQL相当陌生。我试图使用两个查询并将它们结合在一起以获得我想要的结果:

maincircle

我的错误是

  

信息8156,第16层,状态1,第107行   为“ Q1”多次指定了“名称”列。

两个查询单独工作都很好。问题是当我将他们聚在一起时。如您所见,我在[Designation]中输入了两次,但一次是从T1,一次是从T4。

我想念什么?

4 个答案:

答案 0 :(得分:0)

您可以在下面尝试-您有两个指定列,一个是T1。[名称],另一个是T4。[名称]-一个必须重命名

select 
       A.[Article:Link]
      ,A.[ArtNo]
      ,A.[GenericArticle:Link]
      ,A.[Vehicle:Link]
      ,A.[LinkingTargetNo]
      ,Q1.[Name]
FROM [HellaPim].[dbo].[TecDoc.ArticleLinkage.Linkages] A WITH (NOLOCK) 
LEFT OUTER JOIN 
(SELECT 
       base.[TecDocNo], 
       base.[VehicleType],
       base.[TypeNo],
       T3.[Manufacturer:Link], 
       T24.[Name], 
       T1.[Designation] as designation1, 
       T1.[LongDesignation], 
       T2.[DescriptionNo], 
       base.[Model:Link], 
       T4.[Designation], 
       base.[SortNo], 
       base.[ConstructionYearFrom], 
       base.[ConstructionYearTo], 
       base.[KW], 
       base.[PS], 
       base.[CcmTaxation], 
       base.[CcmTechnical], 
       base.[CapacityLitres], 
       base.[NumCylinders], 
       base.[NumDoors], 
       base.[FuelTankCapacity], 
       base.[MainVoltage], 
       base.[ABS], 
       base.[ASR], 
       base.[EngineType:Link], 
       T6.[Description <4>], 
       base.[FuelMixtureFormation:Link], 
       T8.[Description <4>], 
       base.[DriveType:Link], 
       T10.[Description <4>],  
       base.[BrakeType:Link], 
       T12.[Description <4>], 
       base.[BrakeSystem:Link], 
       T14.[Description <4>], 
       base.[NumValves], 
       base.[FuelType:Link], 
       T16.[Description <4>], 
       base.[CatalystConverterType:Link], 
       T18.[Description <4>], 
       base.[TransmissionType:Link], 
       T20.[Description <4>], 
       base.[BodyType:Link], 
       T22.[Description <4>]
FROM [HellaPim].[dbo].[TecDoc.LinkingTargets.Types] base WITH (NOLOCK)

  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models] T3 WITH (NOLOCK) 
               ON T3.[ModelNo] = [base].[Model:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers] T23 WITH (NOLOCK) 
               ON T23.[ManufacturerNo] = [T3].[Manufacturer:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers <TecDoc.GeneralData.Countries>] T24 WITH (NOLOCK) 
               ON T24.[:Id] = [T23].[:Id] 
               AND T24.[:TecDoc.GeneralData.Countries_Id] = 'A98E18B0-1401-4104-9EF9-827497E6407F'
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Types <TecDoc.GeneralData.Countries>] T1 WITH (NOLOCK) 
               ON T1.[:Id] = [base].[:Id] 
               AND T1.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.CouLngDescriptions] T2 WITH (NOLOCK) 
               ON T2.[DescriptionNo] = [base].[Description:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models <TecDoc.GeneralData.Countries>] T4 WITH (NOLOCK) 
               ON T4.[:Id] = [T3].[:Id] 
               AND T4.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T5 WITH (NOLOCK) 
               ON T5.[KeyValueNo] = [base].[EngineType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T6 WITH (NOLOCK) 
               ON T6.[DescriptionNo] = [T5].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T7 WITH (NOLOCK) 
               ON T7.[KeyValueNo] = [base].[FuelMixtureFormation:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T8 WITH (NOLOCK) 
               ON T8.[DescriptionNo] = [T7].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T9 WITH (NOLOCK) 
               ON T9.[KeyValueNo] = [base].[DriveType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T10 WITH (NOLOCK) 
               ON T10.[DescriptionNo] = [T9].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T11 WITH (NOLOCK) 
               ON T11.[KeyValueNo] = [base].[BrakeType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T12 WITH (NOLOCK) 
               ON T12.[DescriptionNo] = [T11].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T13 WITH (NOLOCK) 
               ON T13.[KeyValueNo] = [base].[BrakeSystem:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T14 WITH (NOLOCK) 
               ON T14.[DescriptionNo] = [T13].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T15 WITH (NOLOCK) 
               ON T15.[KeyValueNo] = [base].[FuelType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T16 WITH (NOLOCK) 
               ON T16.[DescriptionNo] = [T15].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T17 WITH (NOLOCK) 
               ON T17.[KeyValueNo] = [base].[CatalystConverterType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T18 WITH (NOLOCK) 
               ON T18.[DescriptionNo] = [T17].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T19 WITH (NOLOCK) 
               ON T19.[KeyValueNo] = [base].[TransmissionType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T20 WITH (NOLOCK) 
               ON T20.[DescriptionNo] = [T19].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T21 WITH (NOLOCK) 
               ON T21.[KeyValueNo] = [base].[BodyType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T22 WITH (NOLOCK) 
               ON T22.[DescriptionNo] = [T21].[Name:Link]
) as Q1 on A.[LinkingTargetNo] = Q1.[TypeNo]

答案 1 :(得分:0)

使用别名进行命名

select 
       A.[Article:Link]
      ,A.[ArtNo]
      ,A.[GenericArticle:Link]
      ,A.[Vehicle:Link]
      ,A.[LinkingTargetNo]
      ,Q1.[Name]
FROM [HellaPim].[dbo].[TecDoc.ArticleLinkage.Linkages] A WITH (NOLOCK) 
LEFT OUTER JOIN 
(SELECT 
       base.[TecDocNo], 
       base.[VehicleType],
       base.[TypeNo],
       T3.[Manufacturer:Link], 
       T24.[Name], 
       T1.[Designation] as DesignationT1 , 
       T1.[LongDesignation], 
       T2.[DescriptionNo], 
       base.[Model:Link], 
       T4.[Designation], 
       base.[SortNo], 
       base.[ConstructionYearFrom], 
       base.[ConstructionYearTo], 
       base.[KW], 
       base.[PS], 
       base.[CcmTaxation], 
       base.[CcmTechnical], 
       base.[CapacityLitres], 
       base.[NumCylinders], 
       base.[NumDoors], 
       base.[FuelTankCapacity], 
       base.[MainVoltage], 
       base.[ABS], 
       base.[ASR], 
       base.[EngineType:Link], 
       T6.[Description <4>], 
       base.[FuelMixtureFormation:Link], 
       T8.[Description <4>], 
       base.[DriveType:Link], 
       T10.[Description <4>],  
       base.[BrakeType:Link], 
       T12.[Description <4>], 
       base.[BrakeSystem:Link], 
       T14.[Description <4>], 
       base.[NumValves], 
       base.[FuelType:Link], 
       T16.[Description <4>], 
       base.[CatalystConverterType:Link], 
       T18.[Description <4>], 
       base.[TransmissionType:Link], 
       T20.[Description <4>], 
       base.[BodyType:Link], 
       T22.[Description <4>]
FROM [HellaPim].[dbo].[TecDoc.LinkingTargets.Types] base WITH (NOLOCK)

  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models] T3 WITH (NOLOCK) 
               ON T3.[ModelNo] = [base].[Model:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers] T23 WITH (NOLOCK) 
               ON T23.[ManufacturerNo] = [T3].[Manufacturer:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers <TecDoc.GeneralData.Countries>] T24 WITH (NOLOCK) 
               ON T24.[:Id] = [T23].[:Id] 
               AND T24.[:TecDoc.GeneralData.Countries_Id] = 'A98E18B0-1401-4104-9EF9-827497E6407F'
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Types <TecDoc.GeneralData.Countries>] T1 WITH (NOLOCK) 
               ON T1.[:Id] = [base].[:Id] 
               AND T1.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.CouLngDescriptions] T2 WITH (NOLOCK) 
               ON T2.[DescriptionNo] = [base].[Description:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models <TecDoc.GeneralData.Countries>] T4 WITH (NOLOCK) 
               ON T4.[:Id] = [T3].[:Id] 
               AND T4.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T5 WITH (NOLOCK) 
               ON T5.[KeyValueNo] = [base].[EngineType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T6 WITH (NOLOCK) 
               ON T6.[DescriptionNo] = [T5].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T7 WITH (NOLOCK) 
               ON T7.[KeyValueNo] = [base].[FuelMixtureFormation:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T8 WITH (NOLOCK) 
               ON T8.[DescriptionNo] = [T7].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T9 WITH (NOLOCK) 
               ON T9.[KeyValueNo] = [base].[DriveType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T10 WITH (NOLOCK) 
               ON T10.[DescriptionNo] = [T9].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T11 WITH (NOLOCK) 
               ON T11.[KeyValueNo] = [base].[BrakeType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T12 WITH (NOLOCK) 
               ON T12.[DescriptionNo] = [T11].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T13 WITH (NOLOCK) 
               ON T13.[KeyValueNo] = [base].[BrakeSystem:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T14 WITH (NOLOCK) 
               ON T14.[DescriptionNo] = [T13].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T15 WITH (NOLOCK) 
               ON T15.[KeyValueNo] = [base].[FuelType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T16 WITH (NOLOCK) 
               ON T16.[DescriptionNo] = [T15].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T17 WITH (NOLOCK) 
               ON T17.[KeyValueNo] = [base].[CatalystConverterType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T18 WITH (NOLOCK) 
               ON T18.[DescriptionNo] = [T17].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T19 WITH (NOLOCK) 
               ON T19.[KeyValueNo] = [base].[TransmissionType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T20 WITH (NOLOCK) 
               ON T20.[DescriptionNo] = [T19].[Name:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T21 WITH (NOLOCK) 
               ON T21.[KeyValueNo] = [base].[BodyType:Link]
  LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T22 WITH (NOLOCK) 
               ON T22.[DescriptionNo] = [T21].[Name:Link]
) as Q1 on A.[LinkingTargetNo] = Q1.[TypeNo]

答案 2 :(得分:0)

您的SELECT查询具有T1.Designate和T4.Designate,这可能是问题所在。也许尝试向其中之一添加别名,例如。 T4。指定AS指定4。

答案 3 :(得分:0)

请为列表中的每个重复列设置别名

像这样

T4.[Designation] As T4Designation,
T6.[Description <4>] As T6Description,

以此类推。