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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:30:10+00:00 2026-06-09T23:30:10+00:00

I have a simple query which returns the following rows : Current rows: Empl

  • 0

I have a simple query which returns the following rows :

Current rows:

Empl    ECode   DCode       LCode       Earn    Dedn    Liab
====    ====    =====       =====       ====    ====    ====
123     PerHr   Null        Null        13      0       0
123     Null    Union       Null        0       10      0
123     Null    Per         Null        0       20      0
123     Null    Null        MyHealth    0       0       5
123     Null    Null        401         0       0       10
123     Null    Null        Train       0       0       15
123     Null    Null        CAFTA       0       0       20

However, I needed to see the above rows as follows :

Empl    ECode   DCode   LCode       Earn    Dedn    Liab
====    ====    =====   =====       ====    ====    ====
123     PerHr   Union   MyHealth    13      10      5
123     Null    Per     401         0       20      10
123     Null    Null    Train       0       0       15
123     Null    Null    CAFTA       0       0       20

It’s more like merging the succeeding rows into the preceding rows wherever there are Nulls encountered for EarnCode, DednCode & LiabCode. Actually what I wanted to see was to roll up everything to the preceding rows.

In Oracle we had this LAST_VALUE function which we could use, but in this case, I simply cannot figure out what to do with this.

In the example above, ECode‘s sum value column is Earn, DCode is Dedn, and LCode is Liab; notice that whenever either of ECode, DCode, or LCode is not null, there is a corresponding value in Earn, Dedn, or the Liab columns.

By the way, we are using SQL Server 2008 R2 at work.

Hoping for your advice, 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-06-09T23:30:12+00:00Added an answer on June 9, 2026 at 11:30 pm

    This is basically the same technique as Tango_Guy does but without the temporary tables and with the sort made explicit. Because the number of rows per Empl is <= the number of rows already in place, I didn’t need to make a dummy table for the leftmost table, just filtered the base data to where there was a match amongst the 3 codes. Also, I reviewed your discussion and the Earn and ECode move together. In fact a non-zero Earn in a column without an ECode is effectively lost (this is a good case for a constraint – non-zero Earn is not allowed when ECode is NULL):

    http://sqlfiddle.com/#!3/7bd04/3

    CREATE TABLE data(ID INT IDENTITY NOT NULL,
                      Empl VARCHAR(3), 
                      ECode VARCHAR(8), 
                      DCode VARCHAR(8), 
                      LCode VARCHAR(8),
                      Earn INT NOT NULL,
                      Dedn INT NOT NULL,
                      Liab INT NOT NULL ) ;
    
    INSERT INTO data (Empl, ECode, DCode, LCode, Earn, Dedn, Liab)
    VALUES ('123', 'PerHr', NULL, NULL, 13, 0, 0),
            ('123', NULL, 'Union', NULL, 0, 10, 0),
            ('123', NULL, 'Per', NULL, 0, 20, 0),
            ('123', NULL, NULL, 'MyHealth', 0, 0, 5),
            ('123', NULL, NULL, '401', 0, 0, 10),
            ('123', NULL, NULL, 'Train', 0, 0, 15),
            ('123', NULL, NULL, 'CAFTA', 0, 0, 20);
    
    WITH basedata AS (
        SELECT *, ROW_NUMBER () OVER(ORDER BY ID) AS OrigSort, ROW_NUMBER () OVER(PARTITION BY Empl ORDER BY ID) AS EmplSort
        FROM data
    ),
    E AS (
      SELECT Empl, ECode, Earn, ROW_NUMBER () OVER(PARTITION BY Empl ORDER BY OrigSort) AS EmplSort
      FROM basedata
      WHERE ECode IS NOT NULL
    ),
    D AS (
      SELECT Empl, DCode, Dedn, ROW_NUMBER () OVER(PARTITION BY Empl ORDER BY OrigSort) AS EmplSort
      FROM basedata
      WHERE DCode IS NOT NULL
    ),
    L AS (
      SELECT Empl, LCode, Liab, ROW_NUMBER () OVER(PARTITION BY Empl ORDER BY OrigSort) AS EmplSort
      FROM basedata
      WHERE LCode IS NOT NULL
    )
    SELECT basedata.Empl, E.ECode, D.Dcode, L.LCode, E.Earn, D.Dedn, L.Liab
    FROM basedata
    LEFT JOIN E
        ON E.Empl = basedata.Empl AND E.EmplSort = basedata.EmplSort
    LEFT JOIN D
        ON D.Empl = basedata.Empl AND D.EmplSort = basedata.EmplSort
    LEFT JOIN L
        ON L.Empl = basedata.Empl AND L.EmplSort = basedata.EmplSort
    WHERE E.ECode IS NOT NULL OR D.DCode IS NOT NULL OR L.LCode IS NOT NULL
    ORDER BY basedata.Empl, basedata.EmplSort
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple query over a table, which returns results like the following:
I have a query which returns the number of rows of a distinct device_type
Say I have a query which returns the following result: | val | type
I have a simple ajax query to a file which basically returns in JSON
I have a simple query which queries by item_id and it obviously works because
I have a simple query which is returning records based on the field status
Let's say I have the following simple query SELECT TOP 1 name FROM months
I have the following simple MySQL query, called from PHP: SELECT foo_id, SUM(number_of_guests) FROM
I'm newbie to Neo4j/GraphDB and have created following simple graph node[1]user1 which is 'friend'
I have a simple query that finds cities that are within a certain distance

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.