Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 20th, 2003, 07:42 AM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL query help

OK, this may be easy to do or it may be impossible but I really don't know how to do this.

Basically, I have just one table which is the output from a log file. I want to count the number of entries for each day.

I can do the following :-

SELECT COUNT(*) FROM log

This gives me the count for every day. I can also do:-

SELECT DISTINCT CAST(CONVERT(CHAR(10), logDate, 101) AS datetime FROM log

which gives me each date. My question is:-

How do I combine these two queries to give me the number of log entries for each distinct day ?
 
Old October 20th, 2003, 07:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

I think this'll do it:

SELECT DISTINCT CAST(CONVERT(CHAR(10), logDate, 101) AS datetime, COUNT(*) FROM log GROUP BY CAST(CONVERT(CHAR(10), logDate, 101)

 
Old October 20th, 2003, 07:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

COUNT(*) counts the number of rows in each GROUP BY group. If you do not have a GROUP BY clause, then the entire query is considered to be one big group, which is why COUNT(*) gives the number of rows in the query.

Thus, you want to GROUP by the date, as:

SELECT TheDate, COUNT(*) FROM table GROUP BY TheDate

To group by your expression, your query will be a bit ugly, as you will have to repeat the expression in both the SELECT and the GROUP BY clause:
Code:
SELECT CAST(CONVERT(CHAR(10), logDate, 101) as TheDate, COUNT(*) FROM Log
GROUP BY CAST(CONVERT(CHAR(10), logDate, 101)
ORDER BY TheDate
Note that you do not have to repeat the expression in an ORDER BY clause, as any column alias's are visible to the ORDER BY clause (but not the GROUP BY clause).

P.S. Don't use a datatype name such as 'datetime' as a column name - eventually you'll regret it.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 20th, 2003, 08:22 AM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff, Jonax,

You are both right and the query now works wonderfully.

Didn't think of the GROUP BY clause, obvious now though.

Thanks for the help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query i need seearam MySQL 7 November 30th, 2008 03:14 AM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
SQL Query!! dpkbahuguna Beginning VB 6 5 October 12th, 2007 12:39 AM
Help with SQL query sattaluri Access 2 August 11th, 2006 09:26 AM
SQL query PinkyCat Classic ASP Databases 3 March 11th, 2005 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.