Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

finding records in table using the count()

Posted on 2014-07-28
9
Medium Priority
?
117 Views
Last Modified: 2014-08-11
I have an attendance table in sql where I store data for activities by organizations.  Each record in my attendancegrid is associated to a specific month and fiscal year.

I am looking to get all the Distinct records based on ID from my table that have a total > 0 throughout the whole year.  I would like to group it by activity and committee.

Here is what I am using.  Does it look like I am on the right track using the count() function?

SELECT committee, Activity, Count(Distinct ID) as NumberofParticipants FROM AttendanceGrid where Total > 0 
and Month IN ('July', 'August', 'September', 'October', 'November','December','January', 'February','March','April', 'May','June') 
And Fiscal = 2014 GROUP BY Committee, Activity order by Committee, Activity

Open in new window

0
Comment
Question by:al4629740
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Assisted Solution

by:5teveo
5teveo earned 750 total points
ID: 40224828
move 'Distinct'

SELECT Distinct  committee, Activity, Count(ID) as NumberofParticipants FROM AttendanceGrid where Total > 0
and Month IN ('July', 'August', 'September', 'October', 'November','December','January', 'February','March','April', 'May','June')
And Fiscal = 2014 GROUP BY Committee, Activity order by Committee, Activity
0
 
LVL 1

Expert Comment

by:Pooja Katiyar Verma
ID: 40225006
Rest of the query seems ok, you can remove month condition (as you are looking for all 12 months data)
0
 

Author Comment

by:al4629740
ID: 40225296
Why would I move distinct? Doesn't that make committee distinct if I did that?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:al4629740
ID: 40225578
What I want is distinct IDs of the students that had any totals.  I tested the change and believe that my original still is the right one.

Let me know your thoughts please
0
 
LVL 8

Expert Comment

by:5teveo
ID: 40225796
Do you want to show the detail ID# numbers and to show the counts of grouped committee and activities? Both at the same time? If yes - then these are reporting issues and not data selection issues.

What does a result record look like? provide an example.
0
 

Author Comment

by:al4629740
ID: 40239143
Here is what worked

SELECT r.Sector, count(Distinct r.RegID) 
                FROM tblOrgRegistrations AS r 
                      LEFT JOIN tblOrgHours AS h 
                            ON h.RegID = r.RegID 
                           AND Month(ActivityDate) = 7 And r.Board = 1 
                            LEFT JOIN tblOrgActivities AS a 
                            ON h.ActivityID = a.ActivityID And h.Hours > 0 
                     LEFT JOIN tblOrgProfile AS o 
                            ON h.AgencyID = o.AgencyID Where o.AgencyID = 74 And a.ActivityName = 'Board Meeting' 
                        Group By r.Sector order by r.Sector

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 750 total points
ID: 40239987
That looks familiar and I am sorry to see that you still incorrectly using a LEFT JOIN.  In other words, this:
LEFT JOIN tblOrgActivities AS a ON h.ActivityID = a.ActivityID  AND h.Hours > 0
and this:
WHERE   ...
        AND a.ActivityName = 'Board Meeting'
Means that you are in fact doing an (implicit) INNER JOIN and not a LEFT JOIN.

But you can confirm this by inspecting the results and you will see that all the a.ActivityName are "Board Meeting".  If that is what you really want than you should change the LEFT JOIN to an INNER JOIN

Also, aliasing all your columns is strongly suggested (see ActivityDate), so that you are not the only one that can read the query,
0
 

Author Comment

by:al4629740
ID: 40254442
Can you explain further how you determined I was doing an implicit INNER JOIN from this section?

LEFT JOIN tblOrgActivities AS a ON h.ActivityID = a.ActivityID  AND h.Hours > 0
and this:
WHERE   ...
        AND a.ActivityName = 'Board Meeting'
Means that you are in fact doing an (implicit) INNER JOIN and not a LEFT JOIN.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40254706
Can you explain further how you determined I was doing an implicit INNER JOIN from this section?
The best way to explain this is for you to test it out for yourself.  Here is a very simple example:
DECLARE @Table1 TABLE (
		ID integer NOT NULL PRIMARY KEY,
		Item varchar(20))


DECLARE @Table2 TABLE (
		ID integer NOT NULL PRIMARY KEY,
		ItemType varchar(20))


INSERT	@Table1 (ID, Item)
VALUES	(1, 'Item 1'),
		(2, 'Item 2'),
		(3, 'Item 3'),
		(4, 'Item 4'),
		(5, 'Item 5'),
		(6, 'Item 6'),
		(7, 'Item 7'),
		(8, 'Item 8')

INSERT	@Table2 (ID, ItemType)
VALUES	(2, 'Item Type 1'),
		(3, 'Item Type 1'),
		(5, 'Item Type 2'),
		(8, 'Item Type 2')

-- 1
SELECT *
FROM	@Table1 t1
		INNER JOIN @Table2 t2 ON t1.ID = t2.ID
WHERE	t2.ItemType = 'Item Type 2'


-- 2 
SELECT *
FROM	@Table1 t1
		LEFT JOIN @Table2 t2 ON t1.ID = t2.ID
WHERE	t2.ItemType = 'Item Type 2'


-- 3
SELECT *
FROM	@Table1 t1
		LEFT JOIN @Table2 t2 ON t1.ID = t2.ID AND t2.ItemType = 'Item Type 2'

Open in new window


Note how the results from Query 1 and 2 are the same, even though 2 is written as an LEFT  JOIN,  If you really want a LEFT JOIN than the correct way to write it, is as Query 3.

If you are still not convinced, look at the Execution plan (press Ctrl+M before executing F5) for all three queries.  Again Query 1 and 2 are identical.  Note how they both read as Nested Loops (Inner Join) and the last one as Nested Loops (Left Outer Join) .
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

926 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