不正确的GROUP BY / HAVING语句导致意外结果

时间:2018-01-30 18:29:55

标签: ms-access sql-server-2012 group-by having having-clause

我有以下查询未产生预期结果。我从Access查询转换了这个,并且Access查询中有一个HAVING子句,我似乎无法在我的SQL中复制。 HAVING子句包含与字段Eval Date相关的代码。我尝试将该代码添加到我的SQL中的HAVING语句中,但它在输出中没有任何区别。如何正确分组此查询?

DECLARE 
@CurrentYear smallint,
@CurrentMonth date,
@Period varchar(3),
@Division varchar(2)

SELECT
@CurrentYear = DATEPART(YY, GETDATE()),
@CurrentMonth = EOMONTH(GETDATE(), -1),
@Division = 'BT'

SELECT @Period = dd.FiscalPeriod
FROM dbo.DateDimension dd (NOLOCK)
WHERE dd.LastDayOfMonth = @CurrentMonth

SELECT
    SupplierNumberBS = REPLACE(LTRIM(REPLACE(pm.[Suppl ID], '0', ' ')), ' ', '0'),
    Period = @Period,
    FY = @CurrentYear,
    Division = @Division,
    pm.Country,
    pm.PCat,
    PMCTValidation1 = pm.[Supplier Status PMCT] + pm.[ES (Supplier)],
    pm.[Eval Date],
    pm.[End Date],
    pm.Evaluator,
    PMCTEValuationStatus =
        CASE
            WHEN pm.[Eval Date] IS NOT NULL THEN 'Completed'
            WHEN pm.[ES (Supplier)] = 'R' AND pm.[SEval (Project)] = 'R' THEN 'Overdue'
            ELSE 'Upcoming'
        END,
    PMCTStarRating =
        CASE
            WHEN pm.Score IS NULL THEN 0
            WHEN pm.Score >= 90 THEN 5
            WHEN pm.Score >= 80 THEN 4
            WHEN pm.Score > 70 THEN 3
            WHEN pm.Score >= 50 THEN 2
            ELSE 1
        END,
    pm.[Overdue Date],
    pm.[Eval ID],
    pm.[ES (Supplier)],
    bb.BranchOffice,
    bb.Zone,
    pm.[Project ID],
    pm.Score,
    pm.NCC,
    pm.[NCC Amount],
    pm.PM,
    SupplierNameBS = pm.[Suppl Name],
    pm.Comment,
    pm.[Comment Problem],
    pm.Problem,
    pm.Purchasing,
    pm.Logistics,
    pm.[Quality and EHS],
    pm.Technology,
    pm.[Devel Status],
    OpenSupplierNCC = pm.[NCC 1]-[NCC Recovered],
    pm.[NCC 1],
    pm.[NCC Recovered],
    pm.Name,
    pm.[Root Cause],
    pm.[Comment Action],
    pm.Claim
FROM
    dbo.BTPMCT pm (NOLOCK)
    LEFT JOIN dbo.BTBuyerBranch bb (NOLOCK) ON pm.[Branch Number] = bb.BranchNumber
WHERE
    pm.[Supplier Status PMCT] + pm.[ES (Supplier)] <> 'InactiveR'
AND
    pm.Country = 'CA'
AND
    pm.[Eval Date] >= DATEADD(MONTH, -14, GETDATE())
OR
    pm.[Eval Date] IS NULL
GROUP BY
    REPLACE(LTRIM(REPLACE(pm.[Suppl ID], '0', ' ')), ' ', '0'),
    pm.Country,
    pm.PCat,
    pm.[Supplier Status PMCT] + pm.[ES (Supplier)],
    pm.[Eval Date],
    pm.[End Date],
    pm.Evaluator,
    pm.[SEval (Project)],
    pm.[Overdue Date],
    pm.[Eval ID],
    pm.[ES (Supplier)],
    bb.BranchOffice,
    bb.Zone,
    pm.[Project ID],
    pm.Score,
    pm.NCC,
    pm.[NCC Amount],
    pm.PM,
    pm.[Suppl Name],
    pm.Comment,
    pm.[Comment Problem],
    pm.Problem,
    pm.Purchasing,
    pm.Logistics,
    pm.[Quality and EHS],
    pm.Technology,
    pm.[Devel Status],
    pm.[NCC 1],
    pm.[NCC Recovered],
    pm.Name,
    pm.[Root Cause],
    pm.[Comment Action],
    pm.Claim
HAVING
    pm.[Eval Date] >= DATEADD(MONTH, -14, GETDATE())
OR
   pm.[Eval Date] IS NULL
ORDER BY
    pm.[Eval Date] DESC

以下是原始的Access查询:

SELECT [qry - BT PMCT CA - A - Inactive R].[Supplier number-BS], [qry - BT PMCT CA - A - Inactive R].Period, [qry - BT PMCT CA - A - Inactive R].FY, [qry - BT PMCT CA - A - Inactive R].Division, [qry - BT PMCT CA - A - Inactive R].Country, [qry - BT PMCT CA - A - Inactive R].PCat, [qry - BT PMCT CA - A - Inactive R].[PMCT Validation 1], [qry - BT PMCT CA - A - Inactive R].[Eval Date], [qry - BT PMCT CA - A - Inactive R].[End Date], [qry - BT PMCT CA - A - Inactive R].Evaluator, [qry - BT PMCT CA - A - Inactive R].[PMCT Evaluation Status], [qry - BT PMCT CA - A - Inactive R].[PMCT Star Rating], [qry - BT PMCT CA - A - Inactive R].[Overdue Date], [qry - BT PMCT CA - A - Inactive R].[Eval ID], [qry - BT PMCT CA - A - Inactive R].[ES (Supplier)], [qry - BT PMCT CA - A - Inactive R].[Branch Office], [qry - BT PMCT CA - A - Inactive R].Zone, [qry - BT PMCT CA - A - Inactive R].[Project ID], [qry - BT PMCT CA - A - Inactive R].Score, [qry - BT PMCT CA - A - Inactive R].NCC, [qry - BT PMCT CA - A - Inactive R].[NCC Amount], [qry - BT PMCT CA - A - Inactive R].PM, [qry - BT PMCT CA - A - Inactive R].[Supplier name-BS], [qry - BT PMCT CA - A - Inactive R].Comment, [qry - BT PMCT CA - A - Inactive R].[Comment Problem], [qry - BT PMCT CA - A - Inactive R].Problem, [qry - BT PMCT CA - A - Inactive R].Purchasing, [qry - BT PMCT CA - A - Inactive R].Logistics, [qry - BT PMCT CA - A - Inactive R].[Quality and EHS], [qry - BT PMCT CA - A - Inactive R].Technology, [qry - BT PMCT CA - A - Inactive R].[Devel Status], [qry - BT PMCT CA - A - Inactive R].[Open Supplier NCC], [qry - BT PMCT CA - A - Inactive R].[NCC 1], [qry - BT PMCT CA - A - Inactive R].[NCC Recovered], [qry - BT PMCT CA - A - Inactive R].Name, [qry - BT PMCT CA - A - Inactive R].[Root Cause], [qry - BT PMCT CA - A - Inactive R].[Comment Action], [qry - BT PMCT CA - A - Inactive R].Claim
FROM [qry - BT PMCT CA - A - Inactive R]
GROUP BY [qry - BT PMCT CA - A - Inactive R].[Supplier number-BS], [qry - BT PMCT CA - A - Inactive R].Period, [qry - BT PMCT CA - A - Inactive R].FY, [qry - BT PMCT CA - A - Inactive R].Division, [qry - BT PMCT CA - A - Inactive R].Country, [qry - BT PMCT CA - A - Inactive R].PCat, [qry - BT PMCT CA - A - Inactive R].[PMCT Validation 1], [qry - BT PMCT CA - A - Inactive R].[Eval Date], [qry - BT PMCT CA - A - Inactive R].[End Date], [qry - BT PMCT CA - A - Inactive R].Evaluator, [qry - BT PMCT CA - A - Inactive R].[PMCT Evaluation Status], [qry - BT PMCT CA - A - Inactive R].[PMCT Star Rating], [qry - BT PMCT CA - A - Inactive R].[Overdue Date], [qry - BT PMCT CA - A - Inactive R].[Eval ID], [qry - BT PMCT CA - A - Inactive R].[ES (Supplier)], [qry - BT PMCT CA - A - Inactive R].[Branch Office], [qry - BT PMCT CA - A - Inactive R].Zone, [qry - BT PMCT CA - A - Inactive R].[Project ID], [qry - BT PMCT CA - A - Inactive R].Score, [qry - BT PMCT CA - A - Inactive R].NCC, [qry - BT PMCT CA - A - Inactive R].[NCC Amount], [qry - BT PMCT CA - A - Inactive R].PM, [qry - BT PMCT CA - A - Inactive R].[Supplier name-BS], [qry - BT PMCT CA - A - Inactive R].Comment, [qry - BT PMCT CA - A - Inactive R].[Comment Problem], [qry - BT PMCT CA - A - Inactive R].Problem, [qry - BT PMCT CA - A - Inactive R].Purchasing, [qry - BT PMCT CA - A - Inactive R].Logistics, [qry - BT PMCT CA - A - Inactive R].[Quality and EHS], [qry - BT PMCT CA - A - Inactive R].Technology, [qry - BT PMCT CA - A - Inactive R].[Devel Status], [qry - BT PMCT CA - A - Inactive R].[Open Supplier NCC], [qry - BT PMCT CA - A - Inactive R].[NCC 1], [qry - BT PMCT CA - A - Inactive R].[NCC Recovered], [qry - BT PMCT CA - A - Inactive R].Name, [qry - BT PMCT CA - A - Inactive R].[Root Cause], [qry - BT PMCT CA - A - Inactive R].[Comment Action], [qry - BT PMCT CA - A - Inactive R].Claim
HAVING ((([qry - BT PMCT CA - A - Inactive R].[Eval Date])>=DateAdd("m",-1,DateAdd("m",-12,Month(Now()) & "/" & [Forms]![Period Form]![txtFiscalYear])) And ([qry - BT PMCT CA - A - Inactive R].[Eval Date])<DateAdd("m",-1,DateAdd("m",-12,Month(Now()) & "/" & [Forms]![Period Form]![txtFiscalYear]))+365)) OR ((([qry - BT PMCT CA - A - Inactive R].[Eval Date]) Is Null));

0 个答案:

没有答案