Using the distinct function in SQL

前端 未结 18 945
無奈伤痛
無奈伤痛 2021-02-06 16:55

I have a SQL query I am running. What I was wanting to know is that is there a way of selecting the rows in a table where the value in on one of those columns is distinct? When

18条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 17:37

    declare @temp as table (colA nchar, colB nchar, colC nchar, colD nchar, rownum int)
    
    insert @temp (colA, colB, colC, colD, rownum)
    select Test.ColA, Test.ColB, Test.ColC, Test.ColD, ROW_NUMBER() over (order by ColA) as rownum
        from Test
    
    select t1.ColA, ColB, ColC, ColD
    from @temp as t1
    join (
        select ColA, MIN(rownum) [min]
        from @temp
        group by Cola)
     as t2 on t1.Cola = t2.Cola and t1.rownum = t2.[min]
    

    This will return a single row for each value of the colA.

提交回复
热议问题