Wrox Programmer Forums
|
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
 
Old February 16th, 2009, 11:52 PM
Authorized User
 
Join Date: Apr 2008
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Default 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..
 
Old February 17th, 2009, 06:08 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

GROUP BY doesn't take a sorting direction; ORDER BY does...

GROUP BY DatePublished DESC

should be

ORDER BY DatePublished DESC

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 17th, 2009, 10:06 AM
Authorized User
 
Join Date: Apr 2008
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Default 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.
 
Old February 17th, 2009, 01:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Engine Optimize from Wrox?? wongsak All Other Wrox Books 1 September 24th, 2006 07:21 PM
Wrox.com search is a bit too pedantic richard.york Forum and Wrox.com Feedback 0 June 23rd, 2006 10:14 PM
SQL Blogs SqlMenace SQL Language 0 September 20th, 2005 12:28 PM
Error in wrox search Page [email protected] Forum and Wrox.com Feedback 7 July 19th, 2004 10:17 PM
wrox.com search TomPaul Forum and Wrox.com Feedback 2 January 14th, 2004 02:14 PM





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