sql - SQL - 两个表之间的不同计数
问题描述
我对我认为是一个相对简单的脚本有一个误会。希望我想太多逻辑了。
我要做的是对正确连接的不同列执行两次计数。
我想要的是:
count(a.book_id) as count_of_books
count(b.book_ref_number) as count_of_losses
预期产出
--------------------------------------------------------
| Book | count_of_books | count of losses|
--------------------------------------------------------
|Hunger Games | 76 | 31 |
--------------------------------------------------------
|Hop on Pop | 27 | 6 |
--------------------------------------------------------
|Pout Pout Fish | 138 | 43 |
--------------------------------------------------------
我尝试了几个不同的脚本。这是我尝试过的两个脚本。
(select count(*) from Inventory_Table x ) Count1,
(select count(*) from Loss_table b ) Count2
from Inventory_Table x
right join Loss_table b on b.book_ref_number = x.book_id
where rownum < 20
select
a.book_name,
count(distinct a.book_id),
count(b.book_ref_number)
from Inventory_Table x
right join Loss_table b on trim(b.book_ref_number) = trim(a.book_id)
我得到的结果
--------------------------------------------------------
| Book | count_of_books | count of losses|
--------------------------------------------------------
|Moby Dick | 4376 | 2574 |
--------------------------------------------------------
我正在为我的疏忽错误寻找指导。先感谢您
解决方案
0
SELECT book_name,COUNT(book_id),COUNT(book_ref_id) FROM Inventory_Table right join Loss_table on book_ref_number = book_id GROUP BY book_name
推荐阅读
- selenium - 如何使用 selenium 和 java 处理网站mercurytravels.co.in 中的“From City”和“To City”字段下拉文本字段
- ruby - 如果否则,我们如何在一行中检索两个变量
- python - 币安 api 获取 makerCommission,两个来源两个不同的结果
- java - 返回类型为 Class 的 Java 泛型
或类 - c# - 如何在 C# 的只读属性中进行两次返回?
- python - 如何摆脱 Python 中阻塞内存的变量?
- ios - NavigationLink 随机弹出并“无法呈现。请提交错误。”
- javascript - 有没有办法在带有数组的对象中搜索 mongodb 中的值,并且该值可以存在于该数组的任何对象中
- r - 为什么 is.na 和 !="" 以不同的方式过滤信息
- arrays - 如何按 PowerShell 中的所有属性进行分组?