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

  • SEARCH
  • Home
  • 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 7631921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:28:59+00:00 2026-05-31T06:28:59+00:00

I am using GROUP BY to only select one row for all entries that

  • 0

I am using GROUP BY to only select one row for all entries that share the same ‘cat_no’ value, because GROUP BY retrieves a random row I need to use ORDER BY ABS(track) to ensure it selects the row that is track number “1” in the database. However once I have this set of results I need to actually have the final order by ‘date’ another field in the DB, but I can’t seem to order twice.

SELECT * FROM cds  
WHERE genre='rock'  
GROUP BY cat_no  
ORDER BY date DESC,ABS(track) DESC  
LIMIT 0,3

Sql is only honouring the first order by clause, in this case ‘date’, if I swap them around then ‘track’ is used and ‘date’ ignored

Table

genre cat_no  track  date
rock  001     1      09323123
rock  001     2      09323123
rock  001     bundle 09323123
rock  002     1      09323123
rock  002     2      09323123
rock  002     bundle 09323123
rock  003     1      09323123
rock  003     2      09323123
rock  003     bundle 09323123
  • 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-05-31T06:29:01+00:00Added an answer on May 31, 2026 at 6:29 am

    If you always want track 1, you don’t need to group:

    SELECT * FROM cds
    WHERE genre = 'rock' AND track = 1
    ORDER BY date DESC
    LIMIT 0, 3
    

    If you want the first track (not necessarily track 1), then you need a subquery:

    SELECT * FROM cds c
    WHERE genre = 'rock'
    AND track = (
        SELECT MIN(track) FROM cds innerq
        WHERE innerq.cat_no = c.cat_no
        LIMIT 1
    )
    ORDER BY date DESC
    LIMIT 0, 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using GROUP BY for all types of aggregate queries over the years.
I have panel that is using group layout to organize some label. I want
I have a doubt in layouts.I have designed one layout using group layout using
I have the following xsl template that I'm using to group my xsl. The
I'm using the Filament Group's jQuery Select Slider to produce a form for selecting
I am having trouble writing a query to select one row per date, given
I need to return only the names that have more than one franchise. I
Please help me to get all rows of same group in tableView. i am
How can I using group by with union in T-SQL? I want to group
I am using the MySQL GROUP BY function, and want to know if there

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.