且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

展平/反规范化SQL查找表的***方法?

更新时间:2021-10-14 23:46:43

您需要的称为交叉表查询.

What you need is called a crosstab query.

如果您使用的是Microsoft SQL Server,则可以使用 运算符即可完成.

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

其他品牌的RDBMS对这种类型的查询有不同的支持.最糟糕的情况是,您将不得不使用动态SQL将非常有价值的值从查找表硬编码到主表的联接中.当您有122个不同的值时,这是不切实际的.

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.

还可以看到标记为 pivot 查看全文