时间:2021-07-01 10:21:17 帮助过:2人阅读
示例2 根据唯一值来统计
DECLARE @hire_date_years TABLE ( hire_date_year NVARCHAR(50) ); DECLARE @sql_yearlist NVARCHAR(MAX); DECLARE @sql_command NVARCHAR(MAX); INSERT INTO @hire_date_years ( hire_date_year ) SELECT DISTINCT DATEPART(YEAR, Employee.HireDate) FROM HumanResources.Employee; SELECT @sql_yearlist = ISNULL(@sql_yearlist, ‘‘) + N‘,‘ + QUOTENAME(hire_date_year) FROM @hire_date_years SET @sql_yearlist = STUFF(@sql_yearlist, 1, 1, ‘‘) SET @sql_command = N‘ WITH employee_data AS ( SELECT Employee.BusinessEntityID , Employee.JobTitle , DATEPART(YEAR, Employee.HireDate) AS HireDate_Year FROM HumanResources.Employee ) SELECT JobTitle,‘ + @sql_yearlist + N‘ FROM employee_data PIVOT (COUNT(BusinessEntityID) FOR HireDate_Year IN (‘ + @sql_yearlist + N‘)) pivot_data‘ PRINT @sql_commandView Code
示例3: 写进SP里面
IF OBJECT_ID(N‘dbo.job_title_year_summary‘, ‘P‘) IS NOT NULL BEGIN DROP PROCEDURE dbo.job_title_year_summary; END SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE job_title_year_summary AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @hire_date_years TABLE ( hire_date_year NVARCHAR(50) ); DECLARE @sql_yearlist NVARCHAR(MAX); DECLARE @sql_command NVARCHAR(MAX); INSERT INTO @hire_date_years ( hire_date_year ) SELECT DISTINCT DATEPART(YEAR, Employee.HireDate) FROM HumanResources.Employee; SELECT @sql_yearlist = ISNULL(@sql_yearlist, ‘‘) + N‘,‘ + QUOTENAME(hire_date_year) FROM @hire_date_years SET @sql_yearlist = STUFF(@sql_yearlist, 1, 1, ‘‘) SET @sql_command = N‘ WITH employee_data AS ( SELECT Employee.BusinessEntityID , Employee.JobTitle , DATEPART(YEAR, Employee.HireDate) AS HireDate_Year FROM HumanResources.Employee ) SELECT JobTitle,‘ + @sql_yearlist + N‘ FROM employee_data PIVOT (COUNT(BusinessEntityID) FOR HireDate_Year IN (‘ + @sql_yearlist + N‘)) pivot_data‘ PRINT @sql_command; EXEC sp_executesql @sql_command; END GOView Code
有了这个存储过程,以后需要用它时,可以用昨时表来存储它的结果,做后一步的处理。
T-SQL Recipes之Customized Database Objects
标签: