Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3981144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:22:30+00:00 2026-05-20T05:22:30+00:00

I have employees attendance table from date 1 to 31 with column names like

  • 0

I have employees attendance table from date 1 to 31 with column names like 1, 2, 3, 4 5…..31
and datatype is nVarchar(5)

I am adding values in those columns (P, H, A) for example:

P = Present
A = Absence
H = Holiday

Now i want to calculate Total (P=Present) in last column that is [TotalPresnets] of Employee.
How to calculate (Count) the string values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:22:31+00:00Added an answer on May 20, 2026 at 5:22 am

    Because you made 31 columns, every type of manipulation will involve fetching all 31 of those columns. This is one of a million reasons why this design is going to cause you a lot of pain. So, I have to say you need to consider changing this design!

    If you must stick with this, you will end up with code something like this:

    select
        EmployeeID,
        (
            case when Day0 = 'P' then 1 else 0 end
            + case when Day1 = 'P' then 1 else 0 end
            + case when Day2 = 'P' then 1 else 0 end
            ...
            + case when Day31 = 'P' then 1 else 0 end
        ) as TotalPresent
    from
        Attendance
    

    Another ugly solution would be to use dynamic SQL to generate the query above.

    In general, when you are forced into such ugly solutions, it’s a good indication that you are approaching the problem wrong.

    Note also that this is some 40 lines of code for a very basic operation on a single row. Carrying this technique into the rest of your application is going to cause far more headaches. Imagine if you had to change any logic in here; what should be one line is now 31.

    A better design would yield more intuitive / maintainable / readable code:

    select
        EmployeeID,
        count(*) TotalPresent
    from
        EmployeeDailyAttendance eda
        inner join Days d on d.ID = eda.DayID
    where
        eda.Code = 'p'
    group by
        EmployeeID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with several employees. They have the following columns empid,datecolumn1,is_valid. Very
I'd like to have an 'UnassignedDepartment' object instead of letting employees have a null
i have the following query to list the employees of two table. i need
i have Employees Table (Eno, FName, Lname, DOB, Tell, Address, Salary, Pno, Bno) //Clients
I have this method that is supposed to search collection of Employees by names.
I have an Employees table: EmployeeID | EmployeeName --------------------------- 1 | Jack 2 |
I have an Employees table. I am using Oracle database. Employees Table - Employee_id
I have a table employee having columns id (primary key), *employee_name* and another table
So I have a SQL table that contains time-clock information like this. The number
Here I have a Leave table with column of AttendanceId(PK, incremental), EmpId, Check_In_Time, Check_Out_Time,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.