生成测试数据
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[test]( [id] [int] NULL, [name] [varchar](20) NULL, [quarter] [int] NULL, [profile] [int] NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (1, N'a', 1, 1000) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (1, N'a', 2, 2000) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (1, N'a', 3, 4000) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (1, N'a', 4, 5000) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (2, N'b', 1, 3000) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (2, N'b', 2, 3500) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (2, N'b', 3, 4200) GO INSERT [dbo].[test] ([id], [name], [quarter], [profile]) VALUES (2, N'b', 4, 5500) GO
总体结构如下:
第一步 :新建转换,在"核心转换"里的"输入"选择"表输入",连接数据库,编写sql语句
第二步:在"转换"里选择步骤"列转行"
图中箭头指向,关键字值为原表中关键字段的取值,对字段要记得聚合操作
第三步:字段选择,修改字段名称
第四步:空操作
完成后点击键盘"F9",查看结果如下
另附上数据库查询方法:
SELECT id, name, [1] AS "Q1", [2] AS "Q2", [3] AS "Q3", [4] AS "Q4" FROM TEST PIVOT ( SUM(profile) FOR quarter IN ([1], [2], [3], [4]) ) AS pvt;