SQL Server How to append multiple delta tables in a loop next to each other?

b4wnujal  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(118)

Let's say I have few tables of the following format:
| Status | Date_2, Date_1 |
| ------------ | ------------ |
| Alive | 15 |
| Dead | -4 |
| Unknown | 25 |

StatusDate_3, Date_2
Alive56
Dead44
Unknown-244
StatusDate_4, Date_3
------------------------
Alive18
Dead2
Unknown5

And so on.

The final result should look like:
| Status | Date_2, Date_1 | Date_3, Date_2 | Date_4, Date_3 |
| ------------ | ------------ | ------------ | ------------ |
| Alive | 15 | 56 | 18 |
| Dead | -4 | 44 | 2 |
| Unknown | 25 | -244 | 5 |

How would I set up a Temp Table so that I could append the above data to it a loop so that they appear next to each other? The issue is that I already have @DynamicSQL which produces the above tables. All I want now is for them to be concatenated next to each other so that I don't have to copy them one by one into Excel to produce the necessary graphs.

vdgimpew

vdgimpew1#

Here is the possible solution. Instead of trying to join the table next to each other, just add them on top of each other. But, to distinguish to which time period of each subsequent addition belongs to, add the new column in the table which shows the time period. Then just use the pivot table in Excel to get the data in the desired format. Will post code late.r

ttcibm8c

ttcibm8c2#

Try this one,

select coalesce(a.Status, b.Status,c.Status) as Status,
 a.Date1 as col1,
 b.Date2 as col2,
 c.Date3 as col3 
 from Table_1 as a,Table_2 as b,Table_3 as c
where a.Status = b.Status and a.Status = c.status

相关问题