Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:48:29+00:00 2026-06-05T09:48:29+00:00

I have a table with three columns: A,B,C . The values are: +—+—–+—-+ |

  • 0

I have a table with three columns: A,B,C.

The values are:

+---+-----+----+
| A |  B  | C  |
+---+-----+----+
| 1 | -10 |  5 |
| 1 |   0 |  5 |
| 1 |  10 |  5 |
| 2 |  10 | 12 |
| 2 |   0 | 12 |
| 3 | -10 | 14 |
| 4 |   0 |  8 |
| 4 |  10 |  8 |
| 5 |   0 |  6 |
| 5 |   1 |  6 |
| 5 |  -5 |  6 |
+---+-----+----+

If I first order the data by column A, then column B, then column C (although I did make all column C values the same per column A value) how would I select the “first row” per column A?

So, this should result in:

+---+-----+----+
| A |  B  | C  |
+---+-----+----+
| 1 | -10 |  5 |
| 2 |   0 | 12 |
| 3 | -10 | 14 |
| 4 |   0 |  8 |
| 5 |  -5 |  6 |
+---+-----+----+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:48:30+00:00Added an answer on June 5, 2026 at 9:48 am
    SELECT  a, b, c
    FROM    (
            SELECT  *, ROW_NUMBER() OVER (PARTITION BY a ORDER BY b, c) rn
            FROM    mytable
            ) q
    WHERE   rn = 1
    ORDER BY
            a
    

    or

    SELECT  mi.*
    FROM    (
            SELECT  DISTINCT  a
            FROM    mytable
            ) md
    CROSS APPLY
            (
            SELECT  TOP 1 *
            FROM    mytable mi
            WHERE   mi.a = md.a
            ORDER BY
                    b, c
            ) mi
    ORDER BY
            a
    

    Create a composite index on (a, b, c) for the queries to work faster.

    Which one is more efficient depends on your data distribution.

    If you have few distinct values of a but lots of records within each a, the second query would be better.

    You could improve it even more by creating an indexed view:

    CREATE VIEW v_mytable_da
    WITH   SCHEMABINDING
    AS
           SELECT  a, COUNT_BIG(*) cnt
           FROM    dbo.mytable
           GROUP BY
                   a
    
    GO
    
    CREATE UNIQUE CLUSTERED INDEX
           pk_vmytableda_a
    ON     v_mytable_da (a)
    
    GO
    
    SELECT  mi.*
    FROM    v_mytable_da md
    CROSS APPLY
            (
            SELECT  TOP 1 *
            FROM    mytable mi
            WHERE   mi.a = md.a
            ORDER BY
                    b, c
            ) mi
    ORDER BY
            a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with three columns; the first column contains IDs and the
Ok, so lets say I have a table comprising three columns. The first column
I have a table in my data base that has three columns: Screen, Icon,
I have a logging table which has three columns. One column is a unique
I have a table ABR with three columns: two lookup values, A and B
In my Db initially i have one table with three columns and no data
I'm using SQL Server 2008 and I have a table with three columns: Length
I have a table with three filled columns named Name, City and Occupation. I
I have a mysql table with three columns: username, location, timestamp. This is basically
I have a (simplified) table consisting of three columns: id INT PRIMARY KEY NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.