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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pulling List of Payments for Each Person in Table

Author  Topic 

melora55
Starting Member

1 Post

Posted - 2013-01-07 : 20:06:21
Hi,
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(j.date) 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!!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 23:19:54
[code]
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
INNER JOIN (SELECT DonorID,ROW_NUMBER() OVER (PARTITION BY Donor_ID ORDER BY [Date] ASC) AS Seq,*
FROM Pledge)p
ON p.DonorID = d.DonorID
GROUP BY d.DonorID,d.DonorName
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2013-01-07 : 23:20:05
[code]
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'

Select
DonorId,Donor,Name,
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

from
(
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


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
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 23:20:30
to make it dynamic use

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -