SQL Server 2008: TOP 10 and distinct together

后端 未结 13 1562
刺人心
刺人心 2021-02-04 23:41

As the title says, I\'m using SQL Server 2008. Apologies if this question is very basic. I\'ve only been using SQL for a few days. Right now I have the following query:

13条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 23:59

    I know this thread is old, but figured I would throw in what came up with since I just ran into this same issue. It may not be efficient, but I believe it gets the job done.

    SELECT TOP 10 p.id, pl.nm, pl.val, pl.txt_val
    INTO #yourTempTable
    from dm.labs pl 
    join mas_data.patients p on pl.id = p.id   
    where pl.nm like '%LDL%' and val is not null
    
    select p.id, pl.nm, pl.val, pl.txt_val
    from #yourTempTable
    where id IN (select distinct id from #yourTempTable)
    

提交回复
热议问题