USE [Montessorischool] GO /****** Object: StoredProcedure [dbo].[GetAttn1] Script Date: 8/28/2017 10:33:44 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[GetAttn1] @startdate date, @enddate date, @class1 varchar(20)
as
--DECLARE @startdate int
--DECLARE @enddate int
--declare @strdays NVARCHAR(4000) declare @cols NVARCHAR(4000) DECLARE @qry NVARCHAR(4000) DECLARE @where VARCHAR(20) begin
--select @startdate from att --union ALL --select @startdate
+ 1 from att --where @startdate < @enddate into #Dates ;
SELECT DISTINCT Date1 INTO #Dates FROM att where Date1 between @startdate and @enddate ORDER BY Date1;
SELECT @cols = COALESCE(@cols + ', ['+ convert(varchar(50),DATEPART(d,Date1),106) + ']', '['+ convert(varchar(50),DATEPART(d,Date1),106) + ']') from #Dates ;
SET @qry =
'SELECT * FROM (SELECT name, status,class, DATEPART(d,Date1) Date1 FROM att) tbldate PIVOT (MAX(status) FOR Date1 IN (' + @cols + ')) AS stat';
Execute(@qry)
-- Dropping temporary tables DROP TABLE #Dates;
end GetAttn1 '2017-08-10','2017-08-30','LKG'
Blockquote
Out put就是
Ashok LKG NULL NULL NULL NULL NULL NULL
A Krishan LKG P P P A A NULL
Naveen LKG P P P P P NULL
Santosh LKG P A P P P NULL
Ashok Pre NULL NULL NULL P NULL NULL
Sachin UKG NULL NULL P NULL NULL NULL
但我需要
Ashok LKG NULL NULL NULL NULL NULL NULL
A Krishan LKG P P P A A NULL
Naveen LKG P P P P P NULL
Santosh LKG P A P P P NULL
答案 0 :(得分:0)
将您的@qry更改为
SET @qry =
'SELECT * FROM (SELECT name, status,class, DATEPART(d,Date1) Date1 FROM att WHERE class = @class1) tbldate PIVOT (MAX(status) FOR Date1 IN (' + @cols + ')) AS stat';