简体   繁体   English

SQL-搜索两个表的sum(amount)列不相等的情况

[英]SQL - Search for cases where sum(amount) column across 2 tables are not equal

Table1 has one row per id, table 2 has one row per id where entrytype = 0 and multiple rows per id where entrytype = 1. 表1每个id有一行,表2每个id有1行,其中entrytype = 0,而每个id有多行,其中entrytype = 1。

I run the following queries: 我运行以下查询:

SELECT sum(amount) FROM table1 where id = 'AUS|License|Maintenance|Aug_2016'

select sum(amount) from table2 where entrytype = 0 and id = 'AUS|License|Maintenance|Aug_2016'

select sum(amount) from table2 where entrytype = 1 and id = 'AUS|License|Maintenance|Aug_2016'

And get the following results: 并得到以下结果:

7689.12

7689.12

7689.119999

Now, I want to search both tables' sum(amount) for cases where table1 sum(amount) <> table2 sum(amount) for a particular id 现在,我要搜索两个表的sum(amount) ,以查找table1 sum(amount) <> table2 sum(amount)以获取特定ID的情况

If you want it for a particular id, you can use cross join to bring them together: 如果需要特定的ID,可以使用cross join将它们组合在一起:

select t1.amount, t2_0.amount, t2_1.amount
from (SELECT sum(amount) as amount FROM table1 where id = 'AUS|License|Maintenance|Aug_2016'
     ) t1 cross join
     (select sum(amount) as amount from table2 where entrytype = 0 and id = 'AUS|License|Maintenance|Aug_2016'
     ) t2_0 cross join
     (select sum(amount) as amount from table2 where entrytype = 1 and id = 'AUS|License|Maintenance|Aug_2016'
     ) t2_1;

If I've understood your requirement correctly, you could join each of your queries on their common id field, and then simply select those id values for which the summation is not equal across the three subqueries, eg: 如果我正确理解了您的要求,则可以将每个查询加入它们的公共id字段,然后只需选择三个子查询的总和不相等的id值即可,例如:

SELECT a.id, a.s1, b.s2, c.s3
FROM
(
    (
        SELECT id, SUM(amount) AS s1
        FROM table1 
        GROUP BY id
    ) a
    INNER JOIN
    (
        SELECT id, SUM(amount) AS s2
        FROM table2
        WHERE entrytype = 0
        GROUP BY id
    ) b ON a.id = b.id
)
INNER JOIN
(
    SELECT id, SUM(amount) AS s3
    FROM table2
    WHERE entrytype = 1
    GROUP BY id
) c ON a.id = c.id
WHERE NOT (a.s1 = b.s2 AND a.s1 = c.s3)

You didn't specify a DBMS, so I've included the extra parentheses required by MS Access, just in case. 您没有指定DBMS,因此为防万一,我在其中加入了MS Access所需的额外括号。

By using INNER JOINs , I've also assumed that you're only interested in records which exist in all three subqueries; 通过使用INNER JOINs ,我还假设您只对所有三个子查询中都存在的记录感兴趣; if one of the subqueries is the master, join the other two subqueries using LEFT JOINs so that all records in the master are returned. 如果子查询之一是主查询,则使用LEFT JOINs连接其他两个子查询,以便返回主数据库中的所有记录。


Including the two additional columns mentioned in the subsequent comments - 包括后续注释中提到的另外两列-

SELECT a.id, a.s1, b.s2, c.s3, a.s1 - b.s2 AS s4, a.s1 - c.s3 AS s5
FROM
(
    (
        SELECT id, SUM(amount) AS s1
        FROM table1 
        GROUP BY id
    ) a
    INNER JOIN
    (
        SELECT id, SUM(amount) AS s2
        FROM table2
        WHERE entrytype = 0
        GROUP BY id
    ) b ON a.id = b.id
)
INNER JOIN
(
    SELECT id, SUM(amount) AS s3
    FROM table2
    WHERE entrytype = 1
    GROUP BY id
) c ON a.id = c.id
WHERE NOT (a.s1 = b.s2 AND a.s1 = c.s3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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