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 |
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-06-19 : 19:09:12
|
Hi, i've run into a problem with a nested query. Consider this:SELECT VehicleRef, Type, Manufacturer, Model, Derivative, isLimited, Term, MilesPA, CH FROM vwVehicles WHERE type='Car' AND manufacturer='Audi' AND model='A3'I get the following results1 Car Audi A3 1.6 Special Edition 3dr 0 2 10000 229.991 Car Audi A3 1.6 Special Edition 3dr 0 2 10000 252.991 Car Audi A3 1.6 Special Edition 3dr 0 3 10000 219.991 Car Audi A3 1.6 Special Edition 3dr 0 3 10000 242.991 Car Audi A3 1.6 Special Edition 3dr 0 2 20000 252.991 Car Audi A3 1.6 Special Edition 3dr 0 2 20000 296.991 Car Audi A3 1.6 Special Edition 3dr 0 3 20000 241.991 Car Audi A3 1.6 Special Edition 3dr 0 3 20000 291.991 Car Audi A3 1.6 Special Edition 3dr 0 2 30000 274.991 Car Audi A3 1.6 Special Edition 3dr 0 2 30000 340.991 Car Audi A3 1.6 Special Edition 3dr 0 3 30000 264.991 Car Audi A3 1.6 Special Edition 3dr 0 3 30000 339.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 2 10000 227.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 2 10000 249.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 3 10000 220.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 3 10000 244.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 2 20000 253.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 2 20000 298.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 3 20000 244.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 3 20000 292.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 2 30000 279.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 2 30000 344.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 3 30000 267.992 Car Audi A3 1.6 Special Edition Sportback 5dr 0 3 30000 339.99What I need is to only retrieve each vehicleID once, but with the best (lowest) CH price, I also need the term and mileage that corresponds with that price.I tried thisSELECT VehicleRef, Type, Manufacturer, Model, Derivative, isLimited, Term, MilesPA, CH FROM vwVehicles WHERE type='Car' AND manufacturer='Audi' AND model='A3' AND CH = (SELECT MIN(CH) FROM vwVehicles WHERE type='Car' AND manufacturer='Audi' AND model='A3' AND ch >=1 AND ch <=1000)This only returns 1 line though. Is there anyway to get the result I want using one query?Thanks |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-19 : 19:13:42
|
[code]SELECT VehicleRef, Type, Manufacturer, Model, Derivative, isLimited, Term, MilesPA, CH FROM vwVehicles vWHERE type = 'Car' AND manufacturer = 'Audi' AND model = 'A3' AND CH = ( SELECT MIN(CH) FROM vwVehicles x WHERE x.VehicleRef = v.VehicleRef AND x.ch >= 1 AND x.ch <= 1000 )[/code] KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-06-19 : 19:27:13
|
Or learn the new SQL Server 2005 T-SQL functions and commands...SELECT d.VehicleRef, d.Type, d.Manufacturer d.Model, d.Derivative, d.isLimited, d.Term, d.MilesPA, d.CHFROM ( SELECT VehicleRef, Type, Manufacturer Model, Derivative, isLimited, Term, MilesPA, CH, ROW_NUMBER() OVER (PARTITION BY Type, Manufacturer, Model ORDER BY CH) AS RecID FROM vwVehicles ) AS dWHERE d.Type = 'Car' AND d.Manufacturer = 'Audi' AND d.Model = 'A3' AND d.RecID = 1 Peter LarssonHelsingborg, Sweden |
 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-06-28 : 06:16:40
|
Thanks for this!I have used this statementSELECT VehicleRef, Type, Manufacturer, Model, Derivative, isLimited, Term, MilesPA, CH FROM vwVehicles vWHERE type = 'Car' AND manufacturer = 'Audi' AND model = 'A3' AND CH = ( SELECT MIN(CH) FROM vwVehicles x WHERE x.VehicleRef = v.VehicleRef AND x.ch >= 1 AND x.ch <= 1000 )It returns 432 rows, BUT there are only 428 unique vehicle references in the table.Also there is another column called PCH, if I change the statement and replace all the CH with PCH to get the lowest PCH prices it returns 462 rows.Where have a gone wrong?Thanks |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-28 : 06:29:46
|
May be you have more than one record which has the lowest price. |
 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-06-28 : 06:49:04
|
Can I get around this and just choose one |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-28 : 06:51:44
|
Since you need to select other fields with the lowest price,you have to decide how to choose which record with the lowest price. |
 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-06-28 : 07:40:07
|
It doesn't matter, any, or the first one returned perhaps? |
 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-06-28 : 18:43:41
|
SELECT MIN(CH) FROM vwVehicles x WHERE x.VehicleRef = v.VehicleRef AND x.ch >= 1 AND x.ch <= 1000If I wrap that inner statment in this one would it work?SELECT TOP 1 ()Thanks |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-28 : 22:31:22
|
What column combination makes your records unique? |
 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-07-01 : 19:12:01
|
Hi,I'm willing to pay someone on a consultancy basis to get this to work, my deadline is middle of week so panicking a bit now!If your interested please drop me a line ben at chillilime dot com |
 |
|
|
|
|
|
|