Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Creating and updating a dynamic table based on two other tables

$
0
0

Hi all,

I currently have two tables: trait and patients that look like this:

TABLE: trait

trait
a
b
c
d
e

TABLE: patient

patient	last_updated	traits
john	1/1/2018	a,b,c,d,e
mary	1/2/2018	a,e
joe	1/10/2018	a,c
tim	1/1/2010	d

We have several million patients and a few hundred traits. A few hundred patients are added daily and a trait may be added every few weeks. I'm looking to create a dynamic table based on this data that looks similar to the following:

TABLE: patient_traits

patient	a	b	c	d	e
john	TRUE	TRUE	TRUE	TRUE	TRUE
mary	TRUE	FALSE	FALSE	FALSE	TRUE
joe	TRUE	FALSE	TRUE	FALSE	TRUE
tim	FALSE	FALSE	FALSE	TRUE	FALSE


I've looked through the create table and alter table SQL statements and can't quite figure out how to do this. What I need do is: 

1. Create the initial table and put all of the patient and trait data into it. I will want to delete and re-create this table entirely every month due to the way some things are updated in the back end, so I need a way for this to be fully T-SQL driven.

2. Every night, run an update script that alters the patient_traits table and adds new trait columns from the trait table, adds any new patients that do not exist in the patient_trait table, and update any patient in the patient_trait table whose last_update was in the last week or so. 

Anyone have any ideas how I can achieve this? Any help would be greatly appreciated. Thanks!



Viewing all articles
Browse latest Browse all 7129

Trending Articles



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