Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Create dynamic pivot columns using two tables in sql server

$
0
0

Hi,

i have two tables that is table1 and table2

table1 tables,

select * from table1

idt mmid  col1  col2  col3 col4 col5
3   1001  null   bb   cc   as   q
3   1002  a      b    c    vf   t
3   1003  b      c    d    kd   s
4   1011  a      null x    mb   v
4   1125  b      c    z    gt   null
5   2542  p      q    r    tb   xs
5   5423  r      s    p    null o

i want idt column record is created as separate table.
which means,
select * from table1 where idt=3 it result is moved to idt_3 table

select * from table1 where idt=4, it result is moved to idt_4 table

idt_3,idt_4,...idt_n tables are created at dynamically. it table contains the only idt,mmid,col1,col2,col3,col4 and col5 these three fields
only.

above mentioned, dynamically created and move the record as separated table. this is working fine using below query.

declare @Sql Varchar(Max)
declare @x int
declare @f1 Varchar(10)
declare @table Table (Id Int Identity(1,1),idt int)
Insert Into @table Select Distinct idt from table1
Select @x = Max(Id) from @table
While @x>0
Begin
Select @f1 = idt from @table Where Id = @x

Set @Sql =
  'If Exists( Select 1 from sys.objects where name = ''table1_'+@f1+''' And Type_Desc = ''USER_TABLE'')
  Begin
   Insert Into table1_'+@f1+ ' Select * from table1 Where idt='''+@f1+'''
  End
  Else
   Select * Into table1_'+@f1+' from table1 Where idt='''+@f1+'''
  '
Set @x = @x-1
Exec (@Sql)
End

this query result come like this,

idt_3 table result, select * from idt_3

idt mmid  col1  col2  col3 col4 col5
3   1001  null   bb   cc   as   q
3   1002  a      b    c    vf   t
3   1003  b      c    d    kd   s


but no need idt field in result because idt field record as table name.

i have no need the col1,col2,col3,col4 and col5.instead of these columns are replaced as data,aaa,bbb,ccc and ddd columns.

data,aaa,bbb,ccc and ddd - these values are available in table2 condname column records.

table2 is there,

select * from table2

idt  cond1 condname
3    col1    data
3    col2    aaa
3    col3    bbb
3    col4    ccc
4    col1    data
4    col2    aaa
5    col1    data
5    col2    aaa
5    col3    bbb
5    col4    ccc
5    col5    ddd

i want create dynamic table idt_3,4..n above mentioned this one.

if create table,
idt_3 it contains mmid,data,aaa,bbb,ccc these are the columns are needed
idt_4, it contains mmid,data,aaa these are the columns are needed.

mmid from table1, data,aaa,bbb,ccc from table2.

data, aaa,bbb,ccc these are records in table2 but replaced as col1,col2,col3

my output is data, aaa,bbb,ccc these are display as columns

my final result come this,

for ex : table - idt_3 result

select * from idt_3

idt mmid   data  aaa   bbb  ccc
3   1001  null   bb   cc   as 
3   1002  a      b    c    vf 
3   1003  b      c    d    kd 

same for all the tables result are like this, idt_4,idt_5..idt_n tables..

Regards,
Abdul Khadir.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>