Java教程

SQL行转列

本文主要是介绍SQL行转列,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、创建样例用数据表

CREATE TABLE [dbo].[OCFOrderDetail](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CustomerCode] [varchar](10) NULL,
[ConsingerCode] [varchar](10) NULL,
[OrderMonth] [datetime] NULL,
[OrderQTY] [int] NULL,
[PO_NO] [varchar](10) NULL,
CONSTRAINT [PK_OCFOrderDetail] 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
SET IDENTITY_INSERT [dbo].[OCFOrderDetail] ON

INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (1, N'0001-00', N'0001-00', CAST(N'2021-01-01 00:00:00.000' AS DateTime), 100, N'20210101')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (2, N'0001-00', N'0001-00', CAST(N'2021-02-01 00:00:00.000' AS DateTime), 100, N'20210201')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (3, N'0001-00', N'0001-00', CAST(N'2021-03-01 00:00:00.000' AS DateTime), 100, N'20210301')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (4, N'0001-00', N'0001-00', CAST(N'2021-04-01 00:00:00.000' AS DateTime), 100, N'20210401')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (5, N'0002-00', N'0002-00', CAST(N'2021-01-01 00:00:00.000' AS DateTime), 100, N'20210101')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (6, N'0002-00', N'0002-00', CAST(N'2021-02-01 00:00:00.000' AS DateTime), 100, N'20210201')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (7, N'0002-00', N'0002-00', CAST(N'2021-03-01 00:00:00.000' AS DateTime), 100, N'20210301')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (8, N'0003-00', N'0003-00', CAST(N'2021-01-01 00:00:00.000' AS DateTime), 100, N'20210101')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (9, N'0003-00', N'0003-00', CAST(N'2021-02-01 00:00:00.000' AS DateTime), 100, N'20210201')
INSERT [dbo].[OCFOrderDetail] ([Id], [CustomerCode], [ConsingerCode], [OrderMonth], [OrderQTY], [PO_NO]) VALUES (10, N'0003-00', N'0003-00', CAST(N'2021-04-01 00:00:00.000' AS DateTime), 100, N'20210401')
SET IDENTITY_INSERT [dbo].[OCFOrderDetail] OFF

2、实例的进化演示

--1)PIVOT固定行转列
SELECT [CustomerCode],[ConsingerCode],[20210101],[20210201],[20210301],[20210401]
FROM [DBStudy].[dbo].[OCFOrderDetail]
PIVOT(sum(OrderQTY) For [PO_NO] in ([20210101],[20210201],[20210301],[20210401])) t

--2)灵活获取订单月部分
Select ',['+m+']' From (
Select Distinct Substring(Convert(varchar(10),OrderMonth,112),1,6) AS m
From [DBStudy].[dbo].[OCFOrderDetail]) t
For xml path('')

--3)删除开头的,并转换为字符串
Select Stuff((
Select ',['+m1+']'
From (
Select Distinct Substring(Convert(varchar(10),OrderMonth,112),1,8) AS m1
From [DBStudy].[dbo].[OCFOrderDetail]
) t For xml path('')),1,1,'')

--4)PIVOT灵活行转列
Declare @monthStr varchar(Max)
Declare @sqlStr varchar(Max)
Set @monthStr=Stuff((
Select ',['+m1+']'
From (
Select Distinct Substring(Convert(varchar(10),OrderMonth,112),1,8) AS m1
From [DBStudy].[dbo].[OCFOrderDetail]
) t For xml path('')),1,1,'')
set @sqlStr='Select [CustomerCode],[ConsingerCode],'+@monthStr+' FROM [DBStudy].[dbo].[OCFOrderDetail] PIVOT(sum(OrderQTY) For [PO_NO] in ('+@monthStr+')) t'
Exec(@sqlStr)

这篇关于SQL行转列的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!