如何将一个表格旋转45度并将结果保存到另一个表格中?
我有一张桌子。
---------
| a | b |
---------
| a | b |
---------
我想将其旋转 45 度(顺时针或逆时针)并将其保存到另一个表中。例如,如果我逆时针旋转45度,它将是:
-------------
| b | | |
-------------
| a | b | |
-------------
| a | | |
-------------
再比如,当我旋转时,
-------------
| a | b | c |
-------------
| d | e | f |
-------------
| g | h | i |
-------------
它会变成
---------------------
| c | | | | |
---------------------
| b | f | | | |
---------------------
| a | e | i | | |
---------------------
| d | h | | | |
---------------------
| g | | | | |
---------------------
How to do this in SQL
?
I have a table.
---------
| a | b |
---------
| a | b |
---------
I want to rotate it 45 degrees(clockwise or anti-clockwise) and save it into another table. For example, if I rotate it 45 degrees anti-clockwise, it will be:
-------------
| b | | |
-------------
| a | b | |
-------------
| a | | |
-------------
Another example, when I rotate
-------------
| a | b | c |
-------------
| d | e | f |
-------------
| g | h | i |
-------------
It will change to
---------------------
| c | | | | |
---------------------
| b | f | | | |
---------------------
| a | e | i | | |
---------------------
| d | h | | | |
---------------------
| g | | | | |
---------------------
How to do this in SQL
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个完整的工作示例(适用于 SQL Server 2005+)
下面的拼图中找到等效项,
您可以从其他 Stackoverflow 问题中找到等效项。例如,Oracle 和 DB2 都很好地支持前两者。
如果您需要任意将其应用于任何表 - 使用动态 SQL 生成上面所示的模式。
A fully working example (for SQL Server 2005+)
If you need it for another system, there are equivalents for the pieces of the puzzle below
You can find the equivalents from other Stackoverflow questions. For example, the first two are well supported by Oracle and DB2.
If you need to arbitrarily apply it to any table - use dynamic SQL to generate the pattern shown above.
桌子不应该
逆时针旋转45度的
是这样吗?以及
类似的东西:
Shouldn't the table
rotated 45 degrees anti-clockwise be like this?
and the
something like:
没有直接在 SQL 中执行此操作的简单方法。
我建议您将结果导入到不同的编程环境中,例如 Java、PHP、Python 或其他环境,在这种情况下解决问题,然后(如果需要)将结果放回到数据库中。
There is no simple way of doing this directly in SQL.
I suggest you import the result into a different programming environment, such as Java, PHP, Python or what ever, solve the problem in this context, and then (if necessary) put the result back into the DB.
SQLServer2008+ 的选项,带有 CROSS APPLY 和 PIVOT 运算
符 演示位于 SQLFiddle
Option for SQLServer2008+ with CROSS APPLY and PIVOT operators
Demo on SQLFiddle