SQL Merge两个选择具有不同列号的查询,同时删除重复项?

时间:2017-08-07 21:46:53

标签: sql sql-server select inner-join where-clause

使用SQL Server,我有两个带有Where子句的Select语句,但不断获得重复的结果。我的第一个问题是:

  SELECT
    PricingContingencies.*,
    Terms.*,
    rscDescription  AS RateScheme,
    ptyAbbreviation AS PointTypeAbbreviation,
    FAKs.*,
    fat.*
  FROM ((((PricingContingencies
    INNER JOIN Terms ON pcoTerms = terID)
    INNER JOIN PointTypes ON pcoPointTypeFK = ptyID)
    LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID)
    LEFT JOIN FAKs ON fakPricingContingencyFK = pcoID)
    LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK
  WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608

并返回两行(这是正确的)。

我的第二个查询非常相似,但有其他列(唯一的区别是' LEFT JOIN PalletPricing ON pprPricingContingenciesFK = pcoID)'取而代之的是“fakPricingContingencyFK = pcoID”中的“JOFT JOIN FAK”':

SELECT
  PricingContingencies.*,
  Terms.*,
  rscDescription as RateScheme,
  ptyAbbreviation as PointTypeAbbreviation,
  PalletPricing.*,
  fat.*
FROM ((((PricingContingencies
  INNER JOIN Terms ON pcoTerms = terID)
  INNER JOIN PointTypes ON pcoPointTypeFK = ptyID)
  LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID)
  LEFT JOIN PalletPricing ON pprPricingContingenciesFK = pcoID)
  LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK
WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608

并返回6行(这也是正确的)。

如何将它们组合在一起,总共得到8行?如果我使用INNER JOIN组合它们,如:

SELECT
  FirstSet.*,
  SecondSet.*
FROM (
  SELECT
    PricingContingencies.*,
    Terms.*,
    rscDescription  AS RateScheme,
    ptyAbbreviation AS PointTypeAbbreviation,
    FAKs.*,
    fat.*
  FROM ((((PricingContingencies
    INNER JOIN Terms ON pcoTerms = terID)
    INNER JOIN PointTypes ON pcoPointTypeFK = ptyID)
    LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID)
    LEFT JOIN FAKs ON fakPricingContingencyFK = pcoID)
    LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK
  WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608
) as FirstSet
INNER JOIN
  (
    SELECT
      PricingContingencies.*,
      Terms.*,
      rscDescription as RateScheme,
      ptyAbbreviation as PointTypeAbbreviation,
      PalletPricing.*,
      fat.*
    FROM ((((PricingContingencies
      INNER JOIN Terms ON pcoTerms = terID)
      INNER JOIN PointTypes ON pcoPointTypeFK = ptyID)
      LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID)
      LEFT JOIN PalletPricing ON pprPricingContingenciesFK = pcoID)
      LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK
    WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608
    ) as SecondSet
ON FirstSet.pcoID = SecondSet.pcoID
ORDER BY FirstSet.pcoPriority DESC

我得到12行,其中PalletPricing列重复且不正确(第二个结果加倍[6 x 2])。我如何组合它们以便我得到正确的8行(2 + 6)?

提前致谢。

2 个答案:

答案 0 :(得分:0)

您希望UNION无法正常工作。像当前代码一样使用连接会将每个查询中的所有列填充到非常宽的行中。您需要UNION ALL(不是UNION,因为它专门用于检查重复项。)

正如您提到的评论一样,每个查询中必须包含相同数量的字段,并且它们应包含等效数据,否则您的字段将具有两种不同类型的数据,具体取决于它们来自哪个查询。

使用.*时,明确指定字段也是一个好主意,SELECT rscDescription AS RateScheme, ptyAbbreviation AS PointTypeAbbreviation, FROM ((((PricingContingencies INNER JOIN Terms ON pcoTerms = terID) INNER JOIN PointTypes ON pcoPointTypeFK = ptyID) LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID) LEFT JOIN FAKs ON fakPricingContingencyFK = pcoID) LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608 UNION SELECT rscDescription as RateScheme, ptyAbbreviation as PointTypeAbbreviation, FROM ((((PricingContingencies INNER JOIN Terms ON pcoTerms = terID) INNER JOIN PointTypes ON pcoPointTypeFK = ptyID) LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID) LEFT JOIN PalletPricing ON pprPricingContingenciesFK = pcoID) LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608 是一种冒险的方式,因为您认为所有列都在相关表格中的相同顺序。所以你可以这样做:

:=

这只是两个明显匹配的字段的示例,您必须逐列。

答案 1 :(得分:0)

你必须把“联合所有”而不是内连接。

 SELECT
    PricingContingencies.*,
    Terms.*,
    rscDescription  AS RateScheme,
    ptyAbbreviation AS PointTypeAbbreviation,
    FAKs.*,
    fat.*
  FROM ((((PricingContingencies
    INNER JOIN Terms ON pcoTerms = terID)
    INNER JOIN PointTypes ON pcoPointTypeFK = ptyID)
    LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID)
    LEFT JOIN FAKs ON fakPricingContingencyFK = pcoID)
    LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK
  WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608
union all
    SELECT
      PricingContingencies.*,
      Terms.*,
      rscDescription as RateScheme,
      ptyAbbreviation as PointTypeAbbreviation,
      PalletPricing.*,
      fat.*
    FROM ((((PricingContingencies
      INNER JOIN Terms ON pcoTerms = terID)
      INNER JOIN PointTypes ON pcoPointTypeFK = ptyID)
      LEFT JOIN RateSchemes ON pcoRateSchemeFK = rscID)
      LEFT JOIN PalletPricing ON pprPricingContingenciesFK = pcoID)
      LEFT JOIN FuelAgreementTypes fat ON ftyID = pcoFuelAgreementTypeFK
    WHERE pcoPricingAgreementFK = 61523 and pcoID = 16490608