Solved

SQL: Query Help

Posted on 2011-03-24
10
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 41

Expert Comment

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

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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 41

Expert Comment

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

Expert Comment

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

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

749 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