有一个血库,有两个桌子
血库想向其他血库订购一些品脱的血
要下达这样的命令,银行需要查询其数据库并获取其供血者可以提供的总血量少于接受者所需的血液总数的血型。
要查询两个血型及其未满足的数量。
**Donor**: DID | NAME | GENDER | CITY | BLOOD_GROUP | AMOUNT
**Acceptor**: AID | NAME | GENDER | CITY | BLOOD_GROUP | AMOUNT
答案 0 :(得分:0)
这将帮助您查询所需的额外血液,
Select a.BG,a.AcceptorAmount-COALESCE(b.DonarAmount,0) as MoreBloodNeeded
From
(Select BG,SUM(AMOUNT)as AcceptorAmount
From ACCEPTOR Group by BG)a
LEFT JOIN
(Select BG, SUM(Amount) as DonarAmount
FromDONOR Group by BG)b on a.BG=b.BG and a.AcceptorAmount>COALESCE(b.DonarAmount,0)
答案 1 :(得分:0)
上面的查询给出了错误,我改变了一点,它的工作原理
Select a.BG, a.AcceptorAmount-b.DonarAmount as MoreBloodNeeded
From
(Select BG,SUM(AMOUNT)as AcceptorAmount
From ACCEPTOR Group by BG) a
LEFT JOIN
(Select BG, SUM(Amount) as DonarAmount
From DONOR Group by BG) b on a.BG=b.BG
WHERE (a.AcceptorAmount - b.DonarAmount > 0)