Database Management
 
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 ForumsDatabasesDatabase Management

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 October 5th, 2011, 04:32 AM
GianniP GianniP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 GianniP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 20 sec
Reputation Power: 0
Best solution for this query

Hi

I would like to write a query to return a number associated to the closest match of another.

I'll try and explain properly.

I have 4 rows: 250,450,650,850

Each of those has a value attached e.g. 1,4,6,9

I would like to return 1 if someone chosses a number between 250 and 450, 4 if 450 to 650 etc

Appreciate any help

Reply With Quote
  #2  
Old October 5th, 2011, 05:33 AM
recyan recyan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 116 recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 10 h 47 m 4 sec
Reputation Power: 70
Hi,

just see if below gives some guidelines :

Code:
SELECT top 1 
	mytable.testNumber
	,mytable.testAssociate
FROM 
	mytable
WHERE 
	(((mytable.testNumber)<800))
ORDER BY 
	mytable.testNumber DESC;


Thanks
Comments on this post
GianniP agrees!

Reply With Quote
  #3  
Old October 5th, 2011, 07:45 AM
GianniP GianniP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 GianniP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 20 sec
Reputation Power: 0
I think I'm not explaining it correctly.

The table looks like this

-------------------------
id | volume | amount |
-------------------------
1 250 2
2 450 3.5
3 650 4
4 850 6
-------------------------

The web form allows for the input of any number form 250. If the person enters 346 for example, the query needs to return 2. If it's 451 it needs to return 3.5 etc

Reply With Quote
  #4  
Old October 5th, 2011, 08:00 AM
recyan recyan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 116 recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 10 h 47 m 4 sec
Reputation Power: 70
Quote:
Originally Posted by GianniP
I think I'm not explaining it correctly.

The table looks like this

-------------------------
id | volume | amount |
-------------------------
1 250 2
2 450 3.5
3 650 4
4 850 6
-------------------------

The web form allows for the input of any number form 250. If the person enters 346 for example, the query needs to return 2. If it's 451 it needs to return 3.5 etc


What happened when u tried the earlier posted query, something like below ?

Code:
SELECT TOP 1 
mytable.[volume]
,mytable.[amount]
FROM 
mytable
WHERE 
(((mytable.[volume])<800))
ORDER BY 
mytable.[volume] DESC;


The

800

is the form input value which is passed to the query

Thanks

Reply With Quote
  #5  
Old October 5th, 2011, 08:20 AM
GianniP GianniP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 GianniP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 20 sec
Reputation Power: 0
:-) Sorry..

It almost works but if I put in 650 it then would select 450 rather than 650 which IS present in the db. So I assume it needs to check if there is such a volume and if not, go with the lower volume.

Make sense ?

Reply With Quote
  #6  
Old October 5th, 2011, 08:22 AM
GianniP GianniP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 GianniP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 20 sec
Reputation Power: 0
changed it to (((OilQuantityPrice.OilQuantityVolume)<=800))

will test some more but I think that should do it.. thanks very much

Reply With Quote
  #7  
Old October 6th, 2011, 10:58 PM
recyan recyan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 116 recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level)recyan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 10 h 47 m 4 sec
Reputation Power: 70
Quote:
Originally Posted by GianniP
changed it to (((OilQuantityPrice.OilQuantityVolume)<=800))

will test some more but I think that should do it.. thanks very much


Glad you found it helpful.
Noticed title of your post "Best solution for this query".
I really donot know whether what I posted is the best solution or not.

Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Best solution for this query

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