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 |
|
seronni
Starting Member
1 Post |
Posted - 2008-07-06 : 11:42:06
|
| Hi ALL!I have 3 tables and i get the values and union all with another select,but i want search foreach row in this select if has more items in the table 2.How i Do that with an performance way?Example my T-SQL look like this:TB1ITEM1_COD NAME UNIM1_COD31790 NAME1 111 NAME2 2337 NAME3 3342 NAME4 4123 NAME5 598 NAME6 622 NAME7 71233 NAME8 819 NAME9 920 NAME10 101568 NAME11 113777 NAME12 123222 NAME13 13 TB2ITEM1_COD ITEM1_COD0 QTD11 31790 1,2337 31790 1,9342 31790 0,7123 31790 1,298 31790 1222 31790 221233 31790 2,419 22 2,520 22 22,21568 22 1,9 3777 22 3,53222 22 2,5 3146 23 3,713663 19 3,515345 19 3,5334334 23 3,94415 25 3,5TB3 UNIM1_COD NAME1 a2 b3 c4 d5 e6 f7 g8 h9 i10 j11 k12 l13 mALTER PROCEDURE [dbo].[MySelect]( @COD int)AS SET NOCOUNT ON;//Get the Name in the TB1, QTD in the TB2 and Name in TB3 where the atribute Item1_COD0 in TB2 = an specific numberSELECT TB1.NAME,TB2.QTD,TB3.NAMEFROM TB1 INNER JOIN TB2 ON TB1.ITEM1_COD = TB2.ITEM1_COD INNER JOIN TB3 ON TB1.UNIM1_COD = TB3.UNIM1_CODWHERE (TB2.ITEM1_COD0 = @COD)UNION ALL//get the name in TB1 and the Name in TB3 and Union whith the select before where the atribute ITEM1_COD in TB1 = the specific numberSELECT TB1.NAME,NULL,TB3.NAMEFROM TB1 AS TB1_1 INNER JOIN TB3 AS TB3_1 ON TB1_1.UNIM1_COD = TB3_1.UNIM1_CODWHERE (TB1_1.ITEM1_COD = @COD)this code when the @COD = 31790 for example may give me this:NAME QTD NAMENAME2 1,2 bNAME3 1,9 cNAME4 0,7 dNAME5 1,2 eNAME6 12 fNAME7 22 gNAME8 2,4 hNAME1 NULL abut if u pay attention in TB2 in the field 'ITEM1_COD' the element 22 and 19 are an subelement of the 31790. Theelement 22 have more 5 element and the element 19 have more 2 elements and i need to get them too...how i do that?Can anyone, please help me!?TKS, |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-07 : 00:17:47
|
| Can you show what your expected output will be from sample data above? |
 |
|
|
|
|
|
|
|