简体   繁体   中英

New to SQL - How to compare data from two sets of joined tables

I'm trying to compare a set of multiple tables against another set of multiple tables in a SQL database. Admittedly, I'm very novice to SQL so please forgive me if my terminology is incorrect.

I have 6 individual tables in my database. 3 of those are comprised of current month's data and the other three are prior month's data. I need to produce a report that shows current month data, but also compare the account balance against the prior month - I need to create a column that will list "true" or "false" based on whether the account balances match.

I have joined the three current month's data tables together and I have joined the three prior month's data tables together.

The two sets of tables are identical. Below is a description of the tables - There is one set prefixed "CURRENT" and one prefixed "PRIOR":

BALANCE:
    EntityID
    AccountID
    AccountBalance

ENTITY:
    ID
    Label
    Description

ACCOUNT:
    ID
    AccountNumber
    Description

The report I need to provide should list the following from the current month's data:

Label     
Description 
AccountNumber   
AccountDescription  
AccountBalance  

I need to add a column called " Changed " at the end of the report. This column should have a "True" or "False" value depending on whether or not the current & prior account balances match.

Thus far, I have only been able to join the tables. I'm unsure how to edit this query to compare the current month dbo.CURRENT_BALANCE.AccountBalance against the prior month's 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

The query above returns my expected results:

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

What I would like to do is have a result similar to:

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

I have no idea where to go from here. Would appreciate any advise from this group!

The below should work as a way to compare the 2 values. I have added aliases to table names to help with reading;

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM