SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
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
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

melora55
Starting Member

1 Posts

Posted - 01/07/2013 :  20:06:21  Show Profile  Reply with Quote
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

India
52249 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
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


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

Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
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'

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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/07/2013 :  23:20:30  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New 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.03 seconds. Powered By: Snitz Forums 2000