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 |
mjpr
Starting Member
7 Posts |
Posted - 2007-05-10 : 13:28:31
|
Hi all,I need some help with a query I have to buildI have 3 tables for make, model and version (cars):TblMarcas (make)TblModelos (model)TblVersoes (version)I filled 3 dependent dropdowns with the values from each table.But now, I need to filter it a bit.I need only to show makes that have certain types, lets say trucks and vans. And the model and version have to be dependent of the choice of the make.The problem is, in the make table I have IDMake and MakeName;in the Model: IDModel, Desc, IDMake;and in Version: IDVersion, Desc, IDModel, IDSubGrupoVeiculo (truck, van,sedan,etc)How do I do the drill down, showing only makes that have those types?I've tried:SELECT distinct m.IDMarca, m.NomeMarca FROM TblMarcas m, TblModelos mo, TblVersoes v WHERE m.IDMarca = mo.IDMarca ANDmo.IDModelo = v.IDModelo AND(v.IDSubGrupoVeiculo = '20' OR v.IDSubGrupoVeiculo = '22' OR v.IDSubGrupoVeiculo = '23' OR v.IDSubGrupoVeiculo = '28')but with no luck.Please help---Mário Ramos |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-05-10 : 13:30:35
|
Please post some sample data in each table and expected output for a given set of parameters.Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
mjpr
Starting Member
7 Posts |
Posted - 2007-05-10 : 13:50:49
|
quote: Originally posted by mjpr Hi all,I need some help with a query I have to buildI have 3 tables for make, model and version (cars):TblMarcas (make)TblModelos (model)TblVersoes (version)I filled 3 dependent dropdowns with the values from each table.But now, I need to filter it a bit.I need only to show makes that have certain types, lets say trucks and vans. And the model and version have to be dependent of the choice of the make.The problem is, in the make table I have IDMake and MakeName;in the Model: IDModel, Desc, IDMake;and in Version: IDVersion, Desc, IDModel, IDSubGrupoVeiculo (truck, van,sedan,etc)How do I do the drill down, showing only makes that have those types?I've tried:SELECT distinct m.IDMarca, m.NomeMarca FROM TblMarcas m, TblModelos mo, TblVersoes v WHERE m.IDMarca = mo.IDMarca ANDmo.IDModelo = v.IDModelo AND(v.IDSubGrupoVeiculo = '20' OR v.IDSubGrupoVeiculo = '22' OR v.IDSubGrupoVeiculo = '23' OR v.IDSubGrupoVeiculo = '28')but with no luck.Please help---Mário Ramos
Ok, some data from the 3 tables:TblMarcas|IDMarca |NomeMarca|1 |Alfa Romeo|3 |Aston Martin|4 |Audi|5 |Bentley|6 |BMW|8 |Caterham|9 |Citroën|11 |Chrysler|12 |Daewoo|13 |DaihatsuTblModelos|IDModelo |IDMarca |Descricao|1 |1 |145|2 |1 |146|3 |1 |156|4 |1 |166|5 |1 |GTV|6 |1 |Spider|7 |3 |DB7|8 |4 |A3|9 |4 |A4|10 |4 |A6TblVersoes|IDVersao |IDModelo |Descricao |IDSubGrupoVeiculos|6781 |1 |145 1.4 TS 16v |3|6782 |2 |146 1.4 TS 16v |3|6785 |3 |156 1.6 TS 16v |4|6787 |3 |156 1.8 TS 16v |4|6789 |3 |156 2.0 TS Selespeed |4|6790 |3 |156 2.5 V6 24v |4|6793 |3 |156 1.9 JTD |4|6795 |3 |156 2.4 JTD |4|6797 |4 |166 2.0 TS 16v |5|6798 |4 |166 2.0 V6 Turbo |5What I want is a query that will fill the make dropdown only with makes that have models that have versions with IDSubGrupoVeiculos = 4, for example---Mário Ramos |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-05-10 : 14:21:12
|
Something like this: ?select t.NomeMarca from TblVersoes tvleft join TblModelos TM on tv.IDModelo = tm.IDModeloleft join TblMarcas t on t.IDMarca = tm.IDMarca where IDSubGrupoVeiculos = 4group by t.NomeMarca Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
mjpr
Starting Member
7 Posts |
Posted - 2007-05-10 : 14:51:43
|
quote: Originally posted by dinakar Something like this: ?select t.NomeMarca from TblVersoes tvleft join TblModelos TM on tv.IDModelo = tm.IDModeloleft join TblMarcas t on t.IDMarca = tm.IDMarca where IDSubGrupoVeiculos = 4group by t.NomeMarca Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/
I don't think so... It gives me makes that don't have the Car Types I need to show.Maybe something like this:?select distinct m.IDMarca, m.NomeMarcafrom TblMarcas m inner join TblModelos mo on m.IDMarca = mo.IDMarca inner join TblVersoes v on mo.IDModelo = v.IDModelowhere v.IDSubGrupoVeiculo = '20' OR v.IDSubGrupoVeiculo = '22' OR v.IDSubGrupoVeiculo = '23' OR v.IDSubGrupoVeiculo = '28'order by m.NomeMarcaWhat I need is this phrase translated to SQL:"I want all the makes (id and name) that have versions with an IDSubGrupoVeiculo = 20 or IDSubGrupoVeiculo = 22 or IDSubGrupoVeiculo = 23 or IDSubGrupoVeiculo = 28."Is my query translating the phrase right?Edited:Sorry Dinakar!Actually, I think you're right... I forgot to change the IDSubGrupoVeiculos field to a pretended value....Resulting query is:select t.IDMarca, t.NomeMarca from TblVersoes tvleft join TblModelos TM on tv.IDModelo = tm.IDModeloleft join TblMarcas t on t.IDMarca = tm.IDMarca where tv.IDSubGrupoVeiculo = 20 or tv.IDSubGrupoVeiculo = 23 or tv.IDSubGrupoVeiculo = 23 or tv.IDSubGrupoVeiculo = 28group by t.IDMarca, t.NomeMarcaNow... is there any difference between yours and mine?Thankx---Mário Ramos |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-05-10 : 15:06:30
|
yes looks about right except you might need to put the values in quotes since the data looks alphanumeric.Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
mjpr
Starting Member
7 Posts |
Posted - 2007-05-11 : 05:33:56
|
Yep, I think so.Thank you for your help.---Mário Ramos |
 |
|
|
|
|
|
|