Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2005-06-07 : 19:40:10
|
I'm just modifying the ORDER BY clause on a pre-existing working query and getting the following error:Server: Msg 207, Level 16, State 3, Procedure select_Media_FrontPage_TopRated, Line 8Invalid column name 'votes'.When I order by VOTES or POINTS on their own, rather than (points/votes) the query works fine. Is this a hint to any possible problem?Thanks once again! :)mike123alter PROCEDURE dbo.select_Media_FrontPage_TopRated AS SET NOCOUNT ONSELECT M.mediaID, pageName, mediaTitle, mediaDesc, datePosted, COUNT(UV.mediaID) AS votes, SUM ( CASE WHEN points IS NULL THEN 0 ELSE points END ) as pointsFROM tblMedia M LEFT OUTER JOIN tblUserVote UV ON UV.mediaID = M.mediaIDWHERE m.activeStatus ='1'GROUP BY M.mediaID, pageName, mediaTitle, mediaDesc, datePostedORDER BY (points / votes) DESCGO |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2005-06-07 : 19:43:02
|
Votes is a calcuation, not an actual column, so it cannot participate in another calculation. Either replace it in the ORDER BY with Count(UV.mediaID) or wrap the whole thing in a subquery, then put the ORDER BY outside of it.Don't ask me why it worked before, it shouldn't have. |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2005-06-07 : 20:11:31
|
Hey Rob thanks for the hand.I added what you saidCount(UV.mediaID) Server: Msg 8127, Level 16, State 1, Procedure select_Media_FrontPage_TopRated, Line 8Column name 'UV.points' is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.I got this error and then after I fix what it recommends my query ends up bringing back 258 rows instead of the 40 it should. It's doing this because of something screwy in the groupby clause I believe.I'd like to try your subquery method but I haven't done one in so long I'm not sure I understand the approach on this one. One other thing that I realized I needed to add a simple "WHERE VOTES > 5" , simple obviously in most cases but it was causing me complications in this one.Thanks very much once again!mike123 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2005-06-07 : 20:22:21
|
This should do it:alter PROCEDURE dbo.select_Media_FrontPage_TopRated AS SET NOCOUNT ONSELECT * FROM (SELECT M.mediaID, pageName, mediaTitle,mediaDesc, datePosted,COUNT(UV.mediaID) AS votes, SUM ( CASE WHEN points IS NULL THEN 0 ELSE points END ) as pointsFROM tblMedia MLEFT OUTER JOIN tblUserVote UV ON UV.mediaID = M.mediaIDWHERE m.activeStatus ='1'GROUP BY M.mediaID, pageName, mediaTitle, mediaDesc, datePosted ) aORDER BY (a.points / a.votes) DESC |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2005-06-07 : 22:51:25
|
thanks alot rob! .. just one quick question, is it ok to do SELECT *'s on subquery's ? Or are you just writing it quickly and I should know better? :)thx! |
|
|
robvolk
Most Valuable Yak
15732 Posts |
|
|
|
|
|
|