SQL新手-如何比较两组联接表中的数据

时间:2019-01-23 16:05:19

标签: sql

我正在尝试将一组多个表与SQL数据库中的另一组多个表进行比较。诚然,我是SQL的新手,所以如果我的术语不正确,请原谅我。

我的数据库中有6个单独的表。其中的3个包含本月的数据,其他三个包含上个月的数据。我需要生成一个显示当前月份数据的报告,还要将帐户余额与上个月进行比较-我需要创建一列,根据帐户余额是否匹配来列出“ true”或“ false”。

我将当前三个月的数据表合并在一起,并将前三个月的数据表合并在一起。

两组表是相同的。下面是对这些表的说明-有一组以“ CURRENT”为前缀的前缀和一组以“ PRIOR”为前缀的前缀:

BALANCE:
    EntityID
    AccountID
    AccountBalance

ENTITY:
    ID
    Label
    Description

ACCOUNT:
    ID
    AccountNumber
    Description

我需要提供的报告应从当月数据中列出以下内容:

Label     
Description 
AccountNumber   
AccountDescription  
AccountBalance  

我需要在报告的末尾添加一个名为“ 已更改”的列。此列的值应为“ True”或“ False”,具体取决于当前和先前的帐户余额是否匹配。

到目前为止,我只能加入表格。我不确定如何编辑此查询以将当月的dbo.CURRENT_BALANCE.AccountBalance与上个月的dbo.PRIOR_BALANCE.AccountBalance进行比较

SELECT DISTINCT 
    dbo.CURRENT_ENTITY.Label, 
    dbo.CURRENT_ENTITY.Description AS Entity, 
    dbo.CURRENT_ACCOUNT.AccountNumber, 
    dbo.CURRENT_ACCOUNT.Description AS AccountDescription, 
    dbo.CURRENT_BALANCE.GLAccountBalance
FROM     
    dbo.CURRENT_BALANCE 
    INNER JOIN dbo.CURRENT_ENTITY ON dbo.CURRENT_BALANCE.EntityID = dbo.CURRENT_ENTITY.ID 
    INNER JOIN dbo.CURRENT_ACCOUNT ON dbo.CURRENT_BALANCE.AccountID = dbo.CURRENT_ACCOUNT.ID 
    CROSS JOIN dbo.PRIOR_BALANCE 
    INNER JOIN dbo.PRIOR_ENTITY ON dbo.PRIOR_BALANCE.EntityID = dbo.PRIOR_ENTITY.ID 
    INNER JOIN dbo.PRIOR_ACCOUNT ON dbo.PRIOR_BALANCE.AccountID = dbo.PRIOR_ACCOUNT.ID

上面的查询返回我的预期结果:

Label   Entity                      AccountNumber   AccountDescription  AccountBalance
21      Company ABC                 1               Customer Sales      25
21      Company ABC                 2               Customer Sales      568
22      XYZ Solutions               3               Vendor Sales        344
23      Number 1 Products           4               Vendor Sales        565
24      Enterprise Inc              5               Wholesale           334
24      Enterprise Inc              6               Wholesale           5452
24      Enterprise Inc              7               Wholesale           5877
26      QWERTY Solutions            8               Customer Sales      456
27      Acme                        9               Customer Sales      752
28      United Product Solutions    10              Vendor Sales        87

我想做的是得到类似以下结果:

Label   Entity                      AccountNumber   AccountDescription  AccountBalance  Changed
21      Company ABC                 1               Customer Sales      25              FALSE
21      Company ABC                 2               Customer Sales      568             FALSE
22      XYZ Solutions               3               Vendor Sales        344             FALSE
23      Number 1 Products           4               Vendor Sales        565             FALSE
24      Enterprise Inc              5               Wholesale           334             TRUE
24      Enterprise Inc              6               Wholesale           5452            FALSE
24      Enterprise Inc              7               Wholesale           5877            TRUE
26      QWERTY Solutions            8               Customer Sales      456             FALSE
27      Acme                        9               Customer Sales      752             FALSE
28      United Product Solutions    10              Vendor Sales        87              FALSE

我不知道从这里去哪里。希望这个小组提供任何建议!

1 个答案:

答案 0 :(得分:0)

以下内容应作为比较两个值的一种方法。 我在表名中添加了别名,以帮助阅读;

    SELECT DISTINCT 
     ce.Label
    ,ce.Description AS Entity
    ,ca.AccountNumber
    ,ca.Description AS AccountDescription
    ,cb.GLAccountBalance
    ,CASE
            WHEN cb.GLAccountBalance = p.PriorBalance THEN 'False'
            WHEN cb.GLAccountBalance <> p.PriorBalance THEN 'True'
        END AS [Changed]
    FROM dbo.CURRENT_BALANCE cb
    INNER JOIN dbo.CURRENT_ENTITY ce ON cb.EntityID = ce.ID 
    INNER JOIN dbo.CURRENT_ACCOUNT ca ON cb.AccountID = ca.ID 
    LEFT JOIN ( SELECT DISTINCT 
                 pe.ID AS PE_ID
                ,pa.ID AS PA_ID
                ,pb.GLAccountBalance AS PriorBalance
                FROM dbo.PRIOR_BALANCE pb
                INNER JOIN dbo.PRIOR_ENTITY pe ON pb.EntityID = pe.ID 
                INNER JOIN dbo.PRIOR_ACCOUNT pa ON pb.AccountID = pa.ID 
            ) p ON p.PE_ID = ce.ID AND p.PA_ID = ca.ID