Solved

finding records in table using the count()

Posted on 2014-07-28
9
110 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

912 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

18 Experts available now in Live!

Get 1:1 Help Now