sql脚本左外连接问题

时间:2018-04-19 17:14:07

标签: outer-join

**Table 1**
Tbcode    description    Amount
2        debtors         40000
3        creditors       50000
4        share_capital   10000
5        reserve         20000
**Table 2**
Tbcode    description    Amount
1          debtors        3000
2          creditors      7000
3          share_capital    0
4          reserve        3000
4          reserve        2000
2          creditors        300
3          share_capital  3000
4          reserve        3000
1          debtors        2000

我必须在table 1

上放置一个左外连接

在我的输出table I中,table 1的所有列和amount列的总和需要TB code

我写了以下脚本:

select openingtb.TBCODE,openingtb.DESCRIPTION,openingtb.AMOUNT,count(journals.AMOUNT)
from openingtb
left outer join journals
on openingtb.tbcode = journals.TBCODE
group by journals.TBCODE.TBCODE
;

有人可以指导我哪些代码错误,以及获得所需输出的正确代码是什么

1 个答案:

答案 0 :(得分:0)

你使用聚合函数,那么你应该对select子句中使用的所有非聚合字段进行分组......

SELECT openingtb.TBCODE, openingtb.DESCRIPTION, openingtb.AMOUNT
, Count(journals.AMOUNT)
FROM openingtb
LEFT JOIN journals
ON openingtb.tbcode = journals.TBCODE
GROUP BY openingtb.TBCODE, openingtb.DESCRIPTION, openingtb.AMOUNT
;