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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:13:38+00:00 2026-05-16T22:13:38+00:00

I’ve been asked to change a pivot query that currently displays a week ending

  • 0

I’ve been asked to change a pivot query that currently displays a week ending date, category and hours by date for one week in a row. I’ve been asked to display a comment field at the end of the row and I can’t figure out how to alter the query to do this.

The table is structured like this

Category   Date      Comments     Hours
test       8/2/2010  myComment      2
test       8/3/2010                 8
test       8/4/2010                 4
test       8/5/2010                 3
test       8/6/2010                 5

I would like the data to display like this. I have a query that will diplay all of this except the comment. On the front End I’m only going to allow one comment per week and add it to the Monday date row in the table for each week/category combination.

WeekEnding Category  SunHrs MonHrs TuesHrs WedHrs ThuHrs FriHrs SatHrs  Comment
8/7/2010   test      0      1      1     1      1      1      1       myComment

Here is the query before adding the comment field which works fine.

DECLARE @WeekEnding datetime
DECLARE @UserName nvarchar(245)

SET @WeekEnding = '09/04/2010'
SET @UserName = 'brogers'


SELECT
   @WeekEnding      WeekEnding

  ,CategoryID
  ,isnull([1], 0)  SunHrs
  ,isnull([2], 0)  MonHrs
  ,isnull([3], 0)  TueHrs
  ,isnull([4], 0)  WedHrs
  ,isnull([5], 0)  ThuHrs
  ,isnull([6], 0)  FriHrs
  ,isnull([7], 0)  SatHrs
 from (select  CategoryID, Datepart(dw, TimeEntryDate) DOW, TimeEntryDuration Hours
        from dbo.aspnet_starterkits_TimeEntry
        where TimeEntryDate between dateadd(dd,  -6, @WeekEnding) and @WeekEnding) Source
  pivot (max(Hours) for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt

I’m not sure how to add the comment field to the end of the row. When I do add it I get a result like this

WeekEnding Category  SunHrs MonHrs TuesHrs WedHrs ThuHrs FriHrs SatHrs  Comment
8/7/2010   test      0      0      1       1      1      1      0       

8/7/2010   test      0      1      0       0      0      0      0       myComment

I only want one row per weekending/category combination and one comment per row in the output.
Here is the query that where I added the comment field and displays incorrectly.

Can anyone point out how to display one comment per week/categroy row?

DECLARE @WeekEnding datetime
DECLARE @UserName nvarchar(245)

SET @WeekEnding = '09/04/2010'
SET @UserName = 'brogers'


SELECT
   @WeekEnding      WeekEnding
  ,TimeEntryDescription 
  ,CategoryID
  ,isnull([1], 0)  SunHrs
  ,isnull([2], 0)  MonHrs
  ,isnull([3], 0)  TueHrs
  ,isnull([4], 0)  WedHrs
  ,isnull([5], 0)  ThuHrs
  ,isnull([6], 0)  FriHrs
  ,isnull([7], 0)  SatHrs
 from (select  
        CategoryID, 
        Datepart(dw, TimeEntryDate) DOW, 
        TimeEntryDuration Hours, 
        TimeEntryDescription
        from dbo.aspnet_starterkits_TimeEntry
        where TimeEntryDate between dateadd(dd,  -6, @WeekEnding) and @WeekEnding) Source
  pivot (max(Hours) for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt

Even though the source table has a comment field for every day of the week, I only want one comment per week and one row per week/category combination.

I will restrict the input to only allow one per week (monday for example) and want this one comment to display at the end of the row on the output 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-16T22:13:38+00:00Added an answer on May 16, 2026 at 10:13 pm

    This is probably easier with an old style pivot

    ;With [Source] As
    (
    select  
            CategoryID, 
            Datepart(dw, TimeEntryDate) DOW, 
            TimeEntryDuration Hours, 
            TimeEntryDescription
            from dbo.aspnet_starterkits_TimeEntry
            where TimeEntryDate between dateadd(dd,  -6, @WeekEnding) and @WeekEnding
    )
    SELECT
       @WeekEnding      WeekEnding
      ,TimeEntryDescription 
      ,CategoryID
      ,max(case when DOW = 1 then [Hours] else 0 end)  SunHrs
      ,max(case when DOW = 2 then [Hours] else 0 end)  MonHrs
      ,max(case when DOW = 3 then [Hours] else 0 end)  TueHrs
      ,max(case when DOW = 4 then [Hours] else 0 end)  WedHrs
      ,max(case when DOW = 5 then [Hours] else 0 end)  ThuHrs
      ,max(case when DOW = 6 then [Hours] else 0 end)  FriHrs
      ,max(case when DOW = 7 then [Hours] else 0 end)  SatHrs
      ,max(comment) as comment
     from [Source]
     group by 
       TimeEntryDescription 
      ,CategoryID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.