Solved

SQL: count question

Posted on 2011-03-01
11
228 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

717 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