Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pulling List of Payments for Each Person in Table
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 01/07/2013 :  20:06:21  Show Profile  Reply with Quote
I am fairly new to SQL, and I am working with a fundraising database. I am trying to pull a list of donors with a list of all of their pledges for the past year, sorted by date (i.e. donor name, pledge #1, pledge #1 date, pledge #2, pledge #2 date, and so on). I have figured out how to summarize people’s total pledges, but I cannot figure out how to get a list of distinct pledges without just repeating the same pledge/dates over and over. I first tried pulling the earliest pledge, but this does not even work correctly:

update #Test_Table
set pledge1_amt = h.creamt, pledge1_date = h.mindate
from #Test_Table g
inner join
(select MIN( as mindate, j.pledge, j.donor_id
from Pledge j
group by j.donor_id, j.pledge) h
on g.donor_id = h.donor_id

Donor Table:
Donor ID Donor Name
1234 Jane Doe
5678 Mike Brown
9012 Mary Smith

Pledge Table
Donor ID Pledge Date
1234 20.00 1/20/2012
5678 50.00 2/3/2012
5678 50.00 3/3/2012
9012 100.00 1/1/2012
9012 45.00 5/10/2012
9012 5.00 12/31/2012

Desired Output:
Donor ID Donor Name Pledge1_Amt Pledge1_Date Pledge2_Amt Pledge2_Date Pledge3_Amt Pledge3_Date
1234 Jane Doe 20.00 1/20/2012 Null Null Null Null
5678 Mike Brown 50.00 2/3/2012 50.00 3/3/2012 Null Null
9012 Mary Smith 100.00 1/1/2012 45 5/10/2012 5.00 12/31/2012

Any help or advice would be greatly appreciated. Thank you!!

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/07/2013 :  23:19:54  Show Profile  Reply with Quote

SELECT d.DonorID,d.DonorName,
MAX(CASE WHEN Seq=1 THEN Pledge END) AS Pledge1_Amt,
MAX(CASE WHEN Seq=1 THEN [Date] END) AS Pledge1_Date,
MAX(CASE WHEN Seq=2 THEN Pledge END) AS Pledge2_Amt,
MAX(CASE WHEN Seq=2 THEN [Date] END) AS Pledge2_Date,
MAX(CASE WHEN Seq=3 THEN Pledge END) AS Pledge3_Amt,
MAX(CASE WHEN Seq=3 THEN [Date] END) AS Pledge3_Date
FROM Donor d
            FROM Pledge)p
ON p.DonorID = d.DonorID
GROUP BY d.DonorID,d.DonorName

SQL Server MVP

Go to Top of Page

Flowing Fount of Yak Knowledge

7174 Posts

Posted - 01/07/2013 :  23:20:05  Show Profile  Reply with Quote

Declare @Donor Table
DonorID int,
Donor Varchar(10),
Name Varchar(10)
Insert into @Donor
Select 1234,'Jane','Doe' union all
Select 5678,'Mike','Brown' union all
Select 9012,'Mary','Smith'

Declare @Pledge Table
Donor int,
Pledge decimal(10,2),
PledgeDate Datetime
Insert into @Pledge
Select 1234,20.00,'1/20/2012' union all
Select 5678,50.00,'2/3/2012'  union all
Select 5678,50.00,'3/3/2012' union all
Select 9012,100.00,'1/1/2012' union all
Select 9012,45.00,'5/10/2012' union all
Select 9012,5.00,'12/31/2012'

    max([1_Pledge]) as Pledge1_Amt ,
    max([1_PledgeDate]) as Pledge1_Date,
    max([2_Pledge]) as Pledge2_Amt ,
    max([2_PledgeDate]) as Pledge2_Date,
    max([3_Pledge]) as Pledge3_Amt,
    max([3_PledgeDate]) as Pledge3_Date 

        Select D.DonorId,D.Donor,D.Name,Pledge,PledgeDate,
            Cast(ROW_NUMBER() OVER (PARTITION by D.DonorId order by Pledge desc) as varchar(2)) + '_Pledge' as ThatFieldSequence,
            Cast(ROW_NUMBER() OVER (PARTITION by D.DonorId order by Pledge) as varchar(2)) + '_PledgeDate' as ThatOtherFieldSequence
       from @Donor D
       inner join @Pledge P on D.DonorId = P.Donor
    ) a
    pivot (
        max(Pledge) for ThatFieldSequence in ([1_Pledge], [2_Pledge], [3_Pledge])
    ) p1
    pivot (
        max(PledgeDate) for ThatOtherFieldSequence in ([1_PledgeDate], [2_PledgeDate], [3_PledgeDate])
    ) p2
group by

DonorId	Donor	Name	Pledge1_Amt	Pledge1_Date	Pledge2_Amt	Pledge2_Date	Pledge3_Amt	Pledge3_Date
1234	Jane	Doe	20.00	2012-01-20 00:00:00.000	NULL	NULL	NULL	NULL
5678	Mike	Brown	50.00	2012-02-03 00:00:00.000	50.00	2012-03-03 00:00:00.000	NULL	NULL
9012	Mary	Smith	100.00	2012-12-31 00:00:00.000	45.00	2012-05-10 00:00:00.000	5.00	2012-01-01 00:00:00.000
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/07/2013 :  23:20:30  Show Profile  Reply with Quote
to make it dynamic use

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000