| Author |
Topic |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-07-11 : 07:49:56
|
| Example tableCustomer PriceSMITH 29.50JONES 39.75 BRADLEY 10.89MURPHY 12.78JACKS 35.60PETERSON 14.67 GORD 50.85PAUL 50.90I want to return the lowest price and the customer, so in this example my results should beBRADLEY 10.89I know I can use SELECT MIN (price) but how to I get the customer alsoThanks |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-07-11 : 07:54:16
|
| maybeselect min(customer), min(price)from YourTable_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-07-11 : 08:11:44
|
| [code]Select Customer, PriceFrom TableWhere Price = (Select Min(Price) from Table)[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-11 : 08:38:20
|
| select customer, (min)price from example tablegroup by customer, priceNeed an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-11 : 08:40:10
|
quote: Originally posted by b.veenings select customer, (min)price from example tablegroup by customer, priceNeed an SQLDB consultant?check www.veeningsengineering.nl
Adding price in the group by clause will give you different resultMadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-11 : 08:43:30
|
quote: Originally posted by b.veenings select customer, (min)price from example tablegroup by customer, priceNeed an SQLDB consultant?check www.veeningsengineering.nl
That would not give the min price and the customer. It basically will return all distinct combination of customer & price. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-11 : 08:58:12
|
| oops!Need an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-07-11 : 08:59:10
|
| well all our solutions are valid because we don't really know what the exact requirement is based on the description._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-11 : 09:07:30
|
| dunno if this works but is worth a try.select customer a, (min)price e from tablewhere e.price = a.customerNeed an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-11 : 09:09:36
|
| not really my day, forgot the inner join:-Pbut i think the following one is the most appropiate:[qoute]Select Customer, PriceFrom TableWhere Price = (Select Min(Price) from Table)Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"[/qoute]Need an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-11 : 09:35:33
|
quote: Originally posted by b.veenings dunno if this works but is worth a try.select customer a, (min)price e from tablewhere e.price = a.customerNeed an SQLDB consultant?check www.veeningsengineering.nl
I wonder if this type of syntax is available MadhivananFailing to plan is Planning to fail |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-11 : 09:41:38
|
what was in that coffee Need an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-07-11 : 12:00:15
|
| Thanks for suggestions,I thought the most suitable one wasSELECT Customer FROM Table WHERE Price = (SELECT MIN(Price) FROM Table)But how does SQL behave if two prices are both the MIN, for example if there were two different customers both with a price of 1.00? |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-11 : 12:09:06
|
| then you get them both back i suppose.Need an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-07-12 : 05:01:29
|
| In this scenario is there anyway to only return 1 record. Could I wrap the whole thing in a SELECT TOP 1 FROM ( ) Is that the best way? |
 |
|
|
b.veenings
Yak Posting Veteran
96 Posts |
Posted - 2007-07-12 : 05:05:57
|
| in that case you can choose to use top 1.Need an SQLDB consultant?check www.veeningsengineering.nl |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 05:08:34
|
quote: Originally posted by Mondeo In this scenario is there anyway to only return 1 record. Could I wrap the whole thing in a SELECT TOP 1 FROM ( ) Is that the best way?
If you only one 1 recordselect top 1 * from yourtableorder by price KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-12 : 05:29:59
|
quote: Originally posted by khtan
quote: Originally posted by Mondeo In this scenario is there anyway to only return 1 record. Could I wrap the whole thing in a SELECT TOP 1 FROM ( ) Is that the best way?
If you want only one 1 recordselect top 1 * from yourtableorder by price KH[spoiler]Time is always against us[/spoiler]
MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 05:33:29
|
Thanks Madhi  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-12 : 05:45:22
|
quote: Originally posted by khtan Thanks Madhi  KH[spoiler]Time is always against us[/spoiler]
I think you need some other tool that checks spellings Also, the color code macro deoesnt seem to work in EXCELCan you post equivalent code of EXCEL? MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 05:57:45
|
Excel ? You use excel to write your query ?  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Next Page
|