行转列示例3

Posted

tags:

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

USE [ExampleDb]
GO
/****** Object:  Table [dbo].[PivotDemo2]    Script Date: 2017/12/16 14:44:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PivotDemo2](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [PDate] [nvarchar](50) NOT NULL CONSTRAINT [DF_ResultInfo_Date]  DEFAULT (‘‘),
    [PResult] [nvarchar](50) NOT NULL CONSTRAINT [DF_ResultInfo_Result]  DEFAULT (‘‘),
 CONSTRAINT [PK_ResultInfo] 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].[PivotDemo2] ON 

INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (1, N2017-10-10, N)
INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (2, N2017-10-10, N)
INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (3, N2017-10-10, N)
INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (4, N2017-10-10, N)
INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (5, N2017-10-11, N)
INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (6, N2017-10-11, N)
INSERT [dbo].[PivotDemo2] ([Id], [PDate], [PResult]) VALUES (7, N2017-10-11, N)
SET IDENTITY_INSERT [dbo].[PivotDemo2] OFF
--------------------------------------------------------------------------------------------------
select  [Id] ,
        [PDate] ,
        [PResult]
from    [ExampleDb].[dbo].[PivotDemo2];
--------------------------------------------------------------------------------------------------
select  tmp.PDate ,
        sum([]) 优 ,
        sum([]) 良
from    ( select    [PDate] ,
                    case PResult
                      when  then count(1)
                      else 0
                    end  ,
                    case PResult
                      when  then count(1)
                      else 0
                    end 
          from      [ExampleDb].[dbo].[PivotDemo2]
          group by  PDate ,
                    PResult
        ) tmp
group by tmp.PDate;
--------------------------------------------------------------------------------------------------                                                              
select  cctable.PDate ,
        sum(cctable.优)  ,
        sum(cctable.良) 
from    ( select    ctable.PDate ,
                    case ctable.PResult
                      when  then ctable.rum
                      else 0
                    end  ,
                    case ctable.PResult
                      when  then ctable.rum
                      else 0
                    end 
          from      ( select    case a.PResult
                                  when  then 
                                  else 
                                end as PResult ,
                                count(a.PResult) rum ,
                                a.PDate
                      from      ExampleDb.[dbo].[PivotDemo2] a with ( nolock )
                      group by  a.PDate ,
                                a.PResult
                    ) ctable
        ) cctable
group by cctable.PDate;
--------------------------------------------------------------------------------------------------

 

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

C#中DataTable行转列示例

pivot-行转列示例1

pivot-行转列示例2

SQL sql语句实现行转列的3种方法

PIVOT:行转列函数

填报需求:行转列填报表的制作