sql - Transact SQL - 将一张表与其他两张表连接起来
问题描述
我有一个用于 PaymentRequest (PR) 的表,每个 PR 表都有两种支付形式,一个 WorkProgress 和一个 AdvanceByWarranty,关系是这样的:
我需要创建一个报告以获取该 (PR) 中的所有付款,以及它们之间的其他一些字段
我用来将 PaymentRequest 与 WorkProgress 连接的 sql 是这样的,它可以工作,它返回该付款的 WorkProgress
为了通过保修获得预付款,我使用它,它也有效,返回 2 预付款,因为它应该是
但是,当我将两者混合时,它不会返回 3 行,它应该返回 2 行。结果是这样的
我期待这样的事情(名字更短)
我怎样才能得到预期的查询?
编辑:
获取预期查询的 sql 是这样的
select
pr.ProjectId, pr.NumberPaymentState,
wp.ToCollectAmmount as WPAmmount, wp.ToCollectPercent as WPPercent,
null as AWAmmount, null as AWPercent
from PaymentRequests pr
left join WorkProgresses wp on (wp.ProjectId = pr.ProjectId and wp.NumberPaymentState = pr.NumberPaymentState)
union all
select
pr.ProjectId, pr.NumberPaymentState,
null as WPAmmount, null as WPPercent,
aw.ToCollectAmmount as AWAmmount, aw.ToCollectPercent as AWPercent
from PaymentRequests pr
left join AdvanceByWarranties aw on (aw.ProjectId = pr.ProjectId and aw.NumberPaymentState = pr.NumberPaymentState)
解决方案
你可能想要你展示的东西,但它不是这样工作的。
只需UNION ALL
在前两个查询之间使用,并相应地重命名列。这里有一些伪代码混合来指导你:
select keycols, workercols, null as advancecols
from pr join worker
union all
select keycols, null as workercols, advancecols
from pr join advancecols
推荐阅读
- javascript - 未处理的拒绝 (InvalidTokenError) 使用 async 和 await
- security - 如何为每月扫描的 30 个站点更好地管理 Qualys WAS
- java - 获取设备的位置,无论应用程序处于什么状态始终从我的公司应用程序中检测位置
- python - 混淆矩阵呈现一些空单元格
- regex - 如何在忽略第一场比赛的同时从一个点grep到另一个点?
- javascript - 如何从另一个组件更改组件状态?
- javascript - 这个参数化查询如何防止 SQL 注入?
- html - 尽管使用简单,媒体查询仍不起作用
- npm - Vue-Cli3 和最近的 Node-Sass DoS 咨询:如何用 dart-sass 替换 node-sass?
- sql - 创建函数后我应该 COMMIT 吗?