SQL Server Query

VolginRnB 256 Reputation points
2021-04-05T20:18:55.757+00:00

I have a database SQL Server and I have a doubt about a query that I need to execute it.

I Have a table and I run a query "select * from mytable" the results is below

84622-image.png

I Would like run a query that show only bigger lvl for each class as example below, but I don't have idea how do this query. Anyone can help me ? please!

84572-image.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,640 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,242 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,547 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lukas Yu -MSFT 5,816 Reputation points
    2021-04-06T01:29:41.13+00:00

    When multiple persons get same highest lvl, do you have specific rules for picking out which one to be selected? If not, firstly, you could try following see if it fits your need:

      select Name, Class, lvl
         from
         (
             select *, ROW_NUMBER() over (partition by Class order by lvl desc) as r
             from Test1
         ) t
         where r = 1
         order by Class
    

3 additional answers

Sort by: Most helpful
  1. Viorel 111.7K Reputation points
    2021-04-05T20:30:00.057+00:00

    Check this approach:

    select [Name], Class, lvl
    from
    (
        select *, rank() over (partition by Class order by lvl ) as r
        from MyTable
    ) t
    where r = 1
    order by Class desc
    
    0 comments No comments

  2. VolginRnB 256 Reputation points
    2021-04-05T22:52:10.137+00:00

    @Viorel Sorry! I think that I described wrong, I'm going to explain again.

    I Have a table and I run a query "select * from mytable" the results is below

    84588-image.png

    I Would like run a query that show only bigger lvl for each class as example below.

    84635-image.png

    What SQL Server Query Should I Use for it ?


  3. Viorel 111.7K Reputation points
    2021-04-06T05:50:11.79+00:00

    To show all people that have the highest lvl, try this query:

    select Name, Class, lvl
    from
    (
        select *, rank() over (partition by Class order by lvl desc) as r
        from MyTable
    ) t
    where r = 1
    order by Class
    
    0 comments No comments