Really just looking for guidance from more experienced SQL users or solutions you think would work.
I need to display a list of Consultants, and next to their names I need to display 3 totals. The thing is... Those totals need their names passed in as a variable in order to work out the amount for him / her.
DECLARE @lastMonthDate VARCHAR(10)
DECLARE @currentMonthDate VARCHAR(10)
DECLARE @Year VARCHAR(10)
DECLARE @TotalExpected VARCHAR (MAX)
DECLARE @TotalPaid VARCHAR (MAX)
DECLARE @TotalUnpaid VARCHAR (MAX)
SET @lastMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -1, GETDATE())))+'-21'
SET @currentMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, GETDATE()))+'-20'
SET @Year = DATEPART(YEAR, GETDATE())
SELECT @TotalExpected = SUM(CONVERT(DECIMAL(10,2), budg_do1_total))
FROM Company
JOIN Opportunity on Company.Comp_CompanyId = Opportunity.Oppo_PrimaryCompanyId
JOIN Budget on Opportunity.Oppo_OpportunityId = Budget.budg_OpportunityId
JOIN Users on company.Comp_PrimaryUserId = User_UserId
Join Channel on User_PrimaryChannelId = Channel.Chan_ChannelId
WHERE CONVERT(DATE, budg_tempDODate1) BETWEEN CONVERT(DATE, @lastMonthDate) AND CONVERT(DATE, @currentMonthDate)
AND User_FirstName = 'Geoffrey' AND User_LastName = 'Wylde'
The other totals are worked out in a similar fashion, but I want to keep this as short as possible so I left it out. You will notice, at the end of @TotalExpected I am manually adding the name and surname of my consultant, but I would like to do this dynamically some how... And this is why I have come to you, because I don't even know where to begin.
Say I give you Michael, Kevin, John, Kim...
How would I be able to display something like this:
+---------+----------+
| Name | Expected |
+---------+----------+
| Michael | 124 |
| Kevin | 246 |
| John | 241 |
| Kim | 233 |
+---------+----------+
(Cant get this table to display properly here)
Please let me know if this makes sense?