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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:07:00+00:00 2026-05-17T00:07:00+00:00

SQL group by problem I have a SQL group by problem. My table has

  • 0

SQL group by problem 

I have a SQL group by problem. My table has the following form.

Cust_id.  Price_id     Price. 
----------------------------  
1.          556.        5000. 
-----------------------------  
2.          654.         600. 
2.          432.         487. 
2.          546.         500. 
---------------------------  
3.          455.         200. 
3.          877.         143. 
3.          123.         879. 

Now when I run this query:

Select  cust_id,  max(price) as max, min(price) as min. 
From table. 
Group by cust_id. 

I get.

Cust_id.      Max.        Min. 
1.           5000.       5000. 
2.            600.        487. 
3.            879.        143. 

But what I really want is not the max and min price but the price_id associated with the price.
So the results would be.

Cust_id.       Max.        Min.   
1.             556.        556.   
2.             654.        432.   
3.             123.        877.    

I am at a loss for how to do this. I think that the above query would be a sub query of some sort but that is as far as I got.  

  • 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-17T00:07:01+00:00Added an answer on May 17, 2026 at 12:07 am

    Use:

       SELECT x.cust_id,
              y.price_id AS max,
              z.price_id AS min
         FROM (SELECT t.cust_id,  
                      MAX(t.price) as max, 
                      MIN(t.price) as min
                 FROM TABLE t
             GROUP BY t.cust_id) x
    LEFT JOIN TABLE y ON y.cust_id = x.cust_id
                     AND y.price = x.max
    LEFT JOIN TABLE z ON z.cust_id = x.cust_id
                     AND z.price = x.min
    

    The problem is that if a cust_id has two records with the same high (or low) price, you’ll see duplicates and will need to provide logic to deal with ties.

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

Sidebar

Related Questions

I have the following example in a SQL table Cust Group Sales A 1
So I have the following group by SQL select count(*) as NO_OF_MSGS,FROM_USER,PROFILE_IMG,MSG from MESSAGES
I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
I have the following SQL problem. Scenario: I have two tables: Change and ChangeTicket.
I have a sql server 2005 table called ZipCode, which has all the US
I have another rather curious problem. I have the following structure: CREATE TABLE [dbo].[Event]
My today's problem is that I have a table that has rows like that:
SQL to find duplicate entries (within a group) I have a small problem and
I have a bit of a strange problem, please examine the following SQL CREATE
I have an SQL table where one of the columns has several comma separated

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.