为什么我为以下SQL查询获取重复记录

时间:2012-11-11 14:16:00

标签: sql-server-2005

我有以下查询:

SELECT 
    l.ModelTypeIndexNumber AS [Model Index Number],
    l.TestGroupname AS [Test Group],
    lf.CE_Fuel AS [Fuel],
    CASE 
        WHEN lf.Electricity = '' THEN '(missing)' \
        ELSE  lf.Electricity 
    END AS [Electric Motor Energy Source],
    tg.TG_Fuel + ', EL' AS [TG Fuel Sources],
    CASE 
        WHEN l.LabelCalculationApproachIdentifier ='PHEV' THEN  'PHEV'
        ELSE '(Not PHEV)' 
    END AS [Label Calculation Approach] 
FROM Relational.Label l 
INNER JOIN (
    SELECT 
        lbl.SubmissionId, 
        lbl.ModelTypeIndexNumber, 
        COUNT(*) AS lblFuelCount, 
        MAX(CASE 
                WHEN fu.FuelUsageIdentifier <>'EL' THEN fu.FuelUsageIdentifier 
                ELSE NULL 
            END) AS CE_Fuel, 
        MAX(CASE 
                WHEN fu.FuelUsageIdentifier ='EL' THEN fu.FuelUsageIdentifier 
                ELSE '' 
            END) AS Electricity 
    FROM Relational.Label lbl 
    INNER JOIN Relational.FuelUsage fu 
        ON lbl.LabelId=fu.LabelId 
    WHERE lbl.SubmissionId=@SubmissionId 
    GROUP BY lbl.SubmissionId, lbl.ModelTypeIndexNumber 
    ) AS lf 
    ON l.SubmissionId=lf.SubmissionId 
INNER JOIN (
    SELECT 
        rtg.SubmissionId, 
        rtg.TestGroupName, 
        MAX(CASE 
            WHEN ds.FuelId <> 'EL' THEN ds.FuelId 
            ELSE NULL 
        END) AS TG_Fuel 
    FROM Relational.DriveSource ds 
    INNER JOIN Relational.TestGroup rtg 
        ON ds.TestGroupId=rtg.TestGroupId 
    WHERE rtg.SubmissionId=@SubmissionId 
    GROUP BY rtg.SubmissionId, rtg.TestGroupName 
    HAVING COUNT(*)>1 AND MAX(CASE WHEN ds.FuelId ='EL' THEN 1 ELSE 0 END) = 1
    ) AS tg 
    ON lf.SubmissionId=tg.SubmissionId 
    AND l.TestGroupName = tg.TestGroupName 
WHERE (lf.Electricity = '' AND l.LabelCalculationApproachIdentifier = 'PHEV') 

它正在生成以下输出:

Model Index Number    Test Group    Fuel    TG Fuel Source(s)    Label Calculation Approach   
19                    ABC.1987      GP      DX, EL               PHEV
19                    XYZ.1989      DM      DS, EL               PHEV
20                    ABC.1987      GP      DX. EL               PHEV
20                    XYZ.1989      DM      DS, EL               PHEV

但我期待以下结果:

Model Index Number    Test Group    Fuel    TG Fuel Source(s)    Label Calculation Approach
19                    ABC.1987      GP      DX, EL               PHEV
20                    XYZ.1989      DM      DS, EL               PHEV

我不想复制。也许加入问题。我试过DISTINCT。我在查询中做错了什么?

1 个答案:

答案 0 :(得分:0)

  

为什么我会获得重复记录?

这个问题的答案在很大程度上取决于源表中的数据。


这只是一个猜测(因为我没有你真正的表和数据我无法运行查询):

我认为您在查询结尾处也需要GROUP BY语句。您的查询可能如下所示:

SELECT 
    l.ModelTypeIndexNumber AS [Model Index Number],
    l.TestGroupname AS [Test Group],
    MAX(lf.CE_Fuel AS [Fuel]),
    MAX(CASE 
        WHEN lf.Electricity = '' THEN '(missing)' \
        ELSE  lf.Electricity 
    END) AS [Electric Motor Energy Source],
    MAX(tg.TG_Fuel + ', EL') AS [TG Fuel Sources],
    MAX(CASE 
        WHEN l.LabelCalculationApproachIdentifier ='PHEV' THEN  'PHEV'
        ELSE '(Not PHEV)' 
    END) AS [Label Calculation Approach] 
FROM Relational.Label l 
INNER JOIN (
    SELECT 
        lbl.SubmissionId, 
        lbl.ModelTypeIndexNumber, 
        COUNT(*) AS lblFuelCount, 
        MAX(CASE 
                WHEN fu.FuelUsageIdentifier <>'EL' THEN fu.FuelUsageIdentifier 
                ELSE NULL 
            END) AS CE_Fuel, 
        MAX(CASE 
                WHEN fu.FuelUsageIdentifier ='EL' THEN fu.FuelUsageIdentifier 
                ELSE '' 
            END) AS Electricity 
    FROM Relational.Label lbl 
    INNER JOIN Relational.FuelUsage fu 
        ON lbl.LabelId=fu.LabelId 
    WHERE lbl.SubmissionId=@SubmissionId 
    GROUP BY lbl.SubmissionId, lbl.ModelTypeIndexNumber 
    ) AS lf 
    ON l.SubmissionId=lf.SubmissionId 
INNER JOIN (
    SELECT 
        rtg.SubmissionId, 
        rtg.TestGroupName, 
        MAX(CASE 
            WHEN ds.FuelId <> 'EL' THEN ds.FuelId 
            ELSE NULL 
        END) AS TG_Fuel 
    FROM Relational.DriveSource ds 
    INNER JOIN Relational.TestGroup rtg 
        ON ds.TestGroupId=rtg.TestGroupId 
    WHERE rtg.SubmissionId=@SubmissionId 
    GROUP BY rtg.SubmissionId, rtg.TestGroupName 
    HAVING COUNT(*)>1 AND MAX(CASE WHEN ds.FuelId ='EL' THEN 1 ELSE 0 END) = 1
    ) AS tg 
    ON lf.SubmissionId=tg.SubmissionId 
    AND l.TestGroupName = tg.TestGroupName 
WHERE (lf.Electricity = '' AND l.LabelCalculationApproachIdentifier = 'PHEV') 
GROUP BY     
    l.ModelTypeIndexNumber,
    l.TestGroupname