Solved

SQL: count question

Posted on 2011-03-01
11
219 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
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.

 

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

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

12 Experts available now in Live!

Get 1:1 Help Now