Question

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?

Was it helpful?

Solution

You're fairly close with the SQL above. You need to add the user name to the SELECT statement rather than the WHERE clause and then use GROUP BY, like so:

    SELECT User_FirstName + ' ' + UserLastName, 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)
    GROUP BY User_FirstName + ' ' + UserLastName

Ideally, also you would GROUP BY on a user id, rather than the first name and last name concatenated. Hope this helps!

OTHER TIPS

if you just remove the restriction

AND User_FirstName = 'Geoffrey' AND User_LastName = 'Wylde' 

then it should calculate the total for every user in the table (is that what you want, or you need to restrict to a subset?)

It should be possible to combine your 3 select statements into a single one, something along the lines of :

select Users.FirstName, Users.SecondName, Total1 = SUM(...), Total2 = SUM(...), Total3 = SUM(...)
from ...
join ...
group by Users.Id

In order to compute the totals, and assuming you can only use standard SQL and not some special-purpose vendor-specific aggregation facility, you'd have to use SUM(...) ... GROUP BY Name;

You'd need three such expressions to create the three totals and you'd need a three-way join to create the single table with the three totals alongside each other (and keeping in mind that the individual tables will not be guaranteed to have all the names in them).

(This response was written when the tag was still just <SQL>)

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



**Declare @tbl table
(
    names nvarchar (50)
)
insert into @tbl 
    select 'Michael'
    Union All
    select 'Kevin'
    Union All
    select 'John'**

SELECT  **Users.consultant**,SUM(CONVERT(DECIMAL(10,2), budg_do1_total)),SUM(CONVERT(DECIMAL(10,2), budg_do2_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 in(select names from @tbl)
    group by Users.consultant**

I assume you Users table contains consultant field

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top