Solved

SQL: Query Help

Posted on 2011-03-24
10
242 Views
Last Modified: 2012-05-11
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?
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

Open in new window

0
Comment
Question by:InfoTechEE
10 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35212117
Provide some sample data from Resources table and the expected result.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35212158
Basically I did not understand the splitting of 50% to Ana LastName  and Sheri LastName. Explain with an example on that?
0
 

Author Comment

by:InfoTechEE
ID: 35212198
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.
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

Open in new window

1.xlsx
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 40

Expert Comment

by:Sharath
ID: 35212255
Do you have display name as ".Split Jodi/Tami" with entered as NULL in Resources table?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35212265
BTW, what is your sql version?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35212349
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

Open in new window

0
 
LVL 5

Expert Comment

by:Kelmen
ID: 35213462
you shouldn't expose these data, if they are real data.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35214612
>>you shouldn't expose these data, if they are real data.<<
Do you honestly think that names such as "Elizabeth LastName" are real data?
0
 

Accepted Solution

by:
InfoTechEE earned 0 total points
ID: 35316892
Thanks to everyone, but none of the solutions really helped. I had to do a CASE statement, and that's what got it working for us. CASE when Jodi THEN COUNT(*) for Jodi + ( COUNT(*) for Split - Jodi/Tami ) / 2.
0
 

Author Closing Comment

by:InfoTechEE
ID: 35356971
Figured out my own way.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question