Author |
Topic |
cosmarchy
Starting Member
14 Posts |
Posted - 2014-03-11 : 09:41:25
|
Hi, I have a question regarding whether it was possible to have a single query for the following. I have two tables in the following format:TableA==================[cl_1] [cl_2] [ob_1] [ob_2] [q]353 354 82154 Part1 1353 354 82154 Part2 2353 354 82154 Part3 3353 354 82154 Part4 4353 354 82160 Part1 1353 354 82160 Part2 2353 354 82160 Part3 3353 354 82160 Part4 4 TableB==================[ob_1] [desc]Part1 I am a widgetPart2 So am IPart3 Hey, me tooPart4 And meCurrently I have managed to get all of the parts in TableA by using the following:SELECT TableA.[ob_2], TableA.[q]FROM TableA INNER JOIN TableB ON TableA.[ob_1] = TableB.[ob_1]WHERE (TableA.[cl_1] = 353) AND (TableB.[cl_2] = 354) AND (TableB.[ob_1] = 82154) which gives me [ob_2] [q]Part1 1Part2 2Part3 3Part4 4what I would like to know is whether it was possible to alter this query so that I can read [ob_2] from TableB into its [desc] so that it gives me this:[desc] [q]I am a widget 1So am I 2Hey, me too 3And me 4Hopefully this makes sense to someone?Thanks |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-11 : 09:57:20
|
[code] SELECT [desc] [q] FROM TableA as A INNER JOIN TableB as B on A.[ob_2]=B.[ob_1]WHERE ([cl_1] = 353) AND ([cl_2] = 354) AND ([ob_1] = '82154')[/code] sabinWeb MCP |
|
|
sqlsaga
Yak Posting Veteran
93 Posts |
Posted - 2014-03-11 : 11:05:18
|
[code]DECLARE @TableA TABLE([cl_1] INT, [cl_2] INT,[ob_1] INT,[ob_2] VARCHAR(24),[q] INT)INSERT INTO @TableA VALUES(353, 354, 82154, 'Part1', 1), (353, 354, 82154, 'Part2', 2), (353, 354, 82154, 'Part3', 3), (353, 354, 82154, 'Part4', 4),(353, 354, 82160, 'Part1', 1), (353, 354, 82160, 'Part2', 2), (353, 354, 82160, 'Part3', 3), (353, 354, 82160, 'Part4', 4)DECLARE @TableB TABLE([ob_1] VARCHAR(16),[desc] VARCHAR(24))INSERT INTO @TableB VALUES('Part1', 'I am a widget'), ('Part2', 'So am I'), ('Part3', 'Hey, me too'), ('Part4', 'And me')SELECT b.[desc], a.qFROM @TableA aINNER JOIN @TableB b ON a.ob_2 = b.ob_1WHERE a.cl_1 = 353 AND a.cl_2 = 354 AND a.ob_1 = 82154[/code]Visit www.sqlsaga.com for more t-sql snippets and BI related how to's. |
|
|
cosmarchy
Starting Member
14 Posts |
Posted - 2014-03-11 : 11:50:38
|
[code] TableA OB_1 CL_1 OB_2 CL_2 Q82154 353 2514 354 1082154 353 2820 354 2082154 353 4404 354 3082154 353 5190 354 40[/code][code] TableBOB_1 CL_1 Desc 2514 354 I am a widget2820 354 So am I4404 354 Hey, me too5190 354 And me82154 353 Widget container[/code]Thanks guys for your input. However when I tried your suggestions I came up with problems and whilst trying to describe the problem to you I was getting more and more confused myself.I've therefore tried to simplify this question by re-wording hopefully in a simpler way and for me to get my head around!!Looking at this again we have:A widget container which contains four different widgets. The widget container is, an assembly if you like, full of parts. The widget container (assembly) and the flavours of widgets (parts) are in tableB. The quantities (Q) of each type of widget is in tableA.So, this query will get all of the widgets and quantities contained in the widget container.[code]SELECT TableA.OB_2, TableA.QFROM TableA INNER JOIN TableB ON TableA.OB_1 = TableB.OB_1WHERE (TableA.CL_1 = 353) AND (TableA.CL_2 = 354) AND (TableB.OB_1 = 82154)[/code]I therefore end up with [code]OB_2 Q2514 102820 20 4404 305190 40[/code]This isn't much good to me as I really would like to know what each widget is called. So really I want this:[code]Desc QI am a widget 10So am I 20Hey, me too 30And me 40[/code]The problem here is that the Desc is contained in tableB. If I use the previous suggestions, I end up with results looking like this:[code]Desc QWidget container 10Widget container 20Widget container 30Widget container 40[/code] |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2014-03-11 : 12:14:23
|
The table design is poor.-- *** Test Data in Consumable Format ***-- You should provide this!CREATE TABLE #TableA( OB_1 int NOT NULL ,CL_1 int NOT NULL ,OB_2 int NOT NULL ,CL_2 int NOT NULL ,Q int NOT NULL);INSERT INTO #TableAVALUES (82154, 353, 2514, 354, 10) ,(82154, 353, 2820, 354, 20) ,(82154, 353, 4404, 354, 30) ,(82154, 353, 5190, 354, 40);CREATE TABLE #TableB( OB_1 int NOT NULL ,CL_1 int NOT NULL ,[Desc] varchar(20) NOT NULL);INSERT INTO #TableBVALUES (2514, 354, 'I am a widget') ,(2820, 354, 'So am I') ,(4404, 354, 'Hey, me too') ,(5190, 354, 'And me') ,(82154, 353, 'Widget container');-- *** End Test Data in Consumable Format ***SELECT B.[Desc], A.QFROM #TableA A JOIN #TableB B ON A.OB_2 = B.OB_1 AND A.CL_2 = B.CL_1ORDER BY A.OB_2, A.CL_2; |
|
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2014-03-11 : 12:18:59
|
I think there is a problem with your code as if you say TableB.OB_1 = 82154 as above that you will only get one DESC as writen. I believe your join should be on different columns.djj |
|
|
cosmarchy
Starting Member
14 Posts |
Posted - 2014-03-11 : 12:29:30
|
quote: Originally posted by Ifor The table design is poor.
I couldn't agree more, however I inherited this mess from a 'professional' PDM company and has been butchered by other supposed professionals in the mean time!!All I'm trying to do is piece it back together |
|
|
sqlsaga
Yak Posting Veteran
93 Posts |
Posted - 2014-03-11 : 12:55:42
|
You are joining OB_1 with OB_1 but you are supposed to do it with OB_2 and OB_1... that's where the problem in your query lies..try the code I gave you...Visit www.sqlsaga.com for more t-sql snippets and BI related how to's. |
|
|
cosmarchy
Starting Member
14 Posts |
Posted - 2014-03-12 : 05:11:25
|
quote: Originally posted by sqlsaga You are joining OB_1 with OB_1 but you are supposed to do it with OB_2 and OB_1... that's where the problem in your query lies..try the code I gave you...Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Ah yes, missed that. I clearly had a typo moment Thanks very much for this |
|
|
Muj9
Yak Posting Veteran
75 Posts |
Posted - 2014-03-12 : 11:30:54
|
drop table TableAcreate table TableA ([cl_1] int,[cl_2] int,[ob_1] int,[ob_2] varchar(10),[q] smallint )insert into TableA([cl_1],[cl_2],[ob_1],[ob_2],[q])values(353,354,82154,'Part1',1),(353,354,82154,'Part2',2),(353,354,82154,'Part3',3),(353,354,82154,'Part4',4),(353,354,82160,'Part1',1),(353,354,82160,'Part2',2),(353,354,82160,'Part3',3),(353,354,82160,'Part4',4)drop table TableBcreate table TableB ([ob_1] varchar(10),[desc] varchar(8000) )insert into TableB([ob_1],[desc])values('Part1','I am a widget'),('Part2','So am I'),('Part3','Hey, me too'),('Part4','And me')select * from TableAselect * from TableBselect b.[desc],a.q from TableA aleft join TableB b on a.ob_2 = b.ob_1 |
|
|
cosmarchy
Starting Member
14 Posts |
Posted - 2014-03-12 : 12:25:37
|
[code]TableBOB_1 CL_1 Desc 2514 354 I am a widget2820 354 So am I4404 354 Hey, me too5190 354 And me82154 353 Widget container[/code][code]SELECT TableB.Desc, TableA.qFROM TableA INNER JOIN TableB ON TableA.ob_2 = TableB.ob_1WHERE (TableA.cl_1 = 353) AND (TableA.ob_1 = 82154)[/code]Thinking about this and taking this a little further, would it be possible to change the where clause so that instead of (TableA.ob_1 = 82154) you use TableB.Desc?In essence replacing ob_1 which is 82154 in both TableA & Table B with 'Widget Container' (the desc for OB_1 in table B) in the where clause?It would be useful to have a meaningful name |
|
|
|
|
|