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 » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Count occurrences



 
 
Thread Tools Display Modes
  #1  
Old January 22nd, 2010, 06:47 PM posted to microsoft.public.access.gettingstarted
Lorri
external usenet poster
 
Posts: 26
Default Count occurrences

Hello all,
I am very new to Access, am doing an online course and learning as I go.
I am stuck with one query and I'm hoping someone can help me.
I have a sales table for which I am required to produce a query which tells
the most popular artists and the number of sales they have made.
The fields in the table a-
Invoice number, product code, artist code, date, payment type.
The query is on the Artist Code field, but for the life of me I cannot get
it to show just the top 5 Artists and their sales count.
Hoping for help - written slowly 'cos I'm very new at this.
Thanks in advance.
  #2  
Old January 22nd, 2010, 07:15 PM posted to microsoft.public.access.gettingstarted
Dirk Goldgar
external usenet poster
 
Posts: 2,529
Default Count occurrences

"Lorri" wrote in message
...
Hello all,
I am very new to Access, am doing an online course and learning as I go.
I am stuck with one query and I'm hoping someone can help me.
I have a sales table for which I am required to produce a query which
tells
the most popular artists and the number of sales they have made.
The fields in the table a-
Invoice number, product code, artist code, date, payment type.
The query is on the Artist Code field, but for the life of me I cannot get
it to show just the top 5 Artists and their sales count.
Hoping for help - written slowly 'cos I'm very new at this.
Thanks in advance.



Assuming that each record in your Sales table represents a sale for a given
artist, then first you need a totals query that returns the artist code and
the count of records for each artist code. That will be the sales count.
You can make such a query in the query designer by creating a new query
based on the Sales table. Click the "Totals" button on the toolbar (the one
with the caption that is the Greek sigma -- looks like a big, stylized "E")
to make it into a totals query. Drag the [Artist Code] to the field grid
twice. On the "Total:" row under the first column, choose "Group By", and
under the second column for the field, choose "Count". Give that field an
alias of SalesCount by changing the field name so that it looks like this:

SalesCount: [Artist Code]

At this point, if you switch into SQL view, the query's SQL looks something
like this:

SELECT [Artist Code], Count([Artist Code] As SalesCount
FROM Sales
GROUP BY [Artist Code];

If you flip into datasheet view, you'll see the raw results, ordered by
artist code.

Now, to get the top 5, we need to rank these results in descending order by
the calculated field SalesCount. Switch back to design view and on the
"Sort: row" of the field grid, under SalesData, choose "Descending". The
SQL view of the query would now look like this:

SELECT [Artist Code], Count([Artist Code] As SalesCount
FROM Sales
GROUP BY [Artist Code]
ORDER BY Count([Artist Code]) DESC;

Finally, we need just the top 5. So in design view, go to the "Top Values"
dropdown box on the toolbar and enter or choose 5. Flip to SQL view and it
should now look like this:

SELECT TOP 5 [Artist Code], Count([Artist Code] As SalesCount
FROM Sales
GROUP BY [Artist Code]
ORDER BY Count([Artist Code]) DESC;

If you switch to datasheet view, you should see that it gives you the
desired result.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 




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 08:05 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.