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 8426509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:33:01+00:00 2026-06-10T04:33:01+00:00

How would i order all rows by ap_status in this order ‘n’, ‘p’, ‘d’,

  • 0

How would i order all rows by ap_status in this order 'n', 'p', 'd', 'c', 'b', 'x' before grouping by product_id?

This query orders correctly when you remove the GROUP BY

SELECT `account_id`, `product_id`, `product_name`, `ap_status`, count(`ap_id`) as `num` 
FROM (accounts_products_view)
WHERE `account_id` = 13
/*GROUP BY `product_id`*/
ORDER BY FIELD(`ap_status`, 'n', 'p', 'd', 'c', 'b', 'x') desc

So i tried using HAVING after viewing some similar questions but, this is not working before the group by either

SELECT account_id, product_id, product_name, ap_status, count(ap_id) as num,
CASE
WHEN ap_status = 'n' then 1
WHEN ap_status = 'p' then 2
WHEN ap_status = 'd' then 3
WHEN ap_status = 'c' then 4
WHEN ap_status = 'b' then 5
WHEN ap_status = 'x' then 6
END as `order`

FROM (accounts_products_view)
WHERE `account_id` = 13
GROUP BY product_id
HAVING `order` = MIN(`order`)

Any suggestions are greatly appreciated

  • 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-10T04:33:02+00:00Added an answer on June 10, 2026 at 4:33 am

    you might want to try:

    SELECT *, count(`ap_id`) as `num` FROM (
            SELECT `account_id`, `product_id`, `product_name`, `ap_status`, `ap_id`
            FROM (accounts_products_view)
            WHERE `account_id` = 13
            /*GROUP BY `product_id`*/
            ORDER BY FIELD(`ap_status`, 'n', 'p', 'd', 'c', 'b', 'x') desc
        ) as res GROUP BY product_id
    

    but keep in mind it is INCORRECT!. If you use group by, then either you need to use aggregate function, like SUM() for example or specify the field in group by as well. In your example the fields that you might correctly retreive are product_id and nothing else. Because of MySQL bug or something you don’t get exception on this query, but in postgresql you will get exception.
    If you have 2 rows with the same product_id and different ap_status, in theory you are not able to say which one should be returned by the query. I guess you have figured out that in MySQL the first row will be returned, that is why you want to order the rows before executing group by. Keep in mind that this is incorrect and very hackish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to get from a single table, all rows, but order them
I am trying to query all rows of a parts table even if their
For example the values for all rows for column rating are zero. ORDER BY
So I have written a query that will grab an order (this is for
I would like to delete all rows from table A that are not exist
How can I build a query where I would retrieve random rows? If I
I would like to have a spark datagrid that wordwraps all rows where relevant.
INFORMIX-SE: My users periodically run an SQL script [REORG.SQL] which unloads all rows from
I'd like to make a JQGrid where all rows are editable (in-line) all the
I want to do a MySQL Query which selects a given number of Rows

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.