Link to home
Start Free TrialLog in
Avatar of John Porter
John PorterFlag for United States of America

asked on

I am trying to save the results of a SQL Pivot table query into a SQL Table. I am using SQL Server 2016.

Hello Experts,


 I have two Tables. Tb_1 and Tb_2 


I am trying to save the results of a SQL Pivot table query on Tb_1 into Tb_2. I am using SQL Server 2016.


Tb_1 Looks like this before running PIVOT table code:

LotNum   [Type]  [Wt]

1               T1          2.5

2               T2          3.7

3               T3          4.2


Here is the PIVOT table  Code I am running on Tb_1 above:

  

SELECT LotNum, [T1] AS T1, [T2] AS T2, [T3] AS T3

FROM

( SELECT LotNum, [Type], Wt

  FROM Tb_1

  ) ps

PIVOT

  ( SUM (Wt)

    FOR [Type] IN ([T1],[T2],[T3])

   ) AS pvt


 Results of PIVOT code on TB_1 above look like this:

LotNum   T1      T2      T3

1               2.5      NULL   NULL

2               NULL  3.7       NULL

3               NULL  NULL   4.2


Tb_2 looks like this and has no Data:

LotNum   T1      T2      T3


I Would like to save the Tb_1 PIVOT code results (shown above) into Tb_2 (shown above) so TB_2 will now look like this.


LotNum   T1      T2      T3

1               2.5      NULL   NULL

2               NULL  3.7       NULL

3               NULL  NULL   4.2


Does anyone know how to do this?


Thanks!






ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

The question I have is do really need or want Tb_2?

Tb_1 seems normalized and should be the best way to deal with that data.  Tb_2 isn't normalized.
We don't store data redundantly. Just use a view or stored procedure or function.
Avatar of John Porter

ASKER

That works - THANKS!