sql - 在 SQL 中显示三个不同表中的三个不同列
问题描述
我试图在 SQL 中显示三个不同的列,它们都来自同一个数据库,但每个列都来自不同的表。一列称为名称,我也希望将输出分组,另一列是称为货币的两列之间的计算,第三列是日期。NameId 是连接 Name 和 Money Column,而 MoneyId 连接 Money 和 Date
我在 dbVisualizer 上创建它,这三列都是同一数据库中不同表的所有部分。如何在按名称列分组的同时显示计算出的 Money 及其正确日期
我知道这是不正确的,但这是我到目前为止所拥有的。我怎样才能进行金钱计算,但不必将其属性按函数分组即可运行。
Select A.Name,
MAX(B.BillsPaid)As Paid,
MAX(B.BillsSent) As Sent, C.DateId As Date_Id,
-- This is the money Calculation
(CAST(BillsPaid As decimal(5,0))/CAST(nullif(BillsSent, 0) As
decimal(5,0)))
* 100 as Money
From B
Inner Join A on B.NameId = A.NameId
Inner Join C on B.MoneyId = C.MoneyId
Group By A.Name, C.DateId, B.BillsPaid, B.BillsSent
我想在显示器上看到这个。我是 SQL 新手,非常感谢所有帮助,谢谢。
Name Money(%) Date
John Doe 87% June 2019
相反,我看到
Name Paid Sent Money Date
John Doe 2 4 50% June 2019
John Doe 1 4 25% June 2019
John Doe 3 4 75% July 2019
我希望每个日期按每个名字对钱进行分组,但是每个名字每月有超过一个支付/发送
解决方案
我相信这会让你得到你想要的 - 交叉应用有点奇怪,但它很方便:) 如果你想要更多关于那个的信息,谷歌有很多!
Select
A.Name
,b.[Paid]
,b.[Sent]
,min(C.DateId) [Date_Id]
,(CAST(b.paid As decimal(5,0))/CAST(nullif(b.[sent], 0) as decimal(5,0))) * 100 [Money]
From
a
cross apply (
select
max(billspaid) [Paid]
,max(billssent) [Sent]
from
B
where
A.NameId = B.NameId
) b
inner join C
on B.MoneyId = C.MoneyId
group by
A.Name
,b.[Paid]
,b.[Sent]
编辑
Select
a.Name
,b.[Paid]
,b.[Sent]
,min(C.DateId) [Date_Id]
,(CAST(b.paid As decimal(5,0))/CAST(nullif(b.[sent], 0) as decimal(5,0))) * 100 [Money]
From
a
cross apply (
select
max(billspaid) [Paid]
,max(billssent) [Sent]
from
B
where
A.NameId = B.NameId
) b
inner join b b1
on a.nameid = b1.nameid
inner join c
on b1.MoneyId = c.MoneyId
group by
a.Name
,b.[Paid]
,b.[Sent]
推荐阅读
- api - AWS Cognito AccessToken 与 IdToken
- typescript - 缩小联合类型 Typescipt
- excel - 突出显示一列中的文本包含另一列中的数值的行
- angular - Angular FormControl 值实际上是什么,为什么它与 nativeElement 值不同?
- oracle - 气流 initdb 与 oracle 数据库有问题
- php - 选中记住我复选框时如何设置 Laravel Passport 令牌过期时间
- python - 即使将模块路径附加到 sys 后,也无法在 google colab 中导入自定义库
- c# - xml序列化器,不要序列化顶级类?
- angular - Angular SpyOn 公共财产
- c# - C#控制台应用程序如何检查请求的点在半径为10的主要点(-3,4)内