PHP Development
 
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 ForumsProgramming LanguagesPHP Development

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 November 23rd, 2012, 07:17 PM
coffeeaddict coffeeaddict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 coffeeaddict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 46 m 37 sec
Reputation Power: 0
How to order closest to specific number?

I have a project I'm working on and I'm stuck on how to order results closest to a specific number.

I have a breed of dog with an ideal number. We will say 250

For that breed I have 5 different dogs all that have their own set of numbers. I then added those together and now have a single total for each of those 5 dogs.

Out of those 5 numbers, I need to order them by the closest to 250.

Now some may be below 250 or above 250.

Lets say my totals for these 5 dogs were

240
310
122
109
87


That right there is how I would want them ordered. Because 240 is closest to 250 and so on.

I know how to order by ascending or desceding, but not how to order according to a specific number. Help please?

Reply With Quote
  #2  
Old November 23rd, 2012, 07:32 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
In essence you need to create a calculated field in your query

PHP Code:
 abs(250) as `nearest


and then sort by this field.

Reply With Quote
  #3  
Old November 23rd, 2012, 07:50 PM
coffeeaddict coffeeaddict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 coffeeaddict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 46 m 37 sec
Reputation Power: 0
That makes sense! Thank you!

I have 40 different breeds, is there a shorter way to go about this for each one, other than typing out the code 40 times? Probably not, and it's not really a lot of code, but can't hurt to ask. Thanks again

Reply With Quote
  #4  
Old November 23rd, 2012, 07:53 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
Quote:
I have 40 different breeds, is there a shorter way to go about this for each one, other than typing out the code 40 times?


I don't understand why you would need to type the code 40 times - that's what variables, functions, and loops are for!

Reply With Quote
  #5  
Old November 23rd, 2012, 07:56 PM
coffeeaddict coffeeaddict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 coffeeaddict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 46 m 37 sec
Reputation Power: 0
Because each breed has a different number. So it's a different calculation for each one.

I am assuming I would need to go through and define the ideal numbers for each breed in the database, then add up the totals for each dog of those breeds, then find the difference between each dog's number and their ideal, and then order it. Correct?

But that finding the ideal number would have to be done 40 different times, right? Or am I missing something that's probably staring right at me?

Edited to add: I think I figured it out. I was thinking I would have to do a bunch of if statements for each breed. If doberman then ideal is 240, if collie then ideal is 400, etc. etc.

But I see now I don't need to do that.

Reply With Quote
  #6  
Old November 23rd, 2012, 08:26 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
I don't quite follow, but if you had another table listing the 40 breeds and their corresponding ideal value (or this information could be created and stored in a temporary array) then a query to obtain the data you need (for all 40 breeds) would be something like the following:

Code:
SELECT Breeds.Breed, Dogs.Dog, Sum(Abs([A]+[ B ]+[C]-[Ideal])) AS TotPerDog FROM Breeds INNER JOIN Dogs ON Breeds.Breed = Dogs.Breed GROUP BY Breeds.Breed, Dogs.Dog ORDER BY Sum(Abs([A]+[ B ]+[C]-[Ideal])) DESC;


(without the extra spaces around B)
N.B. this is an Access query, but the SQL will be similar.

Reply With Quote
  #7  
Old November 24th, 2012, 08:34 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
In MySQL, assuming you have:
DOG: ID, BREED_ID, NAME, VALUE, ETC
BREED: ID, NAME, IDEAL_VALUE

Code:
SELECT DOG.NAME, BREED.NAME FROM DOG JOIN BREED ON DOG.BREED_ID =  BREED.ID ORDER BY BREED.NAME, ABS(DOG.VALUE-BREED.IDEAL_VALUE);
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #8  
Old November 30th, 2012, 11:07 AM
coffeeaddict coffeeaddict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 coffeeaddict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 46 m 37 sec
Reputation Power: 0
I have been trying to test this out and would like some feedback on this snipit of code. So far, it doesn't quite seem to be working. Dogs that are not closest to their ideal are still being ordered first. I'm quite sure it's something I'm doing wrong, any suggestions are appreciated!

genetotal is the ideal number

Code:
$query .= "(D.head + D.ears + D.topline + D.front + D.rear + D.feet + D.health + D.temperament)) AS genetics,  					B.group  			  FROM entries E  			  INNER JOIN dogs D ON D.id = E.dog_id  			  INNER JOIN breeds B on B.id = D.breed  			  WHERE  			  		E.show_id = '{$show['id']}' AND  			  		('{$show['classification']}' = 1 OR E.nc = 1) 			  ORDER BY  			  		D.faults ASC,  					level DESC,  					ABS(genetics-B.genetotal) DESC";

Reply With Quote
  #9  
Old November 30th, 2012, 12:02 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
You're ordering by d.faults first. Don't do that.

Reply With Quote
  #10  
Old November 30th, 2012, 06:45 PM
coffeeaddict coffeeaddict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 coffeeaddict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 46 m 37 sec
Reputation Power: 0
Dogs with faults need to be automatically put last, I didn't think that would be causing the problem. Hmmm.

Everything else looks ok though?

Reply With Quote
  #11  
Old December 1st, 2012, 01:59 PM
coffeeaddict coffeeaddict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 coffeeaddict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 46 m 37 sec
Reputation Power: 0
Just wanted to update this and say it's working perfectly, I had to order the ABS by ASC. Thanks both of you who replied

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > How to order closest to specific number?

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