将行转换为sql server中的列

时间:2018-08-23 09:06:14

标签: sql sql-server

我很难弄清楚这个问题,请帮我。我有时间输入/输出sql查询。有四列显示时间输入/输出信息,例如...

image1

我正在尝试将行转换为列,并在下面显示此类信息

image2

我尝试使用它,但仅选择第一列

select [tblDataYears_Year],[tblDataYears_Grade],[tblDataYears_ServicePeriod],[tblDataYears_DirectAnnual] from [tblDataYears]
declare @s varchar(8000)
select  @s = COALESCE(@s + ',', '') + cast([tblDataYears_Year] as varchar(10)) + ' as number'
from    [tblDataYears]
exec('select '+@s)

用于解决方案的SQL命令:

CREATE TABLE [dbo].[tblDataYears](
    [tblDataYears_Id] [int] IDENTITY(1,1) NOT NULL,
    [tblDataYears_Year] [nvarchar](50) NULL,
    [tblDataYears_Grade] [int] NULL,
    [tblDataYears_ServicePeriod] [int] NULL,
    [tblDataYears_DirectAnnual] [date] NULL,
    [tblDataYears_IsDeleted] [bit] NULL,
    [FKEmp_Id] [int] NULL)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (1, N'1435', 10, 16, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (2, N'1436', 11, 17, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (3, N'1437', 12, 18, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (4, N'1438', 13, 19, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (5, N'1439', 14, 20, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (6, N'1440', 15, 21, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (7, N'1441', 16, 22, CAST(N'2018-01-08' AS Date), 0, 1)
INSERT [dbo].[tblDataYears] ([tblDataYears_Id], [tblDataYears_Year], [tblDataYears_Grade], [tblDataYears_ServicePeriod], [tblDataYears_DirectAnnual], [tblDataYears_IsDeleted], [FKEmp_Id]) VALUES (8, N'1442', 17, 23, CAST(N'2018-01-08' AS Date), 0, 1)

1 个答案:

答案 0 :(得分:0)

这很复杂,因为它既需要枢轴,也需要枢轴。我会这样处理:

Factory.getSession(SessionType.NATIVE).getDatabase(filePath).getView("myView").checkUnique(key, Document)

如果您需要可变数量的列,则可以将其转换为动态SQL-唯一的更改是对checkUnique()子句的修改。