Solved

SQL: Query Help

Posted on 2011-03-24
10
239 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now