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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:12:33+00:00 2026-05-18T08:12:33+00:00

Its a little difficult to explain. It might be easier to skip to the

  • 0

Its a little difficult to explain. It might be easier to skip to the examples.

A table has an id and four columns that each allow null.

ID, Col1, Col2, Col3, Col4

There are x number of rows. ( Usually less than 4 ) And only up to 4 distinct values will be used across the columns.

I am looking to return up to 4 rows, where each row in the resulting set is basically a column value where the value is selected left from right starting at the top preserving Col number. If another row has a value that is not column unique it is shifted to the next available column.

Examples:

If I have:

ID, Col1, Col2, Col3, Col4  
0,  A   , B   ,     , C  
1,      ,     , D   ,

I would like to return

A  
B  
D  
C

and

ID, Col1, Col2, Col3, Col4  
0,  A   , B   , D   ,   
1,  C   ,     ,     ,

Gives

A  
B  
D  
C 

and

ID, Col1, Col2, Col3, Col4  
0,  A   , B   , D   ,   
1,  C   ,     ,     ,  
2,  C   ,     ,     ,

Gives

A  
B  
D  
C 

Thanks! The scenario when there are non unique columns and spaces between values can be thrown out.
This will not happen:

a,b,,d
c,,,

This might help:

CREATE TABLE #original ( id int ,A INT, B INT, C INT, D INT );

INSERT INTO #original
--SELECT 0,1,2,null,4
--union 
--select 1,null,null,3,null
--
--
--SELECT 0,1,2,3,null
--union 
--select 1,4,null,null,null
--
--
SELECT 0,1,2,4,null
union 
select 1,3,null,null,null 
union 
select 2,3,null,null,null 

select * from #original order by id asc;

with cteOriginal as
(
    select *, RANK() over (partition by [SortOrder] order by id asc) as [NonUniqueSortOrder]
    from
    (
        select id, A as [value], 1 as [SortOrder]
        from #original
        where A is not null
        union all
        select id, B as [value], 2 as [SortOrder]
        from #original
        where B is not null
        union all
        select id, C as [value], 3 as [SortOrder]
        from #original
        where C is not null
        union all
        select id, D as [value], 4 as [SortOrder]
        from #original
        where D is not null
    ) as temp
)

select [value] from
(
select top 50 [value], ((([NonUniqueSortOrder] - 1) * 4) + [SortOrder]) sortedOrder
from cteOriginal
order by sortedOrder
) tmp
group by [value]
order by min(sortedOrder)

DROP TABLE #original
  • 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-05-18T08:12:33+00:00Added an answer on May 18, 2026 at 8:12 am

    I may not understand everything that you described you wanted. From reading your problem and comments from others, I am guessing that this is what you are looking for:

    Updated version:

    with cteOriginal as
    (
        select *, RANK() over (partition by [SortOrder] order by id asc) as [NonUniqueSortOrder]
        from
        (
            select id, A as [value], 1 as [SortOrder]
            from #original
            where A is not null
            union all
            select id, B as [value], 2 as [SortOrder]
            from #original
            where B is not null
            union all
            select id, C as [value], 3 as [SortOrder]
            from #original
            where C is not null
            union all
            select id, D as [value], 4 as [SortOrder]
            from #original
            where D is not null
        ) as temp
    )
    select [value]
    from cteOriginal
    where id = (select MIN(tmp.id) from cteOriginal tmp where tmp.value = cteOriginal.value)
    order by ((([NonUniqueSortOrder] - 1) * 4) + [SortOrder])
    

    I got rid of the duplicate values by picking the one with the smallest id, min(id). You can change it to use max(id).

    Initial version:

    with cteOriginal as
    (
        select *, RANK() over (partition by [column] order by id asc) as [NonUniqueSortOrder]
        from
        (
            select id, A as [value], 'A' as [Column], 1 as [SortOrder]
            from #original
            where A is not null
            union all
            select id, B as [value], 'B' as [Column], 2 as [SortOrder]
            from #original
            where B is not null
            union all
            select id, C as [value], 'C' as [Column], 3 as [SortOrder]
            from #original
            where C is not null
            union all
            select id, D as [value], 'D' as [Column], 4 as [SortOrder]
            from #original
            where D is not null
        ) as temp
    )
    select [value]
    from cteOriginal
    order by ((([NonUniqueSortOrder] - 1) * 4) + [SortOrder])
    

    By the way, I am using mssql 2005 for this query. Please comment and we’ll refine it.

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

Sidebar

Related Questions

its a little difficult to explain. I've build the mysql function which works fine
It's a little bit difficult to explain what I need, so I'll use some
It's a little difficult to explain, but what I am trying to achieve is
The objective is a little bit difficult to explain, so please, notify me if
its a little bit hard to understand. in the header.php i have this code:
I need some help with overlaying views using the prism framework.Its a little more
I wrote a nice little game which works well in its .fla form. Because
I am trying to create my own little MVC system, its working very well,
It's always seemed a little at odds with the principles of Java that the
It's weird I have a little request (in nodejs) ( request_working.js ) that require

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.