-2

我有 5 张桌子:

TABLE_A:
    A_ID

TABLE_B:
    A_ID
    B_ID
TABLE_C:
    B_ID
    C_ID
    C_SOME_NUMBER

TABLE_D:
    A_ID
    D_ID
TABLE_E:
    D_ID
    E_ID
    E_SOME_NUMBER

可以看到,映射关系是:

1 A --> n B
1 B --> n C
1 A --> n D
1 D --> n E

我想写一个 SQL 来计算: A_ID 的计数:

sum(C_SOME_NUMBER) != sum(E_SOME_NUMBER)

但是我不知道怎么写这个sql。我的数据库是甲骨文

4

1 回答 1

2
select
    count(Q1.ID) as 'whoopsie'
from
    (
        select
            a.ID,
            sum(c.some_number) as someNum
        from
            TA a,
            TB b,
            TC c
        where
            a.ID=b.IB
            and b.ID=c.ID
        group by
            a.ID
    ) Q1,
    (
        select
            a.ID,
            sum(e.some_number) as someNum
        from
            TA a,
            TD d,
            TE e
        where
            a.ID=d.IB
            and d.ID=e.ID
        group by
            a.ID
    ) Q2
where
    Q1.ID=Q2.ID
    and Q1.someNum != Q2.someNum
于 2012-06-15T04:36:22.387 回答