Best way to flatten/denormalize SQL lookup tables?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 13:02:59
Bill Karwin

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!