2013-05-03 27 views
1

您好我有考勤查询,这将产生使用旋转功能更改列名

这里的考勤报表的过程:

declare @in_date DATETIME  

/*Select all the stagign entries related to promotion id and investment type id */  
    /* also only those staging daat related interface status tracking*/ 

    -- Getting all distinct dates into a temporary table #Dates 
    SELECT a.date as full_date_of_attendence INTO #Dates 
    FROM dbo.getFullmonth(@in_date) a 
    ORDER BY a.date 



    -- The number of days will be dynamic. So building 
    -- a comma seperated value string from the dates in #Dates 
    SELECT @cols = COALESCE(@cols + ',[' + CONVERT(varchar, full_date_of_attendence, 106) 
            + ']','[' + CONVERT(varchar, full_date_of_attendence, 106) + ']') 
    FROM #Dates 
    ORDER BY full_date_of_attendence 


    --select @cols 

    ---- Building the query with dynamic dates 

SET @qry = 
    'SELECT * FROM 
    (SELECT admission_id, attendence_status , date_of_attendence 
    FROM dbo.tblattendence)emp 
    PIVOT (MAX(attendence_status) FOR date_of_attendence IN (' + @cols + ')) AS stat'  



    -- Executing the query 
    EXEC(@qry) 

    -- Dropping temporary tables 
    DROP TABLE #Dates 

这里是上述查询的输出::

admission_id 01 May 2013 02 May 2013 03 May 2013 
2    NULL    1    0 
3    NULL    1    1 
4    NULL    0    0 
5    NULL    0    1 

在这里,我想改变列的名称作为01,02,03...... ,我想为“P” 0的值1作为'A'

任何人都可以帮助我实现这?

+0

use select admission_id as AdmsnId FROM dbo.tblattendence – NetStarter 2013-05-03 13:26:28

回答

0

我会建议您的代码进行以下更改。如果你想要一个日期列表(1,2,3等),那么你可以使用DAY函数。

通常当我得到列的列表动态,我的选择是使用STUFFFOR XML PATH,我会改变该代码如下:

select @colsPiv = STUFF((SELECT ',' + QUOTENAME(cast(day(full_date_of_attendence) as varchar(2))) 
       from #Dates 
       GROUP BY full_date_of_attendence 
       ORDER BY full_date_of_attendence 
     FOR XML PATH(''), TYPE 
     ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'') 

然后,如果你想更换0A和一个1P,你将要创建一个查询来获取列的列表替换值:

select @colsSel = STUFF((SELECT ', case when ' + QUOTENAME(cast(day(full_date_of_attendence) as varchar(2)))+'= 1 then ''P'' else ''A'' end as '+QUOTENAME(cast(day(full_date_of_attendence) as varchar(2))) 
         from #Dates 
         GROUP BY full_date_of_attendence 
         ORDER BY full_date_of_attendence 
       FOR XML PATH(''), TYPE 
       ).value('.', 'NVARCHAR(MAX)') 
       ,1,1,'') 

基本上,这是肌酸GA类似这样的选择列表:

select case when [1] = 1 then 'P' else 'A' end as [1], ... 

那么你最终的查询将是:

SET @qry = 
'SELECT admission_id, '[email protected] +' 
FROM 
(
    SELECT admission_id, 
     attendence_status , 
     day(date_of_attendence) date_of_attendence 
    FROM dbo.tblattendence 
)emp 
PIVOT 
(
    MAX(attendence_status) 
    FOR date_of_attendence IN (' + @colsPiv + ') 
) AS stat' 

SQL Fiddle with Demo

0

,让我们改变只是你想要的两样东西,即

  1. CONVERT(CHAR(2), full_date_of_attendence, 106) - 使用CHAR(2)代替varchar
  2. CASE attendence_status when 1 then 'P' else 'A' END在选择...

代码的变化很小。希望这有助于您了解未来如何对其他代码进行类似更改。

declare @in_date DATETIME 

/*Select all the stagign entries related to promotion id and investment type id */  
/* also only those staging daat related interface status tracking*/ 

-- Getting all distinct dates into a temporary table #Dates 
SELECT a.date as full_date_of_attendence INTO #Dates 
FROM dbo.getFullmonth(@in_date) a 
ORDER BY a.date 



-- The number of days will be dynamic. So building 
-- a comma seperated value string from the dates in #Dates 
SELECT @cols = COALESCE(@cols + ',', '') + [' + 
     CONVERT(CHAR(2), full_date_of_attendence, 106) + ']' 
FROM #Dates 
ORDER BY full_date_of_attendence 


--select @cols 

---- Building the query with dynamic dates 

SET @qry = 
'SELECT * FROM 
(SELECT admission_id, CASE attendence_status when 1 then 'P' else 'A' END, date_of_attendence 
FROM dbo.tblattendence)emp 
PIVOT (MAX(attendence_status) FOR date_of_attendence IN (' + @cols + ')) AS stat'  



-- Executing the query 
EXEC(@qry) 

-- Dropping temporary tables 
DROP TABLE #Dates