Solved

finding records in table using the count()

Posted on 2014-07-28
9
109 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 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now