Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

finding records in table using the count()

Posted on 2014-07-28
9
Medium Priority
?
116 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 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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

715 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