Using Local Variable within Select Statement

30 pts.
Tags:
SQL Select statement problem
I am pulling sponsorship commitments for two events. I am then trying to look at the sponsorship records for the second event and determine if they are a repeat of the same company sponsoring from the first event. I am trying to accomplish this by seeing if the same company as identified by ProfileID exists more than once in the data set. The return for "Occurrence" is not properly evaluating by evaluating the second event sponsorship by looking for multiple occurrences of the ProfileID.

DECLARE @ProfileSelect AS INT 
SELECT @ProfileSelect=EventSignUps.ProfileID 
FROM InvoiceLineItem 
LEFT JOIN EventSignUps ON InvoiceLineItem.EventSignUpID=EventSignUps.EventSignUpID
LEFT JOIN EventList ON EventSignUps.EventID=EventList.EventId 
LEFT JOIN Profile on EventSignUps.ProfileID=Profile.ProfileId 
LEFT JOIN Adjustments ON InvoiceLineItem.InvoiceNum+InvoiceLineItem.LineItemID=Adjustments.InvoiceNum+Adjustments.InvoiceLineItemID WHERE InvoiceLineItem.Item LIKE 'EV_%' AND (InvoiceLineItem.Descr LIKE '%sponsor%' OR InvoiceLineItem.Descr LIKE '%exhibit%' OR InvoiceLineItem.Descr LIKE '%contribut%') AND EventList.EventDate>='01/01/2010' AND EventList.EventID IN(1680,1766)
 SELECT InvoiceLineItem.InvoiceNum
,InvoiceLineItem.Item
,InvoiceLineItem.ItemNum ,InvoiceLineItem.Amount ,InvoiceLineItem.EventSignUpID
,EventSignUps.ProfileID 
,Profile.ReportName 
,EventList.EventName 
,EventList.EventId 
,InvoiceLineItem.Descr 
,(CASE WHEN Adjustments.AdjustmentAmount IS NULL THEN 0 ELSE Adjustments.AdjustmentAmount END) AS Adjustment 
,(CASE WHEN EventList.EventId=1680 THEN 'BASE YEAR' 
WHEN EventList.EventId=1766 AND SUM(CASE WHEN EventSignUps.ProfileID =@ProfileSelect THEN 1 ELSE 0 END)>1 
THEN 'RENEWAL' 
WHEN EventList.EventId=1766 AND SUM(CASE WHEN EventSignUps.ProfileID =@ProfileSelect THEN 1 ELSE 0 END)=1 
THEN 'NEW' ELSE ' ' 
END) AS Occurrance 
 FROM InvoiceLineItem 
LEFT JOIN EventSignUps ON InvoiceLineItem.EventSignUpID=EventSignUps.EventSignUpID 
LEFT JOIN EventList ON EventSignUps.EventID=EventList.EventId 
LEFT JOIN Profile on EventSignUps.ProfileID=Profile.ProfileId LEFT JOIN Adjustments ON InvoiceLineItem.InvoiceNum+InvoiceLineItem.LineItemID=Adjustments.InvoiceNum+Adjustments.InvoiceLineItemID WHERE InvoiceLineItem.Item LIKE 'EV_%' AND (InvoiceLineItem.Descr LIKE '%sponsor%' OR InvoiceLineItem.Descr LIKE '%exhibit%' OR InvoiceLineItem.Descr LIKE '%contribut%') AND EventList.EventDate>='01/01/2010' AND EventList.EventID IN(1680,1766) GROUP BY InvoiceLineItem.InvoiceNum ,InvoiceLineItem.Item ,InvoiceLineItem.ItemNum ,InvoiceLineItem.Amount ,InvoiceLineItem.EventSignUpID ,EventSignUps.ProfileID ,Profile.ReportName ,EventList.EventName ,EventList.EventId ,InvoiceLineItem.Descr ,Adjustments.AdjustmentAmount 
ORDER BY Profile.ReportName

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following