December 8, 2003 at 5:38 am
Hi,
I administer an SQL server database with an Access front end. The main table has 6000 directory entries, each entry has a category ID between 1 and 30. I'm trying to make a T-SQL stored procedure that returns a grid with the number entries in each category. I can use the COUNT function to calculate the number in any particular category, but not a breakdown of each category. Can someone please advise me how to do this? Do I need to use a loop and store the values in variables? If so how?
Thanks for your time,
Ben
December 8, 2003 at 5:46 am
Not sure if I understand this, but would a look in BOL for 'ROLLUP' help.
Anyway, if you use Access as front end, why not take advantage of the grouping functionality in a report?
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 8, 2003 at 6:18 am
Unless I misunderstand, wouldn't something like this be what your after:
select
CategoryID,
Count(*) as [Count]
from TableName
group by
CategoryID
Edited by - DavidT on 12/08/2003 06:19:13 AM
December 8, 2003 at 6:26 am
That's what I first thought, too and that's where my lack of english leaves me insecure.
Let's wait and see.
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 8, 2003 at 7:38 am
Thanks guys,
As usual it was a lot more simple than I thought.
The following statement achieves exactly what I was after.
SELECT categoryID, COUNT(CategoryID) AS total
FROM entry
GROUP BY categoryID
ORDER BY categoryID ASC
I now need this data logging every day to a new table, whats the best way of doing this?
Thanks again,
Ben
December 8, 2003 at 7:59 am
Why does it need to be a new table?
Can't you just use an additional datetime column to store the information and query this.
Frank
Edited by - Frank kalis on 12/08/2003 08:00:11 AM
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 9, 2003 at 6:27 am
Agree with Frank.
(Wow did I really say that )
But if you do want to put the results in another table then
INSERT INTO LogTable
(LogDate,CategoryID,total)
SELECT GETDATE(), categoryID, COUNT(CategoryID) AS total
...
Far away is close at hand in the images of elsewhere.
Anon.
December 9, 2003 at 6:29 am
Bah, to speak with my son:
quote:
You're not my friend anymore!
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 9, 2003 at 7:45 am
When I was doing Access/SQL work I set up a passthrough query to SQL that ran my record aggregate stored procedure.
I then set up a cross-tab query that used the passthrough query as its source. This gave me the advantage of the speed of SQL together with the flexibility of the Access crosstab query.
I tended to let Access do the sorting as well.
Where you can guarantee that your aggregating data doesn't contain NULLs you can use WITH CUBE or WITH ROLLUP and do a select for particular NULL columns to bring back your results.
He was not wholly unware of the potential lack of insignificance.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply