Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frequent DB access?

Status
Not open for further replies.

camillaj

Programmer
Sep 18, 2003
31
AU
I am giving a graphical report on what is stored in the DB.
I am not that experienced though with databases, so i'm afraid I have too many DB access.

In one table I want to count the number of rows for each of '1','2','3','4' and '5'. Such as:

SELECT Count(*) FROM table WHERE level='3'.

Should I perform 5 such select queries or retrieve all rows in one select and then go through each in a loop while counting each of them?

NB! It's not going to be accessed by many users at the same time!

Thanks for tips
 
basic SQL has a built in way to do what you want to do: GROUP BY

Code:
SELECT Level, Count(*) AS Total FROM table GROUP BY Level

Not sure what your database is, but here are a few FAQs from the SQL Server forum:
Where can I find training/tutorials for SQL and T-SQL?
faq183-694
SQL Websites
faq183-513
Useful Reference Books for SQL Server Professionals
faq183-3324

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top