如何将列转换为行 [英] how to Convert column to row

查看:92
本文介绍了如何将列转换为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的桌子结构是这样的:



web 1000

classic 2000

platinum 3000



我希望它像



web classic platinum

1000 2000 3000





可以任意建议

My table structure is like this:

web 1000
classic 2000
platinum 3000

& i want it like

web classic platinum
1000 2000 3000


can anyon suggest on this

推荐答案

你需要透视你的数据:

http://archive.msdn.microsoft.com /SQLExamples/Wiki/View.aspx?title=PIVOTData [ ^ ]

http://msdn.microsoft.com/en-us/library/ms177410%28v=sql.90%29.aspx [ ^ ]



让我们说吧r表包含2列:商品和价格(无标识kolumn),比您的查询看起来像:

You need pivot your data:
http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=PIVOTData[^]
http://msdn.microsoft.com/en-us/library/ms177410%28v=sql.90%29.aspx[^]

Let say, your table contains 2 columns: Goods and Price (no identity kolumn), than your query should looks like:
SELECT RowNo, [web], [classic], [platinum]
FROM(
    SELECT ROW_NUMER() OVER (ORDER BY Price) RowNo, Goods, Price
    FROM YourTable
    ) AS DT
PIVOT(SUM(Price) FOR [RowNo] IN ([web], [classic], [platinum])) AS PT





如何为商品创建动态列( [web],[classic],[platinum] )?

https://www.simple-talk.com/b logs / 2007/09/14 / pivots-with-dynamic-columns-in-sql-server-2005 / [ ^ ]


这篇关于如何将列转换为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆