且构网

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

扁平化/非规范化 SQL 查找表的***方法?

更新时间:2021-12-31 22:29:53

你需要的是一个交叉表查询.

What you need is called a crosstab query.

如果您使用的是 Microsoft SQL Server,则可以使用 PIVOT 操作符来做.

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交叉表.

Also see SO questions tagged pivot or crosstab.