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

  • Home
  • SEARCH
  • 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 6946255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:32:29+00:00 2026-05-27T13:32:29+00:00

So I have a SQL table that contains time-clock information like this. The number

  • 0

So I have a SQL table that contains time-clock information like this. The number of minutes an employee was at work on a given date. The table holds several years worth of but is missing Saturdays and Sundays and other non work days/holidays (with occasional exceptions).

 EmployeeID   Date Attended   Total Minutes
 -----------------------------------
 001          '11/01/2011'    319
 002          '11/01/2011'    355
 003          '11/01/2011'    352
 001          '11/02/2011'    340
 002          '11/02/2011'    322
 003          '11/02/2011'    351

I need to write a pivot that will show the data like this in a report that summarizes attendance to a given month.

 EmployeeId  '11/01/2011'  '11/02/2011' ....  (for all dates in a month)
 -----------------------------------------------------------------------
 001         319           340
 002         355           322
 003         352           351

I have a solution that works (see below)… but my approach uses dynamic sql to build the pivot statement from the days in a given month. In the back of my mind I keep thinking there has to be a way that does not use dynamic SQL – but it’s not coming to me. Any suggestions… am I stuck with Dynamic SQL?

DECLARE @columns VARCHAR(8000)
DECLARE @headers VARCHAR(8000)
DECLARE @lowdate Date 
DECLARE @highdate Date

SELECT @columns = COALESCE(@columns + ',[' + cast(Attended as varchar) + ']',
                     '[' + cast(Attended as varchar)+ ']')
FROM TimeAttended WHERE attended >= @date and attended <= @highdate
GROUP BY Attended

SELECT @headers = COALESCE(@Headers + ',Sum([' + cast(Attended as varchar) + ']) as [' + cast(Attended as varchar) + ']',
                     'Sum([' + cast(Attended as varchar)+ ']) as [' + cast(Attended as varchar) + ']')
FROM TimeAttended WHERE attended >= @date and attended <= @highdate
GROUP BY Attended

DECLARE @query VARCHAR(8000)
SET @query = '
SELECT employeeid, ' + @headers + '
FROM TimeAttended 
PIVOT
 (
 Sum(TotalMinutes)
 FOR [Attended]
 IN (' + @columns + ')
 )
 AS p group by employeeid'

EXECUTE(@query)
  • 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-27T13:32:30+00:00Added an answer on May 27, 2026 at 1:32 pm

    Besides the obvious one of using dynamic SQL (which I generally prefer to avoid if possible), there are two static options.

    The first is to get the rows as normal, and then pivoting the results in your display code (which you’re somewhat doing regardless). This is assuming you’re generating a report of some sort, and have the requisite processing power avaliable.

    The second requires thinking about the data slightly differently.
    Instead of creating the statement to look at a specific set of dates, code it to look at the set of the last 31 days (from some given date).
    That is, column2 is the given date, column3 is the given date – 1 day, etc:

    SELECT a.Id, COALESCE(b.Minutes, 0), COALESCE(c.Minutes, 0), (etc)
    FROM Employees as a
    LEFT JOIN TimeClock as b
    ON b.EmployeeId = a.id
    AND b.Date = @Given
    LEFT JOIN TimeClock as c
    ON C.EmployeeId = a.Id
    AND c.Date = DATEADD(DAY, 1, @Given)
    (etc)
    

    … And then mapping it to column1 of the results to column1 of the display table, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a table in a sql server 2008 database that contains bunch of
I have a table (T1) in t-sql with a column (C1) that contains almost
I have a table which contains my ads that can be searched in sql-server-2008.
I have a table that contains many rows of SQL commands that make up
We have a database that contains information about time booked to projects. We have
This is for MySQL and PHP I have a table that contains the following
I have a MS SQL table that I don't have any control over and
I have a SQL Server table in production that has millions of rows, and

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.