[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL: count question

Posted on 2011-03-01
11
Medium Priority
?
231 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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
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…

649 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