我是第一次在SQL Server 2005中尝试使用pivot功能,然后跑到墙上获取我想要的结果集。
我有两张表defects
和employees
。我正试图在employee
表中找到员工的所有缺陷,我不关心分配给员工表中输入的人之外的任何缺陷(我想加入它,我尚未尝试过子查询,但不认为我需要)
我希望将结果按(透视)defects.severity
字段分组。我想在结果集中使用的字段是:severity, assigned to, employee.name, fix data
基本上我想根据严重程度汇总数据,并根据缺陷的严重程度显示计数
以下是我的表格:
CREATE TABLE [dbo].[Employees]
(
[EmployeeId] [int] NOT NULL,
[ManagerId] [int] NULL,
[NTID] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[FullName] [nvarchar](100) NOT NULL,
[ReportingGroup] [nchar](10) NOT NULL,
[Added] [datetime] NOT NULL,
[LastUpdate] [datetime] NOT NULL,
[UpdateBy] [nvarchar](50) NOT NULL
)
CREATE TABLE [dbo].[defects]
(
[Defect ID] [float] NULL,
[SubSystem] [nvarchar](255) NULL,
[Status] [nvarchar](255) NULL,
[Severity] [nvarchar](255) NULL,
[FixDate] [nvarchar](255) NULL,
[Assigned To] [nvarchar](255) NULL,
[Summary] [nvarchar](255) NULL,
[Product List] [nvarchar](255) NULL,
[Development Type] [nvarchar](255) NULL,
[Defect Category] [nvarchar](255) NULL,
[Defect Cause] [nvarchar](255) NULL,
[Est Ready for Retest] [nvarchar](255) NULL,
[Fix Stage] [nvarchar](255) NULL,
[Planned Fix in Deployment Event] [nvarchar](255) NULL,
[Planned Fix in Event Mgmt Group] [nvarchar](255) NULL,
[Est Fixed Date] [nvarchar](255) NULL,
[Other Owner] [nvarchar](255) NULL
)
答案 0 :(得分:0)
我用这个SQL得到了它:
select [Assigned To], [1-Urgent],[2-High],[3-Medium],[4-Low]
from (select [assigned to],[Business Severity] from defects where [assigned to] is not null) D
pivot
(count([Business Severity]) for [Business Severity] in ([1-Urgent],[2-High],[3-Medium],[4-Low])) V
感谢所有