SQL Server 2008: TOP 10 and distinct together

后端 未结 13 1557
刺人心
刺人心 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-05 00:06

    You could use a Common Table Expression to get the top 10 distinct ID's and then join those to the rest of your data:

    ;WITH TopTenIDs AS
    ( 
       SELECT DISTINCT TOP 10 id
       FROM dm.labs 
       ORDER BY ......
    )
    SELECT 
        tti.id, pl.nm, pl.val, pl.txt_val
    FROM
        TopTenIDs tti
    INNER JOIN
        dm.labs pl ON pl.id = tti.id
    INNER JOIN 
        mas_data.patients p ON pl.id = p.id
    WHERE
        pl.nm like '%LDL%'
        AND val IS NOT NULL
    

    That should work. Mind you: if you have a "TOP x" clause, you typically also need an ORDER BY clause - if you want the TOP 10, you need to tell the system in what order that "TOP" is.

    PS: why do you even join the "patients" table, if you never select any fields from it??

    0 讨论(0)
  • 2021-02-05 00:06

    This is the right answer and you can find 3 heights value from table

    SELECT TOP(1)  T.id FROM (SELECT DISTINCT TOP(3) st.id  FROM Table1 AS t1 , Table2 AS t2 WHERE t1.id=t2.id ORDER BY (t2.id) DESC ) T ORDER BY(T.id) ASC
    
    0 讨论(0)
  • 2021-02-05 00:11

    I think the problem is that you want one result for each p.id?

    But you are getting "duplicate" results for some p.id's, is that right?

    The DISTINCT keyword applies to the entire result set, so applies to pl.nm, pl.val, pl.txt_val, not just p.id.

    You need something like

    SELECT TOP 10 p.id, max( p1.nm ), max (p1.val), ...
    FROM ...
    GROUP BY p.id
    

    Won't need the distinct keyword then.

    0 讨论(0)
  • 2021-02-05 00:11
    SELECT TOP 14 A, B, C
      FROM MyDatabase
      Where EXISTS 
       (
         Select Distinct[A] FROM MyDatabase
        )
    
    0 讨论(0)
  • 2021-02-05 00:11
    SELECT DISTINCT * FROM (
    
    SELECT TOP 10 p.id, pl.nm, pl.val, pl.txt_val
    
    from dm.labs pl
    join mas_data.patients p    
      on pl.id = p.id
      where pl.nm like '%LDL%'
      and val is not null
    
    )
    
    0 讨论(0)
  • 2021-02-05 00:13

    The easy option is to use group by and select min/max for all other fields

    SELECT TOP 10 
        p.id, 
        max(pl.nm),
        max(pl.val),
        max(pl.txt_val)
    from 
        dm.labs pl
    join 
        mas_data.patients p    
    on 
        pl.id = p.id
      where 
        pl.nm like '%LDL%'
    and 
        val is not null
    group by 
        p.id
    

    This can get quite tedious for wide table so the other option is to use rank over and partiion

    SELECT TOP 10 
        p.id, 
         pl.nm, 
         pl.val, 
       pl.txt_val, 
        rank() over(partition by p.id order by p.id) as Rank
    from 
        dm.labs pl
    join 
        mas_data.patients p    
    on 
        pl.id = p.id
      where 
        pl.nm like '%LDL%'
    and 
        val is not null
    and
        Rank = 1
    
    0 讨论(0)
提交回复
热议问题