Best way to flatten/denormalize SQL lookup tables?

后端 未结 3 2188
名媛妹妹
名媛妹妹 2021-02-14 18:44

I have a bunch of tables like this:

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



Pati         


        
相关标签:
3条回答
  • 2021-02-14 19:26

    How about using triggers on INSERT AND UPDATE, DELETE, so that these Denormalized table gets filled up...

    0 讨论(0)
  • 2021-02-14 19:28

    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.

    0 讨论(0)
  • 2021-02-14 19:31

    Use PIVOT TABLE Here - Microsoft and here - tutorial.

    You will need hovewer to specify all the columns. But you can use sp_executesql command to use dynamic SQL.

    0 讨论(0)
提交回复
热议问题