 |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
 | This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 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
|
|
|
|
|

February 16th, 2009, 11:52 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Wrox Blogs - Search
Hi,
I am working on adding functionality to search blogs from BlogEntry table.
I would like to select top 20 rows along with count of number of rows it is selecting.
The following SQL (MS SQL 2000) statement gives me errors.
What am I doing wrong here?
SELECT TOP 20 Id, Title, Body,
CategoryID, DatePublished, PostedBy, CommentCount, searchkeywords, isenabled, count(ID) as BlogCount
FROM BlogEntry WHERE isEnabled=1
GROUP BY DatePublished DESC
(This is part of a long dynamic SQL statement...for simplicity I am posting only part that gives me problems..)
Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'DESC'.
Last edited by norman001; February 16th, 2009 at 11:54 PM..
|
|

February 17th, 2009, 06:08 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
GROUP BY doesn't take a sorting direction; ORDER BY does...
GROUP BY DatePublished DESC
should be
ORDER BY DatePublished DESC
Imar
|
|

February 17th, 2009, 10:06 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Wrox Blogs - Search
Thanks! I changed the GROUP BY to ORDER BY then I get the following errors..
SELECT TOP 20 Id, Title, Body,
CategoryID, DatePublished, PostedBy, CommentCount, searchkeywords, isenabled, count(ID) as BlogCount
FROM BlogEntry WHERE isEnabled=1
ORDER BY DatePublished DESC
ERRORS:
Server: Msg 8118, Level 16, State 1, Line 1
Column 'BlogEntry.CommentCount' is invalid in the select list because it is
not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'BlogEntry.searchkeywords' is invalid in the select list because it is
not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'BlogEntry.isenabled' is invalid in the select list because it is
not contained in an aggregate function and there is no GROUP BY clause.
|
|

February 17th, 2009, 01:52 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
If you do a COUNT, you also need to use a GROUP BY:
SELECT TOP 20 Id, Title, Body,
CategoryID, DatePublished, PostedBy, CommentCount, searchkeywords, isenabled, count(ID) as BlogCount
FROM BlogEntry WHERE isEnabled=1
GROUP BY Id, Title, Body,
CategoryID, DatePublished, PostedBy, CommentCount, searchkeywords, isenabled
ORDER BY DatePublished DESC
However, this may result in a few more problems. First, the WHERE clasue. You may need to replace it with a HAVING clause:
HAVING isEnabled=1
Secondly, the Body. Depending on the underlying data type (text for example), you can't use it in a GROUP by clause.
Finally, what does this query do? What's the point of returning the count of the table's ID?
You may want to look up GROUP BY in the SQL Server Books Online. It shows you a number of useful examples.
Cheers,
Imar
|
|
 |