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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:55:33+00:00 2026-06-10T05:55:33+00:00

In sql server 2008, I have a table that has the columns [a],[b],[c],[sort] and

  • 0

In sql server 2008, I have a table that has the columns [a],[b],[c],[sort] and it has 4 records:

1,      NULL,  NULL    0
NULL,   2,     NULL    1
NULL,   NULL,  3       2
10,     NULL,  NULL    3

I need to combine all the rows in a way that i get one row as a result, and for each column I get the first (ordered by sort column) non-null value. So my result should be:

1,      2,     3

Can anyone suggest how to do this?
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-10T05:55:34+00:00Added an answer on June 10, 2026 at 5:55 am

    One way

    SELECT (SELECT TOP 1 [a]
            FROM   @T
            WHERE  [a] IS NOT NULL
            ORDER  BY [sort]) AS [a],
           (SELECT TOP 1 [b]
            FROM   @T
            WHERE  [b] IS NOT NULL
            ORDER  BY [sort]) AS [b],
           (SELECT TOP 1 [c]
            FROM   @T
            WHERE  [c] IS NOT NULL
            ORDER  BY [sort]) AS [c] 
    

    Or another

    ;WITH R
         AS (SELECT [a],
                    [b],
                    [c],
                    [sort]
             FROM   @T
             WHERE  [sort] = 0
             UNION ALL
             SELECT Isnull(R.[a], T.[a]),
                    Isnull(R.[b], T.[b]),
                    Isnull(R.[c], T.[c]),
                    T.[sort]
             FROM   @T T
                    JOIN R
                      ON T.sort = R.sort + 1
                         AND ( R.[a] IS NULL
                                OR R.[b] IS NULL
                                OR R.[c] IS NULL ))
    SELECT TOP 1 [a],
                 [b],
                 [c]
    FROM   R
    ORDER  BY [sort] DESC 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL Server 2008 database with a table that has a column
I'm using SQL Server 2008 and I have a table that has a 'Status'
I have SQL Server 2008 database with 2 tables: Table A has columns ID
Just a SQL Server 2008 generic question, I have a table that has around
I use SQL Server 2008 R2 and have a table that I want no
I have a table which contains my ads that can be searched in sql-server-2008.
I use SQL Server 2008 and have a table with 5 char typed columns.
DB: SQL Server 2005 We have a table that has data in this manner:
I have a table in SQL Server 2005 which has three columns: id (int),
I'm using Geoserver and SQL Server 2008. I have a table which has a

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.