Solved

finding records in table using the count()

Posted on 2014-07-28
9
114 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
[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
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Assisted Solution

by:5teveo
5teveo earned 250 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

738 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