Re: SELECT COUNT, but only on the last 100 entries
From: Bob Barrows [MVP] (reb01501_at_NOyahoo.SPAMcom)
Date: 08/19/04
- Next message: Bob Barrows [MVP]: "Re: Access 2000 and creating stored procedures"
- Previous message: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- In reply to: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- Next in thread: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- Reply: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 19 Aug 2004 18:09:35 -0400
Dan Nash wrote:
> Bob,
>
> Ok, I'll try and give you some more info.
>
> I have 3 tables, each setup with keys etc.
>
> Questions - this holds the list of questions (subject, question) with
> a question_id field (key). It also has a subcategory_id field, which
> relates to...
>
> SubCategories - info about sub categories (subcategory name) with a
> subcategory_id field (key). It also has a category_id field, which
> relates to...
>
> ..you guessed it, Categories - info about categories (category name)
> with a category_id (key).
>
> So basically, a Category can have a number of SubCategories, which
> can have a number of Questions. I've got all the relationships setup
> in Access, so I can click the + buttons and drill down and see the
> data.
>
> Right, from ASP, I want to be able to get the last 100 Questions
> (SELECT * FROM Questions ORDER BY question_id DESC). Simple enough.
SELECT TOP 100 * FROM Questions ORDER BY question_id DESC
>
> But what I really want it to do is give me a descending list of the
> Categories, based on how many Questions are in it. SO basically...
> Category
> 1.. 23 questions in 5 subcategories, Category 2, 20 questions in 7
> subcategories, Category 3.. 19 questions in 3 subcategories, and so
> on.
>
First of all, forget about using SELECT * in production code. It's OK for
quick testing, but for production, you want to be explicit.
This should work in both SQL Server and Access (untested):
SELECT c.[category name], Count(*) As CategoryCount
FROM (
(SELECT TOP 100 subcategory_id FROM Questions
ORDER BY question_id DESC) As q INNER JOIN SubCategories s
ON q.subcategory_id = s.subcategory_id)
INNER JOIN Categories c ON s.category_id = c.category_id
GROUP BY c.[category name]
ORDER BY CategoryCount DESC
Bob Barrows
-- 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"
- Next message: Bob Barrows [MVP]: "Re: Access 2000 and creating stored procedures"
- Previous message: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- In reply to: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- Next in thread: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- Reply: Dan Nash: "Re: SELECT COUNT, but only on the last 100 entries"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|