InfoTechEE
asked on
SQL: Query Help
The code below is a competition among Sales Staff of how many jobs/orders they booked.
The competition is divided into 2 parts. The seniors (more than 5 years with the company) and the freshmen (less than 5 yrs) compete within their own category.
There are also tricky accounts in the system called "Split Ana/Sheri". It's really weird, but as far as our DB knows, that account is an actual Sales Rep all on his own. It has been decided that if such a Sales Rep books an account, the points need to be split 50%. Basically 0.5 needs to go to Ana LastName, and 0.5 needs to go to Sheri LastName.
Any ideas how to change the code below to incorporate this idea?
The competition is divided into 2 parts. The seniors (more than 5 years with the company) and the freshmen (less than 5 yrs) compete within their own category.
There are also tricky accounts in the system called "Split Ana/Sheri". It's really weird, but as far as our DB knows, that account is an actual Sales Rep all on his own. It has been decided that if such a Sales Rep books an account, the points need to be split 50%. Basically 0.5 needs to go to Ana LastName, and 0.5 needs to go to Sheri LastName.
Any ideas how to change the code below to incorporate this idea?
declare @Table1 Table(RsrcNo int, DisplayName varchar(25), Entered datetime)
insert into @Table1
select 1743, 'Ana LastName', '6/5/2006'
union all
select 4986, 'Bob LastName', '5/3/2010'
union all
select 1672, 'Candace LastName', '6/28/1999'
union all
select 5100, 'Cynthia LastName', '2/28/2011'
union all
select 1821, 'Elizabeth LastName', '3/13/2009'
union all
select 4214, 'Elizabeth LastName', '9/20/2006'
union all
select 3623, 'Faith LastName', '9/2/2008'
union all
select 4089, 'Jodi LastName', '5/12/1997'
union all
select 4690, 'Joseph LastName', '5/12/2008'
union all
select 3011, 'Kimberly LastName', '7/14/1999'
union all
select 4872, 'Kristen LastName', '8/17/2009'
union all
select 1662, 'Nicholas LastName', '6/1/2007'
union all
select 4868, 'Olga LastName', '8/03/2009'
union all
select 4068, 'Pamela LastName', '11/07/1996'
union all
select 4663, 'Sheri LastName', '5/27/2007'
union all
select 2705, 'Stephen LastName', '3/21/2005'
union all
select 1093, 'Tami LastName', '5/9/2005'
union all
select 3749, 'Teresa LastName', '4/27/1999'
union all
select 3357, 'Lindsey LastName', '11/13/2009'
SELECT R.DisplayName,
CASE WHEN(DATEDIFF(dd,E.entered,getdate())/365) >= 5 THEN 'Senior'
ELSE 'Freshman' END AS 'Stage', (select COUNT(*) from Jobs J where J.SalesRep = R.RsrcNo and J.Entered >= '3/1/11') as Jobs_Booked
FROM Resources R LEFT OUTER JOIN @Table1 E ON E.RsrcNo = R.RsrcNo
WHERE R.RsrcNo IN (1743, 4986, 1672, 1821, 4214, 3623, 4089, 4690, 3011, 4872, 1662, 4868, 4068, 4663, 2705, 1093, 3749, 5100, 3357)
AND R.[Disabled] = 0
ORDER BY Stage, Jobs_Booked desc
Provide some sample data from Resources table and the expected result.
Basically I did not understand the splitting of 50% to Ana LastName and Sheri LastName. Explain with an example on that?
ASKER
OK, I attached the modified code, and the results of the code in an Excel sheet.
The current results include Split accounts.
.Split Jodi/Tami has 36 Jobs_Booked.
Jodi LastName has 87 Jobs_Booked.
Tami LastName has 64 Jobs_Booked.
The desired results should look like this:
Jodi LastName has 105 Jobs_Booked.
Tami LastName has 82 Jobs_Booked.
Taking the Split account and dividing it evenly to the individual accounts.
The current results include Split accounts.
.Split Jodi/Tami has 36 Jobs_Booked.
Jodi LastName has 87 Jobs_Booked.
Tami LastName has 64 Jobs_Booked.
The desired results should look like this:
Jodi LastName has 105 Jobs_Booked.
Tami LastName has 82 Jobs_Booked.
Taking the Split account and dividing it evenly to the individual accounts.
declare @Table1 Table(RsrcNo int, DisplayName varchar(25), Entered datetime)
insert into @Table1
select 1743, 'Ana LastName', '6/5/2006'
union all
select 4986, 'Bob LastName', '5/3/2010'
union all
select 1672, 'Candace LastName', '6/28/1999'
union all
select 5100, 'Cynthia LastName', '2/28/2011'
union all
select 1821, 'Elizabeth LastName', '3/13/2009'
union all
select 4214, 'Elizabeth LastName', '9/20/2006'
union all
select 3623, 'Faith LastName', '9/2/2008'
union all
select 4089, 'Jodi LastName', '5/12/1997'
union all
select 4690, 'Joseph LastName', '5/12/2008'
union all
select 3011, 'Kimberly LastName', '7/14/1999'
union all
select 4872, 'Kristen LastName', '8/17/2009'
union all
select 1662, 'Nicholas LastName', '6/1/2007'
union all
select 4868, 'Olga LastName', '8/03/2009'
union all
select 4068, 'Pamela LastName', '11/07/1996'
union all
select 4663, 'Sheri LastName', '5/27/2007'
union all
select 2705, 'Stephen LastName', '3/21/2005'
union all
select 1093, 'Tami LastName', '5/9/2005'
union all
select 3749, 'Teresa LastName', '4/27/1999'
union all
select 3357, 'Lindsey LastName', '11/13/2009'
SELECT R.DisplayName,
CASE WHEN(DATEDIFF(dd,E.entered,getdate())/365) >= 5 THEN 'Senior'
WHEN(DATEDIFF(dd,E.entered,getdate())/365) < 5 THEN 'FreshMan'
ELSE 'Split' END AS 'Stage', (select COUNT(*) from Jobs J where J.SalesRep = R.RsrcNo and J.Entered >= '3/1/11') as Jobs_Booked
FROM Resources R LEFT OUTER JOIN @Table1 E ON E.RsrcNo = R.RsrcNo
WHERE R.RsrcNo IN (1743, 4986, 1672, 1821, 4214, 3623, 4089, 4690, 3011, 4872, 1662, 4868, 4068, 4663, 2705, 1093, 3749, 5100, 3357,
1574, 2706, 3463, 3465, 3466, 4083, 4245, 4436, 4475, 4478, 4506, 4517, 4743, 4747, 4755, 4777, 4871, 4877, 4883,
4896, 4925, 4942, 4947, 5030, 5032, 5074, 5076, 5087, 5089)
AND R.[Disabled] = 0
ORDER BY Stage, Jobs_Booked desc
1.xlsx
Do you have display name as ".Split Jodi/Tami" with entered as NULL in Resources table?
BTW, what is your sql version?
you can try like this.
;WITH Split
AS (SELECT R.DisplayName,
CASE
WHEN (DATEDIFF(dd,E.entered,GETDATE()) / 365) >= 5 THEN 'Senior'
WHEN (DATEDIFF(dd,E.entered,GETDATE()) / 365) < 5 THEN 'FreshMan'
ELSE 'Split'
END AS 'Stage',
(SELECT COUNT(* )
FROM Jobs J
WHERE J.SalesRep = R.RsrcNo
AND J.Entered >= '3/1/11') AS Jobs_Booked
FROM Resources R
LEFT OUTER JOIN @Table1 E
ON E.RsrcNo = R.RsrcNo
WHERE R.RsrcNo IN (1743,4986,1672,1821,
4214,3623,4089,4690,
3011,4872,1662,4868,
4068,4663,2705,1093,
3749,5100,3357,1574,
2706,3463,3465,3466,
4083,4245,4436,4475,
4478,4506,4517,4743,
4747,4755,4777,4871,
4877,4883,4896,4925,
4942,4947,5030,5032,
5074,5076,5087,5089)
AND R.[Disabled] = 0)
SELECT DisplayName,
SUM(Jobs_Booked) Jobs_Booked
FROM (SELECT DisplayName,
Jobs_Booked
FROM Split
WHERE DisplayName NOT LIKE '.Split%'
UNION ALL
SELECT RTRIM(LTRIM(SUBSTRING(REPLACE(DisplayName,'.Split',''),1,CHARINDEX('/',REPLACE(DisplayName,'.Split','')) - 1))) + ' LastName' DisplayName,
Jobs_Booked / 2 Jobs_Booked
FROM Split
WHERE DisplayName LIKE '.Split%'
UNION ALL
SELECT RTRIM(LTRIM(SUBSTRING(REPLACE(DisplayName,'.Split',''),CHARINDEX('/',REPLACE(DisplayName,'.Split','')) + 1,
LEN(DisplayName)))) + ' LastName' DisplayName,
Jobs_Booked / 2 Jobs_Booked
FROM Split
WHERE DisplayName LIKE '.Split%') t1
GROUP BY DisplayName
ORDER BY Jobs_Booked DESC
you shouldn't expose these data, if they are real data.
>>you shouldn't expose these data, if they are real data.<<
Do you honestly think that names such as "Elizabeth LastName" are real data?
Do you honestly think that names such as "Elizabeth LastName" are real data?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Figured out my own way.