【发布时间】:2015-11-19 09:02:43
【问题描述】:
我知道有很多关于此类主题的问题,但我找不到任何可以帮助解决我的问题的问题。
我有两个工作查询。我想将它们组合成一个查询。当我尝试组合它们时,显示的数据不正确。但是,当它们分开时,数据是正确的。
查询 1:
Select Distinct concat(d.FirstName, ' ', d.LastName) as 'Donor',
concat('$', sum(a.amount)) as 'Total Paid'
From Donor d, Pledge p, Payment a
Where d.DonorId=p.DonorId
and p.pledgeId = a.pledgeId
group by d.donorid;
输出:
+--------------+------------+
| Donor | Total Paid |
+--------------+------------+
| John Smith | $3500.00 |
| Linda Smith | $250.00 |
| Jack Clinton | $200.00 |
| Jane Doe | $2100.00 |
+--------------+------------+
查询 2:
Select Distinct concat(d.FirstName, ' ', d.LastName) as 'Donor',
concat('$', sum(a.amount)) as 'Pocket'
From Donor d, Pledge p, Payment a
Where (a.CompanyId is null)
and d.DonorId=p.DonorId
and p.pledgeId = a.pledgeId
group by d.donorid;
输出:
+--------------+----------+
| Donor | Pocket |
+--------------+----------+
| John Smith | $1750.00 |
| Linda Smith | $100.00 |
| Jack Clinton | $200.00 |
| Jane Doe | $2100.00 |
+--------------+----------+
组合时:
Select Distinct concat(d.FirstName, ' ', d.LastName) as 'Donor',
concat('$', sum(a.amount)) as 'Total Paid',
concat('$', sum(a2.amount)) as 'Pocket'
From Donor d, Donor d2, Pledge p, Pledge p2, Payment a, Payment a2
where d.donorId=p.donorId
and p.pledgeId = a.pledgeId
and (a2.CompanyId is null)
and d2.DonorId = p2.DonorId
and p2.pledgeId = a2.PledgeId
group by d.DonorId;
输出:
+--------------+------------+-----------+
| Donor | Total Paid | Pocket |
+--------------+------------+-----------+
| John Smith | $24500.00 | $20750.00 |
| Linda Smith | $1750.00 | $12450.00 |
| Jack Clinton | $1400.00 | $8300.00 |
| Jane Doe | $14700.00 | $8300.00 |
+--------------+------------+-----------+
这些查询中的每一个都有一个用于捐赠者姓名的列和一个包含一些货币值的列。在我的最后一个查询中,我想要一个包含捐赠者姓名的列、一个标有“总支付”的列和一个标有“口袋”的列。当我结合这两个查询时,“总支付”列和“口袋”列都被弄乱了。
我知道如果没有表架构,这可能很难提供帮助,但我想我会试一试。提前致谢。
【问题讨论】: