A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Subquery to return distinct count



 
 
Thread Tools Display Modes
  #1  
Old July 21st, 2008, 10:03 AM posted to microsoft.public.access.queries
Max
external usenet poster
 
Posts: 410
Default Subquery to return distinct count

I am having table with Data

Year-Trainer-Event
2007-X-A
2007-X-B
2008-X-C
2008-X-A

i want count of Trainer by year wise such that Person X count for the year
2007 would be 1 and not 2, same for next year

Please suggest the way, i tried sub query but it fails

thanx

  #2  
Old July 21st, 2008, 10:11 AM posted to microsoft.public.access.queries
Stefan Hoffmann
external usenet poster
 
Posts: 709
Default Subquery to return distinct count

hi Max,

Max wrote:
Year-Trainer-Event
2007-X-A
2007-X-B
2008-X-C
2008-X-A

i want count of Trainer by year wise such that Person X count for the year
2007 would be 1 and not 2, same for next year

Copy this into a new query (SQL view):

SELECT [Year], [Trainer], Count(*) AS EventCount
FROM [yourTable]
GROUP BY [Year], [Trainer]


mfG
-- stefan --
  #3  
Old July 21st, 2008, 10:43 AM posted to microsoft.public.access.queries
Max
external usenet poster
 
Posts: 410
Default Subquery to return distinct count

Hi Stefan

thanx for reply, but my issues remain same i want the following output

Year-Total-Trainers
2007-1
2008-1

as Trainer X is doing differnet event, i want Trainer Count and not the
event count
please suggest

"Stefan Hoffmann" wrote:

hi Max,

Max wrote:
Year-Trainer-Event
2007-X-A
2007-X-B
2008-X-C
2008-X-A

i want count of Trainer by year wise such that Person X count for the year
2007 would be 1 and not 2, same for next year

Copy this into a new query (SQL view):

SELECT [Year], [Trainer], Count(*) AS EventCount
FROM [yourTable]
GROUP BY [Year], [Trainer]


mfG
-- stefan --

  #4  
Old July 21st, 2008, 11:14 AM posted to microsoft.public.access.queries
steve dassin
external usenet poster
 
Posts: 10
Default Subquery to return distinct count

Hi,

He's looking for the distinct count of Trainer. I forgot if Access has a
Count(Distinct [Trainer])

www.beyondsql.blogspot.com

"Stefan Hoffmann" wrote in message
...
hi Max,

Max wrote:
Year-Trainer-Event
2007-X-A
2007-X-B
2008-X-C
2008-X-A

i want count of Trainer by year wise such that Person X count for the
year 2007 would be 1 and not 2, same for next year

Copy this into a new query (SQL view):

SELECT [Year], [Trainer], Count(*) AS EventCount
FROM [yourTable]
GROUP BY [Year], [Trainer]


mfG
-- stefan --



  #5  
Old July 21st, 2008, 11:39 AM posted to microsoft.public.access.queries
Bob Barrows [MVP]
external usenet poster
 
Posts: 441
Default Subquery to return distinct count

Max wrote:
I am having table with Data

Year-Trainer-Event
2007-X-A
2007-X-B
2008-X-C
2008-X-A

i want count of Trainer by year wise such that Person X count for the
year 2007 would be 1 and not 2, same for next year

Please suggest the way, i tried sub query but it fails

Start by creating a query to return unique events by year and trainer:

SELECT DISTINCT [Year],Trainer,[Event from yourtable

Now either save that query, calling it something like UniqueEvents and use
it as the source for a new query:
SELECT [Year], [Trainer], Count(*) AS EventCount
FROM UniqueEvents
GROUP BY [Year], [Trainer]

Or, use it in a subquery:


SELECT [Year], [Trainer], Count(*) AS EventCount
FROM
(SELECT DISTINCT [Year],Trainer,[Event from yourtable) As q
GROUP BY [Year], [Trainer]

I would do the latter if this were the only query that needed unique events
per trainer.


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


  #6  
Old July 21st, 2008, 12:30 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 2,364
Default Subquery to return distinct count

SELECT [Year], Trainer, Count(Trainer)
FROM (SELECT DISTINCT YourTable.Year, Trainer
FROM YourTable)

If your field names and table names will not allow the above structure,
you will need to use two queries to get the desired result. Table and
field names should consist of only letters, numbers and the underscore
character. Also, avoid the use of reserved words such as Year, Month,
Date, etc.

SELECT DISTINCT YourTable.Year
, Trainer
FROM YourTable

Now use that query as the source to get the distinct count
SELECT [Year], Trainer, Count(Trainer)
FROM TheSavedQuery

'================================================= ===
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'================================================= ===


Max wrote:
I am having table with Data

Year-Trainer-Event
2007-X-A
2007-X-B
2008-X-C
2008-X-A

i want count of Trainer by year wise such that Person X count for the year
2007 would be 1 and not 2, same for next year

Please suggest the way, i tried sub query but it fails

thanx

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 07:49 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.