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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:22:31+00:00 2026-06-13T06:22:31+00:00

I have the following view in SQL Server 2008. DEPT | EMP_ID | EMP_NAME

  • 0

I have the following view in SQL Server 2008.

DEPT | EMP_ID | EMP_NAME | P_DATE  | HOURS_WORKED

I want the view to be this way:

DEPT | EMP_ID | EMP_NAME | 2012-09-28 | 2012-09-29 | 2012-09-30 | 2012-10-01 ...

where the above date column header is P_DATE below which is “Hours_Worked” values of that employee on that particular date.

Like

2012-09-28

09:00:00

10:00:00

I am not sure whether I could achieve it using Pivot.

Please go to this link for clear understanding : SQL Server View Snapshots

  • 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-06-13T06:22:32+00:00Added an answer on June 13, 2026 at 6:22 am

    You can perform this with the PIVOT function. If you know the values that you want to turn into columns than you can hard code then using a static pivot:

    select *
    from 
    (
      select dept, emp_id, emp_name, p_date, hours_worked
      from table1
    ) x
    pivot
    (
      max(hours_worked)
      for p_date in ([2012-10-19], [2012-10-20], [2012-10-21])
    ) p
    

    See SQL Fiddle with Demo

    If you have an unknown number of values, then you can use dynamic sql to PIVOT the data:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' 
                            + QUOTENAME(convert(char(10), p_date, 120)) 
                        from table1
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT dept, emp_id, emp_name,' + @cols + ' from 
                 (
                    select dept, emp_id, emp_name, p_date, hours_worked
                    from table1
                ) x
                pivot 
                (
                    max(hours_worked)
                    for p_date in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

Imagine I have the following SELECT statement in a view (SQL Server 2008): SELECT
I have a view in SQL Server that is somewhat similar to the following
We have a database called AVL in SQL Server 2008 R2 SE. This database
I have the following tables in a SQL Server 2008 db: tblItem , which
I have following view <asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server> <h2>Tables <%=ViewData[RetriverName] %></h2> <%using (Html.BeginForm(ResfreshSelectedTables, Home))
I have the following view and table in my project, vw_get_arabia_upod_full_details arabia_upod_item_avalability_master I want
Does anyone have experience of when SQL Server 2008 R2 is able to automatically
I have a Sql Server 2005 table with following data: idHearing is the primary
I want to try and understand what SQL Server 2008 is doing when an
I created a view in SQL Server 2008. It is the result set from

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.