pivot row count P based on my below code in asp.net

RAVI 1,056 Reputation points
2021-07-15T14:23:44.907+00:00

hello

This is my code

I want to show total count

-- Creating table
CREATE TABLE #Employee_Attendence
(
EmpID bigint,
AttendenceDate datetime,
AttendenceStatus char
)

-- Inserting Values
INSERT #Employee_Attendence
VALUES
(1,'20100601', 'P')

INSERT #Employee_Attendence
VALUES
(1,'20100602', 'P')

INSERT #Employee_Attendence
VALUES
(1,'20100603', 'A')

INSERT #Employee_Attendence
VALUES
(2,'20100601', 'P')

INSERT #Employee_Attendence
VALUES
(2,'20100602', 'A')

INSERT #Employee_Attendence
VALUES
(2,'20100603', 'P')

-- Getting all distinct dates into a temporary table #Dates
SELECT DISTINCT AttendenceDate INTO #Dates
FROM #Employee_Attendence
ORDER BY AttendenceDate

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

  • ']','[' + CONVERT(varchar, AttendenceDate, 106) + ']')
    FROM #Dates
    ORDER BY AttendenceDate

-- Building the query with dynamic dates
DECLARE @qry NVARCHAR(4000)
SET @qry =
'SELECT EmpID, ' + @cols + ' FROM
(SELECT EmpID, AttendenceDate, AttendenceStatus
FROM #Employee_Attendence)p
PIVOT (MAX(AttendenceStatus) FOR AttendenceDate IN (' + @cols + ')) AS Pvt'

-- Executing the query
EXEC(@qry)

-- Dropping temporary tables
DROP TABLE #Dates
DROP TABLE #Employee_Attendence

Thank you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,081 Reputation points
    2021-07-16T06:47:21.583+00:00

    Hi @RAVI ,
    I suggest you could union the result of summing the "P" records.Just like this:

    CREATE TABLE #Employee_Attendence  
    (  
    EmpID bigint,  
    AttendenceDate datetime,  
    AttendenceStatus char  
    )  
    INSERT #Employee_Attendence VALUES(1,'20100601', 'P')  
    INSERT #Employee_Attendence VALUES(1,'20100602', 'P')  
    INSERT #Employee_Attendence VALUES(1,'20100603', 'A')  
    INSERT #Employee_Attendence VALUES(2,'20100601', 'P')  
    INSERT #Employee_Attendence VALUES(2,'20100602', 'A')  
    INSERT #Employee_Attendence VALUES(2,'20100603', 'P')  
      
      
    SELECT DISTINCT AttendenceDate INTO #Dates  
    FROM #Employee_Attendence  
    ORDER BY AttendenceDate  
      
    DECLARE @cols NVARCHAR(4000)  
    SELECT @cols = COALESCE(@cols + ',[' + CONVERT(varchar, AttendenceDate, 106)  
    + ']','[' + CONVERT(varchar, AttendenceDate, 106) + ']')  
    FROM #Dates  
    ORDER BY AttendenceDate  
      
    DECLARE @cols1 NVARCHAR(4000)  
    SELECT @cols1 = COALESCE(@cols1 + ',cast(sum(IIF([' + CONVERT(varchar, AttendenceDate, 106)  
    + ']=''P'',1,0)) as char(2))','cast(sum(IIF([' + CONVERT(varchar, AttendenceDate, 106) + ']=''P'',1,0)) as char(2))')  
    FROM #Dates  
    ORDER BY AttendenceDate  
      
    DECLARE @qry NVARCHAR(4000)  
    SET @qry =  
    ';WITH CTE AS (  
    SELECT EmpID, ' + @cols + ' FROM  
    (SELECT EmpID, AttendenceDate, AttendenceStatus  
    FROM #Employee_Attendence)p  
    PIVOT (MAX(AttendenceStatus) FOR AttendenceDate IN (' + @cols + ')) AS Pvt)  
    select cast(empid as char(10)) empid,'+@cols+' from CTE  
    union  
    select ''Total'','+@cols1+'from CTE'  
    EXEC(@qry)  
    

    Result:
    115289-capture2.png

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. RAVI 1,056 Reputation points
    2021-07-18T11:08:00.563+00:00

    Hello YijingSun-MSFT

    Thanks for your code but i want row total as well

    115519-image.png

    Please give me code for row total like this above
    Thank you

    0 comments No comments

  3. RAVI 1,056 Reputation points
    2021-07-18T13:22:49.423+00:00

    Msg 102, Level 15, State 1, Line 8
    Incorrect syntax near '='.


  4. RAVI 1,056 Reputation points
    2021-07-19T13:23:29.243+00:00

    Hello
    again error

    Incorrect syntax near =
    Msg 102, Level 15, State 1, Line 8

    can you post complete code it would be helpful

    0 comments No comments

  5. RAVI 1,056 Reputation points
    2021-07-19T13:23:29.37+00:00

    Hello
    again error

    Incorrect syntax near =
    Msg 102, Level 15, State 1, Line 8

    can you post complete code it would be helpful


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.