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 6804199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:26:12+00:00 2026-05-26T19:26:12+00:00

I have written an SQL Query that brings the result in below format ResourceName

  • 0

I have written an SQL Query that brings the result in below format

ResourceName    ProjectName  TaskName Timesheet Period  ActualWork  TimeByDay  DayName

    Jim Carry        Project A   Task A   2011-12           40        09/10/2011  Monday

     Jim Carry        Project A   Task A   2011-12           70        09/10/2011  Monday
     Jim Carry        Project B   Task A   2011-12           80        09/12/2011  Tue.
     Perth S.         Project A   Task A   2011-12           35        09/10/2011  Mon.

I want to write the query so that it gives me something like;

ResourceName    ProjectName         TaskName         Timesheet Period  Monday Tue  
Jim Carry           Project A        Task A            2011-12          110    80

As you can see that the new columns is created in second query name as Monday, Tuesday and so on. Plus the total is also calculated based on day and hence result of Monday column is 40+70 while of Tuesday is of 80+0.

Below is the query that brings the code snippet 1

DECLARE @startdate DATETIME
DECLARE @enddate DATETIME

SET @startdate='1/1/2011'
SET @enddate='12/31/2011'

SELECT TOP 100 [IR.TimesheetProjectHoursByDay].ResourceName,
               [IR.TimesheetProjectHoursByDay].ProjectName,
               [IR.TimesheetProjectHoursByDay].TaskName,
               [IR.TimesheetProjectHoursByDay].PeriodName + ' ' + '(' + CONVERT(NVARCHAR, [IR.TimesheetProjectHoursByDay].PeriodStartDate, 101) + ' - ' + CONVERT(NVARCHAR, [IR.TimesheetProjectHoursByDay].PeriodEndDate, 101) + ')' AS [Timesheet Period],
               [IR.TimesheetProjectHoursByDay].ActualWork,
               [IR.TimesheetProjectHoursByDay].TimeByDay,
              DATENAME(dw,TimeByDay) as [DayName]
FROM   [IR.TimesheetProjectHoursByDay]
WHERE  [IR.TimesheetProjectHoursByDay].TimeByDay >= @StartDate
       AND [IR.TimesheetProjectHoursByDay].TimeByDay <= @EndDate
       AND [IR.TimesheetProjectHoursByDay].[Project Departments] LIKE N'CSENG'
       AND [IR.TimesheetProjectHoursByDay].ActualWork > 0
       AND [IR.TimesheetProjectHoursByDay].ResourceName LIKE '%Jim%'
GROUP  BY [IR.TimesheetProjectHoursByDay].TimeByDay,
          [IR.TimesheetProjectHoursByDay].ResourceName,
          [IR.TimesheetProjectHoursByDay].ProjectName,
          [IR.TimesheetProjectHoursByDay].TaskName,
          [IR.TimesheetProjectHoursByDay].[Project Departments],
          [IR.TimesheetProjectHoursByDay].ResourceStandardRate,
          [IR.TimesheetProjectHoursByDay].PeriodName,
          [IR.TimesheetProjectHoursByDay].PeriodStartDate,
          [IR.TimesheetProjectHoursByDay].PeriodEndDate,
          [IR.TimesheetProjectHoursByDay].ActualWork
ORDER  BY [IR.TimesheetProjectHoursByDay].TimeByDay DESC 

Can you please suggest me what change i need to do in the SQL Query so that i can get the result as mentioned in code snippet 2?

Let me know for any inputs required. Please find test data sheet that make things look more clear at here
Please guide!

Thanks!

  • 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-26T19:26:13+00:00Added an answer on May 26, 2026 at 7:26 pm

    If I understand you correct you can try this:

    create table #tbl (ResourceName varchar(100), ProjectName varchar(100), TaskName varchar(100), ActualWork int, TimeByDay date)
    
    insert into #tbl values    ('Jim Carry','Project A','Task A',40,'20111009'),
    ('Jim Carry','Project A','Task A',70,'20111009'),
    ('Jim Carry','Project B','Task B',80,'20111209'),
    ('Perth S.','Project A','Task A',35,'20111209')
    
    select ResourceName,ProjectName, year(TimeByDay), MONTH(TimeByDay),
           sum(case when datename(weekday, TimeByDay)='Monday' then ActualWork else 0 end) Monday,
           sum(case when datename(weekday, TimeByDay)='Tuesday' then ActualWork else 0 end) Tuesday,
           sum(case when datename(weekday, TimeByDay)='Wednesday' then ActualWork else 0 end) Wednesday,
           sum(case when datename(weekday, TimeByDay)='Thursday' then ActualWork else 0 end) Thursday,
           sum(case when datename(weekday, TimeByDay)='Thursday' then ActualWork else 0 end) Thursday,
           sum(case when datename(weekday, TimeByDay)='Friday' then ActualWork else 0 end) Friday,
           sum(case when datename(weekday, TimeByDay)='Saturday' then ActualWork else 0 end) Saturday,
           sum(case when datename(weekday, TimeByDay)='Sunday' then ActualWork else 0 end) Sunday
    from #tbl
    group by ResourceName,ProjectName, year(TimeByDay), MONTH(TimeByDay)
    
    drop table #tbl
    

    You can also use PIVOT as CJM suggested.

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

Sidebar

Related Questions

I have written a SQL query that works just fine, but am having a
I have written an SQL query that gets unread messages but I think I
As I had written in title, I have SQL query, run on Oracle DB,
Hi I have some software written in VB6.0 that uses an SQL Server, but
I have written an sql script for updating a database that runs in SQL
I have written this query in sql server 2005 but it still will show
I have a big fat query that's written dynamically to integrate some data. Basically
I have a SQL-Sever query that returns the Capacity available to a person on
I have written a MDX query which works fine in SQL Server Management Studio.
I am building up an SQL query in VB.net and have written a routine

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.