MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 19th, 2011, 04:38 AM
ColumK ColumK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 8 ColumK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 23 sec
Reputation Power: 0
Multiple Select Statements to Order Data

Good Morning,

New to the forums, I am having a spot of trouble with some code I am writing. First let me give you a breakdown.

I am creating a Ranking module for one of the companies websites. These will show the historical rankings of the keywords so I first order it by date. The problem with this is that as there could be multiple urls ranking for that specific keyword I am then trying to order it by rank first before grouping by keyword (to obtain the highest ranking page to be displayed first).

My code all works except for when i try to order it by ranking first by using a second select query:

Code:
SELECT MAX(Date) AS date, Keyword, URL, Ranking, Traffic, Value FROM (SELECT * FROM $Table ORDER BY Ranking ASC) GROUP BY Keyword ORDER BY Value DESC


I am wondering if i have the syntax all wrong? Any help would be appreciate.

Reply With Quote
  #2  
Old August 19th, 2011, 05:02 AM
ColumK ColumK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 8 ColumK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 23 sec
Reputation Power: 0
Figured out the problem. I had to specify that it was in fact a new table by putting as tablename:

SELECT MAX(Date) AS date, Keyword, URL, Ranking, Traffic, Value FROM (SELECT * FROM $Table ORDER BY Ranking ASC) as rankingordered GROUP BY Keyword ORDER BY Value DESC

Hope that helps someone who had the same problem.

Reply With Quote
  #3  
Old August 19th, 2011, 11:15 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 33 m 44 sec
Reputation Power: 4140
what you have coded, will only work in mysql

therefore let's move this thread to the mysql forum

now the bad news: the URL that you display beside the MAX(date) is not necessarily the URL that is actually on the same row as the MAX(date)

read this: http://dev.mysql.com/doc/refman/5.0...en-columns.html
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #4  
Old August 22nd, 2011, 03:10 AM
ColumK ColumK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 8 ColumK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 23 sec
Reputation Power: 0
Quote:
Originally Posted by r937
what you have coded, will only work in mysql

therefore let's move this thread to the mysql forum

now the bad news: the URL that you display beside the MAX(date) is not necessarily the URL that is actually on the same row as the MAX(date)


Yes I have come to realize this. The data is looking fairly skewed. Could I ask if anyone has come up with a solution to this, or perhaps there is a better way to organize the data?

Reply With Quote
  #5  
Old August 22nd, 2011, 03:14 AM
ColumK ColumK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 8 ColumK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 23 sec
Reputation Power: 0
Would doing something like:

SELECT FROM (SELECT FROM $table Order By Date ASC And Ranking DESC) Group By Keyword

Work? i.e. does the keyword now group to the first instance of that keyword as it is now ordered by date and then ordered by rankings?

Reply With Quote
  #6  
Old August 22nd, 2011, 03:39 AM
ColumK ColumK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 8 ColumK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 23 sec
Reputation Power: 0
This fixed it:

Code:
SELECT * FROM (SELECT * FROM (SELECT * FROM $Table ORDER BY DATE DESC , Ranking ASC) AS Rankings GROUP BY Keyword) AS GroupedKW ORDER BY Value DESC

Reply With Quote
  #7  
Old August 22nd, 2011, 05:03 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 33 m 44 sec
Reputation Power: 4140
Quote:
Originally Posted by ColumK
This fixed it:
no, sorry, it might look fixed, but i assure you, when you have "hidden" columns in the SELECT clause (i.e. columns that aren't also in the GROUP BY clause, as explained by that link i gave you), you are not guaranteed to get the results you think you're getting

Reply With Quote
  #8  
Old August 22nd, 2011, 08:33 AM
ColumK ColumK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 8 ColumK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 23 sec
Reputation Power: 0
Quote:
Originally Posted by r937
no, sorry, it might look fixed, but i assure you, when you have "hidden" columns in the SELECT clause (i.e. columns that aren't also in the GROUP BY clause, as explained by that link i gave you), you are not guaranteed to get the results you think you're getting


So do you have a solution? Because trial running 100 keywords seems to output the correct data :S

Reply With Quote
  #9  
Old August 22nd, 2011, 08:57 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 33 m 44 sec
Reputation Power: 4140
Quote:
Originally Posted by ColumK
So do you have a solution?
would you kindly restate the problem?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Multiple Select Statements to Order Data

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap