TSQL 表转换字段 =>专栏
我有以下表格布局。每行值始终是唯一的。相同 Id、Name 和 Line 的实例永远不会超过一个。
Id Name Line
1 A Z
2 B Y
3 C X
3 C W
4 D W
我想查询数据,使 Line 字段成为一列。如果值存在,则在字段数据中应用 1,否则应用 0。例如,
Id Name Z Y X W
1 A 1 0 0 0
2 B 0 1 0 0
3 C 0 0 1 1
4 D 0 0 0 1
字段名称 W、X、Y、Z 只是字段值的示例,因此我无法应用运算符来显式检查,例如、“X”、“Y”或“Z”。这些可能随时改变,并且不限于一组有限的值。结果集中的列名称应将唯一字段值反映为列。
知道我怎样才能做到这一点吗?
I have the following table layout. Each line value will always be unique. There will never be more than one instance of the same Id, Name, and Line.
Id Name Line
1 A Z
2 B Y
3 C X
3 C W
4 D W
I would like to query the data so that the Line field becomes a column. If the value exists, a 1 is applied in the field data, otherwise a 0. e.g.
Id Name Z Y X W
1 A 1 0 0 0
2 B 0 1 0 0
3 C 0 0 1 1
4 D 0 0 0 1
The field names W, X, Y, Z are just examples of field values, so I can't apply an operator to explicitly check, for example, 'X', 'Y', or 'Z'. These could change at any time and are not restricted to a finate set of values. The column names in the result-set should reflect the unique field values as columns.
Any idea how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个标准的枢轴查询。
如果 1 表示布尔指示符 - 使用:
如果 1 表示该组具有该值的记录数,则使用:
It's a standard pivot query.
If 1 represents a boolean indicator - use:
If 1 represents the number of records with that value for the group, use:
已编辑以下相关更新
SQL Server 不支持动态旋转。
为此,您可以使用动态 SQL 来生成如下查询。
或者我已经读过但没有实际尝试过的另一种方法是利用 Access
Transform
功能,通过设置一个 Access 数据库,并使用指向 SQL Server 表的链接表,然后从 SQL Server 查询 Access 数据库!Edited following update in question
SQL Server does not support dynamic pivoting.
To do this you could either use dynamic SQL to generate a query along the following lines.
Or an alternative which I have read about but not actually tried is to leverage the Access
Transform
function by setting up an Access database with a linked table pointing at the SQL Server table then query the Access database from SQL Server!这是动态版本
测试表
现在运行这个
现在向表中添加一行并再次运行上面的查询,您将看到 B
警告:当然,动态 SQL 的所有问题都适用,如果您可以使用 sp_executeSQL 但由于参数不像查询中那样使用,所以确实没有意义
Here is the dynamic version
Test table
Now run this
Now add one row to the table and run the query above again and you will see the B
Caution: Of course all the problems with dynamic SQL apply, if you can use sp_executeSQL but since parameters are not use like that in the query there really is no point
假设您可以枚举有限数量的 Line 值:
Assuming you have a finite number of values for Line that you could enumerate:
当您使用 SQL Server 时,您可以使用 PIVOT 运算符旨在用于此目的。
As you are using SQL Server, you could possibly use the PIVOT operator intended for this purpose.
如果您正在为 SQL Server Reporting Services (SSRS) 报表执行此操作,或者可能转而使用该报表,那么现在就停止并在报表中添加一个 Matrix 控件。噗!你完成了!对数据的转变感到高兴。
If you're doing this for a SQL Server Reporting Services (SSRS) report, or could possibly switch to using one, then stop now and go throw a Matrix control onto your report. Poof! You're done! Happy as a clam with your data pivoted.
这是一种相当奇特的方法(使用旧 Northwind 数据库中的示例数据)。它改编自版本 此处,由于 DBCC RENAMECOLUMN 的弃用以及 PIVOT 作为关键字的添加而不再有效。
Here's a rather exotic approach (using sample data from the old Northwind database). It's adapted from the version here, which no longer worked due to the deprecation of DBCC RENAMECOLUMN and the addition of PIVOT as a keyword.