Solved

SQL: count question

Posted on 2011-03-01
11
220 Views
Last Modified: 2012-05-11
We have 20 sales reps. I am creating a basic query that counts how many items sold this month per sales rep.

SELECT SR.SalesRep, COUNT(*)
FROM SalesReps SR LEFT OUTER JOIN Items I ON I.SalesRep = SR.SalesRepNo
GROUP BY SR.SalesRep

I want all 20 Sales reps to show up, even if they did not sell anything. If they didn't sell anything, I want to see a 0.

However, since ONLY 13 sales reps sold something thus far in March, I am only getting 13 items, not all 20.

How do I modify my query to show all 20 sales reps whether they sold something or not.
0
Comment
Question by:InfoTechEE
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 35013386
SELECT SR.SalesRep, ( select COUNT(*) from Items I where I.SalesRep = SR.SalesRepNo )
FROM SalesReps SR
0
 

Author Comment

by:InfoTechEE
ID: 35013398
Sorry, that did not help.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 35013413
whats the error , can you show me the results
0
 
LVL 10

Expert Comment

by:himanshut
ID: 35013423
0
 
LVL 10

Expert Comment

by:himanshut
ID: 35013424
Oops!! wrong thread
Damn..
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:InfoTechEE
ID: 35013435
Sure...I did not want to make it confusing, so I used aliases for my table names...Here is the actual code, and the results.


SELECT R.DisplayName, (DATEDIFF(dd,r.entered,getdate())/365) as Years_as_AE, 
(select COUNT(*) from Jobs J where J.SalesRep = R.RsrcNo and J.Entered >= '3/1/11') as Jobs_Booked
FROM Jobs J LEFT OUTER JOIN Resources R ON R.RsrcNo = J.SalesRep
WHERE R.RsrcNo IN (1743, 4986, 1672, 1821, 4214, 3623, 4089, 4690, 3011, 4872, 1662, 4868, 4068, 4663, 2705, 1093, 3749, 5100)
AND R.[Disabled] = 0
AND J.Entered >= '3/1/11'
GROUP BY R.RsrcNo, R.DisplayName, R.Entered
ORDER BY COUNT(*) desc

Open in new window

Book1.xlsx
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 35013467
SELECT R.DisplayName, (DATEDIFF(dd,r.entered,getdate())/365) as Years_as_AE,
(select COUNT(*) from Jobs J where J.SalesRep = R.RsrcNo and J.Entered >= '3/1/11') as Jobs_Booked
FROM  Resources R
WHERE R.RsrcNo IN (1743, 4986, 1672, 1821, 4214, 3623, 4089, 4690, 3011, 4872, 1662, 4868, 4068, 4663, 2705, 1093, 3749, 5100)
AND R.[Disabled] = 0
0
 

Author Comment

by:InfoTechEE
ID: 35013480
That worked. Can you explain please? I always thought you needed a group by in order to do a count.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 35013489
this one used a subquery, if you use a subquery you dont really need a Group by clause
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35013495
try
SELECT RES.DisplayName, RES.Years_as_AE, RES.Jobs_Booked 
FROM
	(SELECT R.DisplayName, (DATEDIFF(dd,r.entered,getdate())/365) as Years_as_AE, R.Entered, 
		   (select COUNT(*) from Jobs J where J.SalesRep = R.RsrcNo and J.Entered >= '3/1/11') as Jobs_Booked
	FROM Jobs J 
	LEFT OUTER JOIN Resources R ON R.RsrcNo = J.SalesRep
	WHERE R.RsrcNo IN (1743, 4986, 1672, 1821, 4214, 3623, 4089, 4690, 3011, 4872, 1662, 4868, 4068, 4663, 2705, 1093, 3749, 5100)
	AND R.[Disabled] = 0
	AND J.Entered >= '3/1/11') RES
GROUP BY RES.RsrcNo, RES.DisplayName, RES.Entered
ORDER BY COUNT(*) DESC

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 35013498
Try like this.
SELECT SR.SalesRep, 
       ISNULL(cnt,0) Cnt 
  FROM SalesReps SR 
       LEFT OUTER JOIN (  SELECT SalesRep, 
                                 COUNT(* ) cnt 
                            FROM Items 
                        GROUP BY SalesRep) I 
         ON I.SalesRep = SR.SalesRepNo

Open in new window

In your code, you can try like this.
SELECT R.DisplayName, 
         (DATEDIFF(dd,r.entered,GETDATE()) / 365) AS Years_as_AE, 
         ISNULL(Cnt,0)                            AS Jobs_Booked 
    FROM Jobs J 
         LEFT OUTER JOIN (  SELECT RsrcNo, 
                                   COUNT(* ) cnt 
                              FROM Resources 
                             WHERE RsrcNo IN (1743,4986,1672,1821, 
                                              4214,3623,4089,4690, 
                                              3011,4872,1662,4868, 
                                              4068,4663,2705,1093, 
                                              3749,5100) 
                                   AND [Disabled] = 0 
                          GROUP BY RsrcNo) R 
           ON R.RsrcNo = J.SalesRep 
   WHERE J.Entered >= '3/1/11' 
ORDER BY Cnt DESC

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

930 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

14 Experts available now in Live!

Get 1:1 Help Now