从连接表/视图中对列进行求和会导致值加倍

时间:2014-04-02 19:12:36

标签: sql sql-server

我刚刚开始为一家公司工作一周,他们在我开始构建他们的新系统之前让我纠正了他们的一些数据库问题。他们的SQL服务器绑定并访问前端,当我们运行报告时,我发现某些字段的值正在加倍。特别是每个字段都说“' SUM'。

现在在挖掘了36个视图以找到有问题的代码之后,我发现了错误发生的地方,但我真的不知道如何解决这个问题。我已经尝试格式化代码以使其更容易阅读,但坦率地说,在这个可怕的混乱的页面一页一页地盯着我后,我的头脑变得麻木了。

这是查询,每个使用sum的列都是重复的。

SELECT DISTINCT  Equip.[CMS Dump Table Current].Date, 
'CDC' AS Name, Equip.[CMS Dump Table Current].Mod, 
Equip.[CMS Dump Table Current].Cell, 
Equip.[CMS Dump Table Current].Row, 
BusDuctQueries.CMSMigrated.Section, 
ISNULL(SUM(BusDuctQueries.CMSMigrated.[Phase A Breaker Amps]), 0) AS [Sum Of Phase A Breaker Amps], 
ISNULL(SUM(BusDuctQueries.CMSMigrated.[Phase B Breaker Amps]), 0) AS [Sum Of Phase B Breaker Amps], 
ISNULL(SUM(BusDuctQueries.CMSMigrated.[Phase C Breaker Amps]), 0) AS [Sum Of Phase C Breaker Amps], 
ISNULL(ROUND(SUM(BusDuctQueries.CMSMigrated.[Phase A Actual Amps]), 2), 0) AS [Sum Of Phase A Actual Amps], 
ISNULL(ROUND(SUM(BusDuctQueries.CMSMigrated.[Phase B Actual Amps]), 2), 0)  AS [Sum Of Phase B Actual Amps], 
ISNULL(ROUND(SUM(BusDuctQueries.CMSMigrated.[Phase C Actual Amps]), 2), 0) AS [Sum Of Phase C Actual Amps], 
Equip.[CMS Dump Table Current].[Bus Duct # 1], 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase A Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase B Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase C Current Load], 
ISNULL(Equip.PDU.PDU, N'N/A') AS [PDU # 2], 
Equip.[CMS Dump Table Current].[Bus Duct # 2], 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase A Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase B Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase C Current Load], 
ROUND((Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase A Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase B Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase C Current Load] +
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase A Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase B Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase C Current Load])/ 3.00, 2) AS [Average Total Load], 
ROUND(((Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase A Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase B Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase C Current Load])
/ 3 + (Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase A Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase B Current Load] + 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase C Current Load])
/ 3.00) * Equip.Panel.HighVoltage * 1.732 * 0.99 / 1000.00, 2) AS [Total KW Used], 
Equip.Panel.LowVoltage, Equip.Panel.HighVoltage, Equip.Panel.KWAllotment * 2 AS KWAllotment, 
Equip.Panel.KWRating * 2 AS MaxKW
FROM   Equip.PDU 
RIGHT OUTER JOIN
Equip.Panel 
INNER JOIN Equip.[CMS Dump Table Current] 
ON Equip.Panel.Panel = Equip.[CMS Dump Table Current].[Bus Duct # 2]
LEFT OUTER JOIN BusDuctQueries.CMSMigrated 
ON Equip.[CMS Dump Table Current].[Bus Duct # 1] = BusDuctQueries.CMSMigrated.[New RDC 1] 
ON Equip.PDU.PDUID = Equip.Panel.PDUID
GROUP BY Equip.[CMS Dump Table Current].Date, Equip.[CMS Dump Table Current].Mod, 
Equip.[CMS Dump Table Current].Row, BusDuctQueries.CMSMigrated.Section, 
Equip.[CMS Dump Table Current].[Bus Duct # 1], 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase A Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase B Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 1 Phase C Current Load], 
Equip.PDU.PDU, Equip.[CMS Dump Table Current].[Bus Duct # 2], 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase A Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase B Current Load], 
Equip.[CMS Dump Table Current].[Bus Duct # 2 Phase C Current Load], 
Equip.[CMS Dump Table Current].Cell, Equip.Panel.LowVoltage, 
Equip.Panel.HighVoltage, Equip.Panel.KWAllotment, 
Equip.Panel.KWRating
HAVING (Equip.[CMS Dump Table Current].[Bus Duct # 1] LIKE N'%RDC%') AND 
(Equip.[CMS Dump Table Current].[Bus Duct # 1] NOT LIKE N'%SSAB%') AND 
(Equip.[CMS Dump Table Current].Mod = 'D2')

此外,如果有一种简单的方法将其移植到存储过程中,这样我就可以开始远离这些荒谬的视图,那么任何帮助都会受到赞赏。该应用程序包含近20个互连的数据库,仅在该数据库中就有近350个视图。

编辑为了进一步说明,我的表中[阶段A实际电流]的值应该加起来为173.2,但是,它们返回为346.40。现在,当我只查询' BusDuctQueries.CMSMigrated'表,并对值求和,得到173.3的正确数据。

1 个答案:

答案 0 :(得分:0)

经过一些研究和测试,我发现我的错误实际上是两次。首先,应用程序查询的表包含重复值(这将在前端处理)。但是,即使存在重复值,SELECT DISTINCT标记也应该捕获它们。在将聚合分成单独的视图之后,而不是将它们聚集在一起,获得了正确的结果。

正确的代码如下:

USE [Bus Duct Report]
SELECT DISTINCT 
    cmsd.[Date],
    'CDC' AS Name,
    cmsd.[Mod],
    cmsd.[Cell],
    cmsd.[Row],
    cmsm.Section,
    cmsm.[Sum Of Phase A Breaker Amps],
    cmsm.[Sum Of Phase B Breaker Amps],
    cmsm.[Sum Of Phase C Breaker Amps],
    cmsm.[Sum Of Phase A Actual Amps],
    cmsm.[Sum Of Phase B Actual Amps],
    cmsm.[Sum Of Phase C Actual Amps],
    cmsd.[Bus Duct # 1],
    cmsd.[Bus Duct # 1 Phase A Current Load],
    cmsd.[Bus Duct # 1 Phase B Current Load],
    cmsd.[Bus Duct # 1 Phase C Current Load],
    ISNULL(pdu.PDU,N'N/A') AS [PDU # 2],
    cmsd.[Bus Duct # 2],
    cmsd.[Bus Duct # 2 Phase A Current Load],
    cmsd.[Bus Duct # 2 Phase B Current Load],
    cmsd.[Bus Duct # 2 Phase C Current Load],
    ROUND(([Bus Duct # 1 Phase A Current Load] + [Bus Duct # 1 Phase B Current Load] +
        [Bus Duct # 1 Phase C Current Load] + [Bus Duct # 2 Phase A Current Load] +
        [Bus Duct # 2 Phase B Current Load] + [Bus Duct # 2 Phase C Current Load]) / 3.00,2) AS
        [Average Total Load],
    ROUND(([Bus Duct # 1 Phase A Current Load] + [Bus Duct # 1 Phase B Current Load] +
        [Bus Duct # 1 Phase C Current Load] + [Bus Duct # 2 Phase A Current Load] +
        [Bus Duct # 2 Phase B Current Load] + [Bus Duct # 2 Phase C Current Load]) / 3.00
        * pnl.HighVoltage * 1.732 * 00.99 / 1000,2) AS [Total KW Used],
    pnl.LowVoltage,
    pnl.HighVoltage,
    pnl.KWAllotment * 2 AS [KWAllotment],
    pnl.KWRating * 2 AS [MaxKW]
FROM Equip.[CMS Dump Table Current] cmsd
INNER JOIN Equip.Panel pnl ON pnl.Panel = cmsd.[Bus Duct # 2]
JOIN Equip.PDU pdu ON pdu.PDUID = pnl.PDUID 
LEFT OUTER JOIN (
    SELECT [New RDC 1], Section,
        ISNULL(SUM([Phase A Breaker Amps]),0) AS [Sum Of Phase A Breaker Amps],
        ISNULL(SUM([Phase B Breaker Amps]),0) AS [Sum Of Phase B Breaker Amps],
        ISNULL(SUM([Phase C Breaker Amps]),0) AS [Sum Of Phase C Breaker Amps],
        ISNULL(SUM([Phase A Actual Amps]),0) AS [Sum Of Phase A Actual Amps],
        ISNULL(SUM([Phase B Actual Amps]),0) AS [Sum Of Phase B Actual Amps],
        ISNULL(SUM([Phase C Actual Amps]),0) AS [Sum Of Phase C Actual Amps]
    FROM BusDuctQueries.CMSMigrated
    GROUP BY [New RDC 1],Section) cmsm
    ON cmsd.[Bus Duct # 1] = cmsm.[New RDC 1]
WHERE   cmsd.[Bus Duct # 1] LIKE N'%RDC%' AND
        cmsd.[Bus Duct # 1] NOT LIKE N'%SSAB%';