2017-08-07 48 views
2

使用SQL Server,我有Where子句的两个Select语句,但不断得到重复的结果。我的第一个查询是:SQL合并两个选择具有不同列号的查询,同时删除重复项?

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)' 而不是 'LEFT JOIN电传ON fakPricingContingencyFK = pcoID)':

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×2])。我如何组合它们,以便得到正确的8行(2 + 6)?

在此先感谢。

+1

你基本上是在寻找一个UNION查询。但是,UNION(或其更具包容性的同级UNION ALL)要求两个查询的列数完全相同,并且每列中的数据类型大致相同。除非PalletPricing和Fak具有相同的表格布局,否则您必须在某些时候返回假数据,并且将两个来源中的每个来源的列名设置为您从顶级查询输出的任何列名称。 –

+0

他们真的是重复的吗?可能至少有一列不同。 – Parfait

+0

你是对的,一列是不同的。如何排除一个不同的列,以便整个行不包含在查询中? – smac

回答

0

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

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

当使用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 

这仅仅是为你的两个字段匹配明显的例子,你必须去列逐列。

+0

不幸的是,列不匹配。 FAK表有6列,PalletPricing表有9列。我尝试在第一个查询中为UNION放置额外3列的NULL,但结果是FAK表列中的PalletPricing数据不正确,因为第一个查询具有这些列标题。 – smac

0

你必须把“union all”而不是inner join。

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 
相关问题