Solved

SQL: count question

Posted on 2011-03-01
11
226 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 10

Expert Comment

by:himanshut
ID: 35013423
0
 
LVL 10

Expert Comment

by:himanshut
ID: 35013424
Oops!! wrong thread
Damn..
0
 

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:Ephraim Wangoya
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 41

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

734 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