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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:38:33+00:00 2026-06-02T06:38:33+00:00

I have seen posts which would fetch you pivot results but not unpivot, Need

  • 0

I have seen posts which would fetch you pivot results but not unpivot, Need to know if there is any clean way to achieve ? If not the any workaround would do as well ?

Execute this to see unpivot results in Management Studio

CREATE TABLE [dbo].[Payment](
    [PaymentId] [int] NOT NULL,
    [EmployeeId] [int] NOT NULL,
    [RegularHours] [decimal](18, 0) NULL,
    [OvertimeHOurs] [decimal](18, 0) NULL
) ON [PRIMARY]

go

insert into payment values (1, 1, 40,  10)
insert into payment values (1, 2, 20,  0)

go

select * from payment 

select * from payment unpivot ([hours] for [paytype] in ([RegularHours], [OvertimeHOurs]))a

The output for first Select statement

PaymentId   EmployeeId  RegularHours                            OvertimeHOurs
----------- ----------- --------------------------------------- 
1           1           40                                      10
1           2           20                                      0

(2 row(s) affected)

The output for second Select statement & this is what i am looking for

PaymentId   EmployeeId  hours                                   paytype
----------- ----------- ----------------------------------------------------- 
1           1           40                                      RegularHours
1           1           10                                      OvertimeHOurs
1           2           20                                      RegularHours
1           2           0                                       OvertimeHOurs

(4 row(s) affected)
  • 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-02T06:38:34+00:00Added an answer on June 2, 2026 at 6:38 am

    Ok, I cant see a way you can do it where it is translated into SQL, below is what I have come up with but this is all performed managed code.

    Or… you can simply create a view in SQL.

    var payments = Payments.Select (p => new {
                                OvertimeHOurs = new {
                                        p.PaymentId,
                                        p.EmployeeId,
                                        Hours = p.OvertimeHOurs,
                                        PayType = "OvertimeHOurs"
                                        },
                                RegularHours = new {
                                        p.PaymentId,
                                        p.EmployeeId,
                                        Hours = p.RegularHours,
                                        PayType = "RegularHours"
                                        }
                                }
                    );
    var result = payments.Select(a => a.OvertimeHOurs).Union(payments.Select (p => p.RegularHours));
    result.Dump(); // LINQPad Method
    

    SQL Generated is

    -- Region Parameters
    DECLARE @p0 NVarChar(1000) = 'OvertimeHOurs'
    DECLARE @p1 NVarChar(1000) = 'RegularHours'
    -- EndRegion
    SELECT [t4].[PaymentId], [t4].[EmployeeId], [t4].[OvertimeHOurs] AS [Hours], [t4].[value] AS [PayType]
    FROM (
        SELECT [t1].[PaymentId], [t1].[EmployeeId], [t1].[OvertimeHOurs], [t1].[value]
        FROM (
            SELECT [t0].[PaymentId], [t0].[EmployeeId], [t0].[OvertimeHOurs], @p0 AS [value]
            FROM [payment] AS [t0]
            ) AS [t1]
        UNION
        SELECT [t3].[PaymentId], [t3].[EmployeeId], [t3].[RegularHours], [t3].[value]
        FROM (
            SELECT [t2].[PaymentId], [t2].[EmployeeId], [t2].[RegularHours], @p1 AS [value]
            FROM [payment] AS [t2]
            ) AS [t3]
        ) AS [t4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen a few posts regarding this issue but not one specific to
So I have seen some other posts similar, but not quite answering what I
I have seen many posts on this subject, but none have been answered, and
i have seen similar posts here but i have had no luck solving my
I have seen several posts similar to this but none with a strait forward
I have seen the other posts but I am still having trouble. Below is
I have seen a few posts which suggest using something like the following inside
I've seen some other posts in this topic, but thought I would ask the
I know I've seen this done a lot in places, but I need something
I have seen a number of posts similar but wasn't able to achieve my

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.