gpt4 book ai didi

sql - 我无法正确查询此查询

转载 作者:行者123 更新时间:2023-12-04 23:20:21 26 4
gpt4 key购买 nike

我有一张两张 table ,第一个看起来像这样

Leave       Limit
-----------------
vacation 15
sick 10
absent 9

第二个..
Person             Leave      Use
-------------------------------
Chuck Norris sick 1
Bruce Lee absent 2

我想要它做的是加入表格,当我选择一个人时,它会像这样显示
ex. i select Chuck Norris 

Leave Limit Use
------------------------------
vacation 15 0
sick 10 1
absent 9 0

这是我已经做的......
SELECT        LeavetypTbl.Leave, LeavetypTbl.Limit, ISNULL(SUM(LeaveTbl.Use), 0) AS Expr1, 
LeavetypTbl.Limit- ISNULL(SUM(LeaveTbl.USE), 0) AS DayLeft
FROM LeavetypTbl LEFT OUTER JOIN
LeaveTbl ON LeavetypTbl.Leave = LeaveTbl.Leave
WHERE LeaveTbl.Person ='Chuck Norris'
GROUP BY LeavetypTbl.Leave, LeavetypTbl.Limit

但结果不是我所期望的..lol
Leave    Limit    Use    DayLeft
Sick       10       1          9

我只得到 1 行将..

最佳答案

您只得到一行,因为您将结果限制为仅 'Chuck Norris'在顶级WHERE条款。然而,Chuck Norris从不休假或缺席,因此来自外连接的另外两行被 WHERE 消除了。条款。将其移至 ON子句以获得您期望的结果:

SELECT        LeavetypTbl.Leave, LeavetypTbl.Limit, ISNULL(SUM(LeaveTbl.Use), 0) AS Expr1, 
LeavetypTbl.Limit- ISNULL(SUM(LeaveTbl.USE), 0) AS DayLeft
FROM LeavetypTbl LEFT OUTER JOIN
LeaveTbl ON LeavetypTbl.Leave = LeaveTbl.Leave AND LeaveTbl.Person ='Chuck Norris'
GROUP BY LeavetypTbl.Leave, LeavetypTbl.Limit

关于sql - 我无法正确查询此查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28946014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com