pivot-行转列示例2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pivot-行转列示例2相关的知识,希望对你有一定的参考价值。

USE [ExampleDb]
GO
/****** Object:  Table [dbo].[PivotDemo1]    Script Date: 2017/12/16 14:12:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PivotDemo1](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [WarehouseType] [nvarchar](50) NOT NULL CONSTRAINT [DF_Table_1_FType]  DEFAULT (‘‘),
    [Warehouse] [nvarchar](50) NOT NULL CONSTRAINT [DF_Demo_B仓库]  DEFAULT (‘‘),
    [Number] [int] NOT NULL CONSTRAINT [DF_Demo_C数量]  DEFAULT ((0)),
 CONSTRAINT [PK_Demo] 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 IDENTITY_INSERT [dbo].[PivotDemo1] ON 

INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (1, NAH125, NS库, 100)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (2, NAH125, NH库, 200)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (3, NAH125, NA库, 300)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (4, NAH125, NB库, 400)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (5, NAH126, NS库, 100)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (6, NAH126, NH库, 300)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (7, NAH126, NA库, 500)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (8, NAH126, NB库, 700)
INSERT [dbo].[PivotDemo1] ([Id], [WarehouseType], [Warehouse], [Number]) VALUES (9, NAH127, NB库, 700)
SET IDENTITY_INSERT [dbo].[PivotDemo1] OFF

--------------------------------------------------------------------------------------
select  [Id] ,
        [WarehouseType] ,
        [Warehouse] ,
        [Number]
from    [ExampleDb].[dbo].[PivotDemo1];
--------------------------------------------------------------------------------------
select  *
from    ( select    Number ,
                    Warehouse ,
                    WarehouseType
          from      ExampleDb.dbo.PivotDemo1
        ) p pivot
( sum(Number) for Warehouse in ( S库, H库, A库, B库 ) ) as pvt
order by pvt.WarehouseType;
--------------------------------------------------------------------------------------

with    a as ( select   WarehouseType ,
                        S库 ,
                        H库 ,
                        A库 ,
                        B库
               from     ( select    Number ,
                                    Warehouse ,
                                    WarehouseType
                          from      ExampleDb.dbo.PivotDemo1
                        ) p pivot
( sum(Number) for Warehouse in ( S库, H库, A库, B库 ) ) as pvt
             ),
        b as ( select   WarehouseType as mo ,
                        sum(Number) as total
               from     ExampleDb.dbo.PivotDemo1
               group by WarehouseType
             )
    select  a.* ,
            b.total
    from    a
            join b on a.WarehouseType = b.mo;
--------------------------------------------------------------------------------------

 

以上是关于pivot-行转列示例2的主要内容,如果未能解决你的问题,请参考以下文章

PIVOT:行转列函数

Sqlserver行转列pivot()

SQLServer pivot 行转列

SQL 行转列 列转行 PIVOT UNPIVOT

MySQL---行转列

sql的行转列(PIVOT)与列转行(UNPIVOT)