求助sql2005关于行转列 PIVOT的另一个用法

thunderbird521 2009-04-23 11:47:59
为方便各位洞察问题,提供测试代码如下:

CREATE TABLE salesByMonth
(
year char(4),
month char(3),
amount money,
PRIMARY KEY (year, month)
)

INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Jan', 789.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Feb', 389.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Mar', 8867.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Apr', 778.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','May', 78.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Jun', 9.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Jul', 987.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Aug', 866.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Sep', 7787.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Oct', 85576.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Nov', 855.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Dec', 5878.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Jan', 7.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Feb', 6868.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Mar', 688.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Apr', 9897.0000)
--查看数据
select * from salesByMonth
--按照常规行转列
SELECT *
FROM salesByMonth
PIVOT ( SUM(amount) FOR month IN
([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
)t
显示结果
year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
---- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- ---------------------
2004 789.00 389.00 8867.00 778.00 78.00 9.00 987.00 866.00 7787.00 85576.00 855.00 5878.00
2005 7.00 6868.00 688.00 9897.00 NULL NULL NULL NULL NULL NULL NULL NULL

(2 行受影响)


而我的需求是相反的操作,我要求把year作为列显示,呈现类似如下的结果:
2004 2005
Jan X X
Feb
Mar
Apr
May


求助各位达人!
...全文
769 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
深夜情感老师 2009-05-29
  • 打赏
  • 举报
回复
别说少用,我都没有见过哦
thunderbird521 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 playwarcraft 的回复:]
LZ你。。。。。。
会by month来转,不会by year来转。。。。。。晕掉
[/Quote]
我刚发现问了一个多简单的问题,
可能昨晚激情过度,脑袋有点发晕了。。。
感谢liangCK的超音速回答!
结贴。
中国风 2009-04-23
  • 打赏
  • 举报
回复
樓主的行列不固定時最好用動態生成
參照
http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html
htl258_Tony 2009-04-23
  • 打赏
  • 举报
回复
收藏一下,还比较少用这函数.
中国风 2009-04-23
  • 打赏
  • 举报
回复
排序上面可用case when ...指定月份的排序
中国风 2009-04-23
  • 打赏
  • 举报
回复
select month,[2004]=isnull([2004],0),[2005]=isnull([2005],0)
from salesByMonth pivot (sum(amount) for year in([2004],[2005]))t
playwarcraft 2009-04-23
  • 打赏
  • 举报
回复
LZ你。。。。。。
会by month来转,不会by year来转。。。。。。晕掉
playwarcraft 2009-04-23
  • 打赏
  • 举报
回复
額。。。不一樣?
liangCK 2009-04-23
  • 打赏
  • 举报
回复
CREATE TABLE salesByMonth
(
year char(4),
month char(3),
amount money,
PRIMARY KEY (year, month)
)

INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Jan', 789.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Feb', 389.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Mar', 8867.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Apr', 778.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','May', 78.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Jun', 9.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Jul', 987.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Aug', 866.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Sep', 7787.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Oct', 85576.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Nov', 855.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2004','Dec', 5878.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Jan', 7.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Feb', 6868.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Mar', 688.0000)
INSERT INTO salesByMonth (year, month, amount)
VALUES('2005','Apr', 9897.0000)


SELECT *
FROM salesByMonth
PIVOT ( SUM(amount) FOR YEAR IN
([2004],[2005])
)t

drop table salesByMonth

/*
month 2004 2005
----- --------------------- ---------------------
Apr 778.00 9897.00
Aug 866.00 NULL
Dec 5878.00 NULL
Feb 389.00 6868.00
Jan 789.00 7.00
Jul 987.00 NULL
Jun 9.00 NULL
Mar 8867.00 688.00
May 78.00 NULL
Nov 855.00 NULL
Oct 85576.00 NULL
Sep 7787.00 NULL

(12 行受影响)

*/
liangCK 2009-04-23
  • 打赏
  • 举报
回复
直接PIVOT YEAR
liangCK 2009-04-23
  • 打赏
  • 举报
回复
错了..
liangCK 2009-04-23
  • 打赏
  • 举报
回复
PIVOT后再UNPIVOT

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧