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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:06:35+00:00 2026-06-03T13:06:35+00:00

Basically, I have created a pivot query wherein it will display the total_work_hours per

  • 0

Basically, I have created a pivot query wherein it will display the total_work_hours per each costcode for each employee.

This is my desired output:

employeeno  8322.170    10184.2648    8321.169    10184.2649 <- costcodes
--------------------------------------------------------------------------
080418         10.00          1.50        NULL          NULL
080441          6.50          NULL        1.00          3.00

but this is the result of my query:

employeeno  8322.170    10184.2648    8321.169    10184.2649 <- costcodes
--------------------------------------------------------------------------
080418         10.00          NULL        NULL          NULL
080418          NULL          1.50        NULL          NULL
080441          NULL          NULL        1.00          NULL
080441          6.50          NULL        NULL          NULL
080441          NULL          NULL        NULL          3.00

This is the result of my inner query:

      employeeno    costcoding  hour_per_costcode        
      --------------------------------------------------
      PH080418   8322.170       10.00
      PH080418   10184.2648     1.50
      PH080441   8321.169       1.00
      PH080441   8322.170       6.50
      PH080441   10184.2649     3.00

This is my query:

WITH PivotData AS  
    (SELECT wa.id,wa.sitecode, wa.companycode, wa.startdate, wa.enddate,  
            wa.description, wa.ratetypeid, wa_details.employeeno,  
            CAST(wa_details.costcode AS NVARCHAR(MAX)) + '.' +
            CAST(wa_details.subcostcode AS NVARCHAR(MAX)) costcoding ,
            wa_details.subcostcode, wa_details.hrswork [hour_per_costcode],  
            view_ttl_hours.ttl_work_hrs 
       FROM workallocation wa  
      INNER JOIN workallocation_details wa_details 
         ON wa.id = wa_details.workallocationid   
      INNER JOIN 
            (SELECT SUM(ttl_work_hrs) ttl_work_hrs, employee_id 
               FROM vwu_SUM_TIMESHEET_DAILY  
              WHERE TKSDATE BETWEEN '02-09-2012' AND '02-09-2012' 
              GROUP BY employee_id
            ) view_ttl_hours 
         ON wa_details.employeeno=view_ttl_hours.employee_id  WHERE wa.id=99
    )
SELECT employeeno, [8322.170], [10184.2648], [8321.169], [10184.2649]
  FROM PivotData   
 PIVOT (MAX([hour_per_costcode])          
        FOR costcoding         
         IN ([8322.170], [10184.2648], [8321.169], [10184.2649])
        ) AS PivotResult  
 ORDER BY employeeno;

My question is how will I be able to unite the value for my multiple rows so that my desire output will be achieve? I also try the cross tab query but the result is also the same.

I need some guidance on how to solve this; can you help?

  • 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-03T13:06:37+00:00Added an answer on June 3, 2026 at 1:06 pm

    Remove the columns you don’t need from the CTE. Something like this should work for you.

    WITH PivotData AS  
        (SELECT wa_details.employeeno,  
                CAST(wa_details.costcode AS NVARCHAR(MAX)) + '.' +
                CAST(wa_details.subcostcode AS NVARCHAR(MAX)) costcoding ,
                wa_details.subcostcode, wa_details.hrswork [hour_per_costcode]
           FROM workallocation wa  
          INNER JOIN workallocation_details wa_details 
             ON wa.id = wa_details.workallocationid   
          INNER JOIN 
                (SELECT SUM(ttl_work_hrs) ttl_work_hrs, employee_id 
                   FROM vwu_SUM_TIMESHEET_DAILY  
                  WHERE TKSDATE BETWEEN '02-09-2012' AND '02-09-2012' 
                  GROUP BY employee_id
                ) view_ttl_hours 
             ON wa_details.employeeno=view_ttl_hours.employee_id  WHERE wa.id=99
        )
    SELECT employeeno, [8322.170], [10184.2648], [8321.169], [10184.2649]
      FROM PivotData   
     PIVOT (MAX([hour_per_costcode])          
            FOR costcoding         
             IN ([8322.170], [10184.2648], [8321.169], [10184.2649])
            ) AS PivotResult  
     ORDER BY employeeno;
    

    Above query with your data:

    WITH PivotData AS  
        (select 'PH080418' as employeeno,   8322.170 as costcoding, 10.00 as hour_per_costcode union all
         select 'PH080418', 10184.2648, 1.50 union all
         select 'PH080441', 8321.169,   1.00 union all
         select 'PH080441', 8322.170,   6.50 union all
         select 'PH080441', 10184.2649, 3.00
        )
    SELECT employeeno, [8322.170], [10184.2648], [8321.169], [10184.2649]
      FROM PivotData   
     PIVOT (MAX([hour_per_costcode])          
            FOR costcoding         
             IN ([8322.170], [10184.2648], [8321.169], [10184.2649])
            ) AS PivotResult  
     ORDER BY employeeno;
    

    Result:

    employeeno 8322.170   10184.2648   8321.169   10184.2649
    ---------- ---------- ------------ ---------- -----------
    PH080418   10.00      1.50         NULL       NULL
    PH080441   6.50       NULL         1.00       3.00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I basically have two subviews within my main view. I created each subview
I'm new to this site, and basically I have created a Windows Form using
I have created following thing in android using android compatibility support package Basically i
Basically, I have created a custom control that uses an UpdatePanel, and as I
Basically, I am using Wireshark looking at captures that have been created previously. How
Basically, I have a model where I've created a superclass that many other classes
I have a query that is more complicated, but basically creates an HTML page
I have a mock up of a sql query that will represent a real
I have created a certificate basically straight from the keytool example page: keytool -genkey
Basically I have created an app that reads and writes values in text boxes

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.