本文介绍了怎么做Pivot为此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
CREATE TABLE [dbo].[pivot](
[id] [int] NOT NULL,
[cntnr_no] [varchar](11) NULL,
[fld_vl] [varchar](11) NULL,
[fld_id] [varchar](11) NULL,
CONSTRAINT [PK_pivot] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
insert into [pivot] values(1,'CRSU1234620','ACID',38)
insert into [pivot] values(2,'CRSU1234620','FOOD',39)
insert into [pivot] values(3,'CRSU1234620','FRUITS',40)
select * From [pivot]
id cntnr_no fld_vl fld_id
1 CRSU1234620 ACID 38
2 CRSU1234620 FOOD 39
3 CRSU1234620水果40
但我需要像这样的O / P
cntnr_no | fld_vl1 | fld_vl2 | fld_val3 | fld_id1 | fld_id2 | fld_id3
CRSU1234620 |酸|食品|水果| 38 | 39 | 40
如何做到这一点我不知道枢轴,但我也是这样想的
select * From [pivot]
idcntnr_nofld_vlfld_id
1CRSU1234620ACID38
2CRSU1234620FOOD39
3CRSU1234620FRUITS40
But i need The O/P like this
cntnr_no |fld_vl1 |fld_vl2 | fld_val3 | fld_id1 |fld_id2 |fld_id3
CRSU1234620 | ACID |FOOD | FRUITS | 38 |39 | 40
How to do this i dnot know about pivot,but i am also trying like this
SELECT cntnr_no, fld_vl as fld_vl1, fld_vl as fld_vl2, fld_vl as fld_vl3
FROM
(SELECT cntnr_no, fld_vl, fld_id
FROM [pivot] ) ps
PIVOT
(
SUM (id)
FOR cntnr_no IN
( fld_vl, fld_id)
) AS pvt
推荐答案
这篇关于怎么做Pivot为此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!