SQL是否在删除重复项的同时合并两个列号不同的Select查询?

SQL是否在删除重复项的同时合并两个列号不同的Select查询?,sql,sql-server,select,inner-join,where-clause,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 INNE

使用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
并返回两行(这是正确的)

我的第二个查询非常类似,但有额外的列(唯一的区别是“PPrpricingContentingCiesFK=pcoID上的左连接托盘定价”,而不是“FakPricingTorrencyFK=pcoID上的左连接托盘定价”):

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

我如何将它们组合在一起,以便总共得到8行?如果我使用内部联接将它们组合起来,如:

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)


提前感谢。

您想要的
内部连接将无效。使用像当前代码那样的连接将把每个查询中的所有列塞进非常宽的行中。您需要
UNION
(而不是
UNION ALL
,因为它专门用于不检查DUP)

由于您收到的注释提到,在每个查询中必须有相同数量的字段,并且它们应该包含相同的数据,否则,根据它们来自哪个查询,您的字段将有两种不同类型的数据

当使用
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
这只是两个明显匹配的字段的一个示例,您必须逐列进行操作。

您必须放置“union all”而不是内部联接

 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

您基本上是在寻找联合查询。但是,UNION(或其更具包容性的同级UNION ALL)要求两个查询的列数完全相同,每列中的数据类型大致相同。除非PalletPricing和FAK具有相同的表布局,否则您将不得不在某些时候返回假数据,并且这两个源中的每一个的列名都将设置为您从顶部查询输出的任何列名。它们真的是重复的吗?可能至少有一列不同。您是正确的,一列不同。如何排除一个不同的列,使整个行不包括在查询中?很遗憾,这些列不匹配。FAK表有6列,托盘定价表有9列。我尝试在第一个查询中为额外的3列执行一个联合,并将其置为null,但结果导致FAK表的列中的PalletPricing数据不正确,因为第一个查询具有这些列标题。
 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