且构网

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

将行数据转换为Sql Server中的列

更新时间:2023-02-07 13:57:56

PIVOT TABLE EXAMPLE

PIVOT TABLE EXAMPLE
CREATE TABLE #TempPrasad
(
Val int,
Dt DATETIME
)

INSERT INTO #TempPrasad VALUES(2,'1/1/2013')
INSERT INTO #TempPrasad VALUES(1,'1/1/2013')
INSERT INTO #TempPrasad VALUES(2,'1/1/2013')
INSERT INTO #TempPrasad VALUES(1,'2/1/2013')
INSERT INTO #TempPrasad VALUES(2,'3/1/2013')
INSERT INTO #TempPrasad VALUES(4,'3/1/2014')

--SELECT * FROM #TempPrasad

SELECT *
FROM (
    SELECT
        left(datename(month,dt),3)as [month],
        Val
    FROM #TempPrasad
) as s
PIVOT
(
    SUM(Val)
    FOR [month] IN (jan, feb, mar, apr,
    may, jun, jul, aug, sep, oct, nov, dec)
)AS pivott


DROP TABLE #TempPrasad







直接贬低abo在ssms上面的脚本中,它可以为您提供解决方案。




Directly runt the above Above script in ssms,it could give you the solution.


您好,Chetan。





你可以使用Pivot选项。
Hi,Chetan.


You may use Pivot option for that.