Ordered Value by Column / Row

后端 未结 4 938
借酒劲吻你
借酒劲吻你 2021-01-16 15:24

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, Co         


        
相关标签:
4条回答
  • 2021-01-16 15:41

    Use:

    SELECT DISTINCT COL1 AS col
      FROM YOUR_TABLE
     WHERE col1 IS NOT NULL
    UNION
    SELECT DISTINCT COL2 AS col
      FROM YOUR_TABLE
     WHERE col2 IS NOT NULL
    UNION 
    SELECT DISTINCT COL3 AS col
      FROM YOUR_TABLE
     WHERE col3 IS NOT NULL
    UNION
    SELECT DISTINCT COL4 AS col
      FROM YOUR_TABLE
     WHERE col4 IS NOT NULL
    ORDER BY col
    

    UNION will remove duplicates between the statements; DISTINCT will return a unique list of values per statement. UNION ALL would be faster than UNION, but it doesn't remove duplicates.

    0 讨论(0)
  • 2021-01-16 15:50

    Try this one

    With MyTables as
    (
    SELECT  [Col1] as [ColX] FROM [Test].[dbo].[MyTable] 
    Union 
    SELECT  [Col2] as [ColX]   FROM [Test].[dbo].[MyTable]
    Union
    SELECT  [Col3] as [ColX]   FROM [Test].[dbo].[MyTable]
    Union
    SELECT  [Col4] as [ColX]   FROM [Test].[dbo].[MyTable]
    )
    select ColX from MyTables where ColX is not null
    
    0 讨论(0)
  • 2021-01-16 15:57
    select value from (    
        select col1 as value from table_name where col1 is not null
         union
        select col2 as value from table_name where col2 is not null
         union
        select col3 as value from table_name where col3 is not null
         union
        select col4 as value from table_name where col4 is not null     
    ) order by value
    
    0 讨论(0)
  • 2021-01-16 15:58

    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 讨论(0)
提交回复
热议问题