Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to flatten/denormalize SQL lookup tables?

I have a bunch of tables like this:

Lookup_HealthCheckupRisks
------------
ID  Name
1   Anemia
2   Anorexic
3   Bulemic
4   Depression
...
122   Syphilis



PatientRisksOnCheckup
------------------
ID CheckupID RiskID
1  11        2
2  11        3
3  12        1
4  14        1
5  14        3
...

But I need a flattened version, like this:

PatientCheckup
------------------
CheckupID Risk_1 Risk_2 Risk_3 Risk_4 .. Risk_122
11        0      1      1      0         0
12        1      0      0      0         0
13        0      0      0      0         0
14        1      0      1      0         0

I'm clueless how to do this, the best I can think of is to write a temp table, define all 122 columns, and then do If Exists ( SELECT * FROM PatientRisksOnCheckup where RiskID=i and checkupID=j ) INSERT INTO PatientCheckup (1) WHERE CheckupID=j and iterate overi, j... >_<

Writing this query for just one table is doable not the best, but I've need to flatten data like this for another thirty tables of the same size. Er... suggestions please?

I am also curious to know if what I am doing is a common thing to do or not... ?

I am needing to denormalize/flatten the sql data for statistics software.

like image 508
rlb.usa Avatar asked Sep 22 '09 17:09

rlb.usa


People also ask

How do you Denormalize a table?

Denormalization is a database optimization technique where we add redundant data in the database to get rid of the complex join operations. This is done to speed up database access speed. Denormalization is done after normalization for improving the performance of the database.

What is a flattened table in SQL?

Flattened tables are typically fact tables where one or more columns query other tables for their values, using DEFAULT or SET USING constraints.

What is denormalization in SQL?

Denormalization is the process of adding precomputed redundant data to an otherwise normalized relational database to improve read performance of the database. Normalizing a database involves removing redundancy so only a single copy exists of each piece of information.


1 Answers

What you need is called a crosstab query.

If you're using Microsoft SQL Server, you can use the PIVOT operator to do it.

Other brands of RDBMS have varying support for this type of query. Worst case is you'll have to use dynamic SQL to hard-code very value from the lookup table into a join to your main table. This is not practical when you have 122 distinct values.

Also see SO questions tagged pivot or crosstab.

like image 84
Bill Karwin Avatar answered Nov 01 '22 15:11

Bill Karwin