如何使用Union组合这两个查询?

时间:2015-12-01 09:49:48

标签: mysql sql database

我有两个查询,我试图使用union语句组合成一个。

这是我到目前为止所做的:

+--------------+------------+---------+
| Donor        | Total Paid | Pocket  |
+--------------+------------+---------+
| John Smith   |    3500.00 |    0.00 |
| Linda Smith  |     250.00 |    0.00 |
| Jack Clinton |     200.00 |    0.00 |
| Jane Doe     |    2100.00 |    0.00 |
| John Smith   |       0.00 | 1750.00 |
| Linda Smith  |       0.00 |  100.00 |
| Jack Clinton |       0.00 |  200.00 |
| Jane Doe     |       0.00 | 2100.00 |
+--------------+------------+---------+

这会创建:

+--------------+------------+---------+
| Donor        | Total Paid | Pocket  |
+--------------+------------+---------+
| John Smith   |    3500.00 | 1750.00 |
| Linda Smith  |     250.00 |  100.00 |
| Jack Clinton |     200.00 |  200.00 |
| Jane Doe     |    2100.00 | 2100.00 |
+--------------+------------+---------+ 

我不知道如何摆脱重复的部分。我希望将前四个名字与最后四个名字结合起来创建4个名字,其中包括"总付费和#34;和"口袋"有价值而不是零。

为了清楚起见,我希望输出看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Accounts locale="en_US">
  <Account>
    <Id>abcd</Id>
    <OwnerLastName>asd</OwnerLastName>
    <OwnerFirstName>zxc</OwnerFirstName>
    <Locked>false</Locked>
    <Database>mail</Database>
    <Customer>mail</Customer>
    <CreationDate year="2011" month="8" month-name="fevrier" day-of-month="19" hour-of-day="15" minute="23" day-name="dimanche"/>
    <LastLoginDate year="2015" month="04" month-name="avril" day-of-month="22" hour-of-day="11" minute="13" day-name="macredi"/>
    <LoginsCount>10405</LoginsCount>
    <Locale>nl</Locale>
    <Country>NL</Country>
    <SubscriptionType>free</SubscriptionType>
    <ActiveSubscriptionType>free</ActiveSubscriptionType>
    <SubscriptionExpiration year="1980" month="1" month-name="janvier" day-of-month="1" hour-of-day="0" minute="0" day-name="jeudi"/>
    <SubscriptionMonthlyFee>0</SubscriptionMonthlyFee>
    <PaymentMode>Undefined</PaymentMode>
    <Provision>0</Provision>
    <InternalMail>asdf@asdf.com</InternalMail>
    <ExternalMail>fdsa@zxczxc.com</ExternalMail>
    <GroupMemberships>
      <Group>werkgroep X.Y.Z.</Group>
    </GroupMemberships>
    <SynchroCount>6</SynchroCount>
    <LastSynchroDate year="2003" month="12" month-name="decembre" day-of-month="5" hour-of-day="12" minute="48" day-name="mardi"/>
    <HasActiveSync>false</HasActiveSync>
    <Company/>
  </Account>
  <Account>
    <Id>mnbv</Id>
    <OwnerLastName>cvbb</OwnerLastName>
    <OwnerFirstName>bvcc</OwnerFirstName>
    <Locked>true</Locked>
    <Database>mail</Database>
    <Customer>mail</Customer>
    <CreationDate year="2012" month="10" month-name="octobre" day-of-month="10" hour-of-day="10" minute="18" day-name="jeudi"/>
    <LastLoginDate/>
    <LoginsCount>0</LoginsCount>
    <Locale>fr</Locale>
    <Country>BE</Country>
    <SubscriptionType>free</SubscriptionType>
    <ActiveSubscriptionType>free</ActiveSubscriptionType>
    <SubscriptionExpiration year="1970" month="1" month-name="janvier" day-of-month="1" hour-of-day="1" minute="0" day-name="jeudi"/>
    <SubscriptionMonthlyFee>0</SubscriptionMonthlyFee>
    <PaymentMode>Undefined</PaymentMode>
    <Provision>0</Provision>
    <InternalMail/>
    <ExternalMail>qweqwe@qwe.com</ExternalMail>
    <GroupMemberships/>
    <SynchroCount>0</SynchroCount>
    <LastSynchroDate year="1970" month="1" month-name="janvier" day-of-month="1" hour-of-day="1" minute="0" day-name="jeudi"/>
    <HasActiveSync>false</HasActiveSync>
    <Company/>
  </Account>
</Accounts>

我知道我错过了关于工会声明的一些内容,我只是不知道它是什么。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您似乎不必这样做,您只需计算条件sumcase}:

select concat(d.FirstName, ' ', d.LastName) as 'Donor'
     , sum(a.amount) as 'Total Paid'
     , sum(case when a.CompanyId is null then a.amount else 0 end) as 'Pocket' 
from Donor d
join Pledge p on d.DonorId = p.DonorId
join Payment a on p.pledgeId = a.pledgeId
group by d.donorid