2017-08-28 55 views
-4
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 

出放是找到一流的基地出席,開始日期和結束日期

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 
+2

請解釋你想做什麼。你想要的是什麼。還提供了一些樣本數據的預期結果 – Squirrel

+0

我有表名att有商店出勤率的學生lkg,ukg和pre。我希望從開始日期到結束日期一次只能看到一個班級(在運行時提供)的預期數據,我需要 –

回答

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'; 
+0

這不起作用 –

相關問題