下面為表創(chuàng)建代碼:
CREATE TABLE [dbo].[ProductAuditRecord]([ParID] [nchar](12)
NOT NULL,[MOID] [nchar](12)
NOT NULL,[LotSN] [nvarchar](50)
NOT NULL,[CosmeticInspection] [nchar](12)
NOT NULL,[FunctionalTest] [nchar](12)
NOT NULL,[UNumber] [nchar](50)
NULL,[LeadWire] [nchar](50)
NULL,[ResourceID] [nchar](12)
NOT NULL,[UserID] [nchar](12)
NOT NULL,[Remark] [nvarchar](100)
NULL,[CreatDate] [datetime] NOT NULL,[ProductID] [nchar](12)
NOT NULL,[nextID] [int] NOT NULL, CONSTRAINT [PK_ProductAuditRecord] PRIMARY KEY CLUSTERED
([ParID] ASC)WITH
(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY])
ON [PRIMARY]GOALTER TABLE [dbo].[ProductAuditRecord] ADD
CONSTRAINT [DF_ProductAuditRecord_ParID] DEFAULT
(substring(CONVERT([char](36),
newid(),(0)),(1),(12)))
FOR [ParID]GOALTER TABLE [dbo].
[ProductAuditRecord] ADD CONSTRAINT
[DF_ProductAuditRecord_nextID] DEFAULT
((0))
FOR [nextID]GO
下面為自己測試數(shù)據(jù)得到的結(jié)果:上面為原始數(shù)據(jù),下面為轉(zhuǎn)換后的數(shù)據(jù):轉(zhuǎn)換代碼
測試表代碼:CREATE TABLE [dbo].[Test](
[月份] [varchar](4) NULL,
[工資] [int] NULL,
[福利] [int] NULL,
[獎(jiǎng)金] [int] NULL
) ON [PRIMARY]
1:月份 工資 福利 獎(jiǎng)金
1月 100 200 300
2月 110 210 310
3月 120 220 320
4月 130 230 330
2:考核月份 1月 2月 3月 4月
福利 200 210 220 230
工資 100 110 120 130
獎(jiǎng)金 300 310 320 330
SELECT * FROM
(
SELECT 考核月份,月份,金額 FROM
(SELECT 月份, 工資, 福利, 獎(jiǎng)金 FROM Test) p
UNPIVOT
(金額 FOR 考核月份 IN (工資, 福利, 獎(jiǎng)金))AS unpvt
) T
PIVOT
(MAX(金額) FOR 月份 in ([1月],[2月],[3月],[4月]))AS pt
更多信息請查看IT技術(shù)專欄