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 |
|
SCHEMA
Posting Yak Master
192 Posts |
Posted - 2009-05-01 : 12:29:59
|
| I have table AField A FieldB Field Ca 2 3b 3 4Other table has value for aTableBA(col) 1234I need to get this final output:Field A FieldB FieldC1 2 32 2 33 2 34 2 3......... so on for b |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-05-01 : 12:41:27
|
| try thisdeclare @table1 table (FieldA char(1),FieldB int, FieldC int )insert into @table1select 'a', 2, 3 union allselect 'b', 3, 4 --select * from @table1declare @table2 table (a int )insert into @table2select 1 union allselect 2 union allselect 3 union allselect 4select a as fielda,fieldb,fieldc from @table1,@table2 order by fielda |
 |
|
|
SCHEMA
Posting Yak Master
192 Posts |
Posted - 2009-05-01 : 12:45:40
|
quote: Originally posted by raky try thisdeclare @table1 table (FieldA char(1),FieldB int, FieldC int )insert into @table1select 'a', 2, 3 union allselect 'b', 3, 4 --select * from @table1declare @table2 table (a int )insert into @table2select 1 union allselect 2 union allselect 3 union allselect 4select a as fielda,fieldb,fieldc from @table1,@table2 order by fielda
No this doesn't work.I know we have to use PIVOT or crosstab but don't know how to. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-01 : 13:04:41
|
quote: Originally posted by SCHEMANo this doesn't work.I know we have to use PIVOT or crosstab but don't know how to.
Why? Are you asking for a solution using PIVOT or someother method than Raky's?If you change Raky's query you'll get the proper order:select a as fielda,fieldb,fieldc from @table1,@table2 order by fieldb, fielda |
 |
|
|
SCHEMA
Posting Yak Master
192 Posts |
Posted - 2009-05-01 : 13:11:25
|
| I have other values for 'b'5678So for boutput:5 3 46 3 47 3 48 3 4So it needs to loop according to FieldA in TableAa,b,c..... |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-01 : 14:57:54
|
| can you prove sample data and expected output so we can have some data to work with? I'm not clear on where 'b' is located. |
 |
|
|
SCHEMA
Posting Yak Master
192 Posts |
Posted - 2009-05-01 : 15:03:37
|
| TABLE Acol1 col2 col3a 2 3b 3 4TABLE BA B1 22 3OUTPUT:col1 col2 col31 2 32 2 32 3 43 3 4 |
 |
|
|
|
|
|
|
|