因此,我正在尝试从只读数据库中编写查询,该查询将基本向我展示父值和子值之间的关系以及其他表中的一些其他信息。
我几乎在需要的地方找到了查询,但是正在用相应的名称和电子邮件替换一些值而感到苦恼
基本上,我试图反映在电子邮件和凭据表中显示的名称和电子邮件,该名称和电子邮件与在“父母姓名”和“父母电子邮件”列的当前位置显示的相应AcctID相对应。即。用“约翰·史密斯”代替父母姓名中的“ 1a2b”,用“ test @ test”代替父母电子邮件中的“ 1a2b”
我一开始尝试了一个案例陈述,但我想这可能与我所缺少的联接有关。
在此先感谢任何帮助人员。
答案 0 :(得分:2)
您只需再次连接到credentials
和email
表 即可提取父数据:
select
cr1.data as "Parent Name",
e1.email as "Parent Email",
cr.data as "Child Name",
e.email as "Child Email",
sum(a.amount) as "Amount",
(sum(a.amount)*2.5/100) as "% of Amount"
from
email e
join
class c
on e.AcctID = c.AcctID
left join
relationship r
on c.AcctID = r.child
left join
amount a
on e.AcctID = a.AcctID
left join
credentials cr
on e.AcctID = cr.AcctID and cr.value = 1
left join
credentials cr1
on r.Parent = cr1.AcctID and cr1.value = 1
left join
email e1
on e1.AcctId = r.Parent
where
c.relationship = 'child'
group by
cr1.data, e1.email, cr.data, e.email
order by
cr1.data
输出:
Parent Name Parent Email Child Name Child Email Amount % of Amount
so@so add@add 10.36 0.259000
John Smith Test@Test Jane Smith email@email 24.72 0.618000
答案 1 :(得分:1)
如果我的理解正确,您只需join
表email
两次。查询看起来像这样:
select r.parent as "Parent Name",
ep.email as "Parent Email",
cr.data as "Child Name",
ec.email as "Child Email",
sum(a.amount) as "Amount",
(sum(a.amount)*2.5/100) as "% of Amount"
from class c left join
relationship r
on c.AcctID = r.child left join
email ec
on ec.AcctId = r.child left join
email ep
on ep.AcctId = r.parent left join
amount a
on c.AcctID = a.AcctID left join
credentials cr
on c.AcctID = cr.AcctID and cr.value = 1
where c.relationship = 'child'
group by r.Parent, ep.email, cr.data, ec.email
order by r.Parent;
Here是您的db <>小提琴,带有固定的查询。
请注意,这也可以解决您的join
条件(因此一旦引入email
表,就不会有任何内容加入)。而且还简化了group by
键。