sql - 我需要加入两个表。第一个表有一些带有 id 列的数据,第二个表有相同 id 的数据,但会有多个 id
问题描述
表格1
workid est_amt amount_spent
1 1000 50
2 200 100
表 2
workid totalDeduction amt_payble
1 30 20
1 30 20
2 25 30
代码
select count(a.workid),a.est_amt,SUM(a.amt_spent + b.total_deductions + b.amt_payable) from table1 a left join table2 b on a.workid = b.workid
结果应该是
total_works amount_paid balance_amount_to_pay
2 305 695
我得到这个结果
total_works amount_paid balance_amount_to_pay
3 305 695
解决方案
usingdistinct()
将仅获取唯一 ID 的计数。
select count(distinct a.workid)
,SUM(a.est_amt)
,SUM(a.amt_spent + b.total_deductions + b.amt_payable)
from table1 a
left join table2 b on a.workid = b.workid
推荐阅读
- vue.js - 将动态插槽从父级传递到子级到孙子级
- snowflake-cloud-data-platform - 将大量小文件加载到雪花中
- amazon-web-services - 使用 SAM AWS 部署带有 HTTP 端点集成的 REST API?
- python - 如何使用默认填充值将 2d numpy 数组的所有内部值设置为相同的大小
- javascript - Cloud Firestore onSnapshot() 仅在没有初始状态的更改时触发
- javascript - 在js中将div旋转到一定高度
- c++ - 我可以以相同的方式使用 char* c 和字符串吗?
- java - android中的Webview不会从res/drawable加载图像
- java - Polarion ALM JAVA SDK API WorkItem 获取类型表示
- authentication - 关于身份验证令牌的 insomnia bitbucket git 配置