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 |
ryanoc
Starting Member
25 Posts |
Posted - 2007-04-03 : 09:51:34
|
config table--------------------id name type--------------------1 make varchar2 model varchar3 color varcharveh table--------------------------id make model color--------------------------1 chevy s10 white2 ford ranger silver2 chevy blazer brownrecordset needed for veh.id=1---------------------------id name type value---------------------------1 make varchar chevy2 model varchar s103 color varchar white |
|
ryanoc
Starting Member
25 Posts |
Posted - 2007-04-03 : 09:52:16
|
I have 2 tables, my vehicle data table and my config table. I need a query to join them by a datarow and a data column. Heres my tables... |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 10:14:16
|
[code]SELECT c.ID, c.Name, c.Type, x.Info AS ValueFROM Config AS cINNER JOIN ( SELECT 1 AS C, Make AS Info FROM Veh WHERE ID = 1 UNION ALL SELECT 2, Model FROM Veh WHERE ID = 1 UNION ALL SELECT 3, Color FROM Veh WHERE ID = 1 ) AS x ON x.C = c.ID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
ryanoc
Starting Member
25 Posts |
Posted - 2007-04-03 : 10:28:19
|
I'm a little concerned about the performance since the real query will have almost 80 fields to do this on instead of the example table of only 3, but it works great. How concerned should I be do you think?Thanks!Ryan |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 10:30:24
|
I would be more concerned that table is not normalized.This is the price you pay.Try and see what response time you get!Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-03 : 11:03:46
|
[code]declare @config table( [id] int, [name] varchar(10), type varchar(10))insert into @configselect 1, 'make', 'varchar' union allselect 2, 'model', 'varchar' union allselect 3, 'color', 'varchar'declare @veh table( [id] int, make varchar(10), model varchar(10), color varchar(10))insert into @vehselect 1, 'chevy', 's10', 'white' union allselect 2, 'ford', 'ranger', 'silver' union allselect 2, 'chevy', 'blazer', 'brown'select c.id, c.name, c.type, [value] = case when c.[name] = 'make' then v.make when c.[name] = 'model' then v.model when c.[name] = 'color' then v.color endfrom @veh v cross join @config cwhere v.id = 1/*id name type value ----------- ---------- ---------- ---------- 1 make varchar chevy2 model varchar s103 color varchar white*/[/code] KH |
 |
|
ryanoc
Starting Member
25 Posts |
Posted - 2007-04-03 : 11:10:34
|
ok, i got the first solution to work, but I also have a table with parameters on how the data is validated and I cant join it now. Heres my query. The part with the MASKEDEDIT table wont return values when I include the inner join.SELECT mc.config_id ,mc.config_name ,mc.config_req ,mc.config_length ,mc.ctrl_id ,mc.map_field ,mc.map_dtype ,mc.default_val ,mc.visible_add ,me.min_val ,me.max_val ,me.is_money ,me.mask ,x.Info AS def_valFROM crm_map_config AS mcLEFT JOIN MASKEDEDIT me on me.config_id = mc.config_idINNER JOIN ( SELECT 1 AS C, untid as info FROM VHVIN WHERE untid = '0A0018' UNION ALL SELECT 2, vin FROM VHVIN WHERE untid = '0A0018' UNION ALL SELECT 4, CAST ( AmtOurPrice AS varchar(50) ) --AmtOurPrice FROM VHVIN WHERE untid = '0A0018' ) AS x ON x.C = mc.config_id |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 11:11:56
|
Nice  SELECT c.ID, c.Name, c.Type, CASE WHEN c.Name = 'make' THEN v.Make WHEN c.Name = 'model' THEN v.Model WHEN c.Name = 'color' THEN v.Color END AS [Value]FROM @Config AS cINNER JOIN @Veh AS v ON v.ID = 1 Peter LarssonHelsingborg, Sweden |
 |
|
ryanoc
Starting Member
25 Posts |
Posted - 2007-04-03 : 11:43:33
|
Thanks guys, I got the reply from KH to work with all the joins. so far so good!Ryan |
 |
|
|
|
|
|
|