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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:33:18+00:00 2026-05-13T07:33:18+00:00

I need to get maximum and minimum values but also I need to get

  • 0

I need to get maximum and minimum values but also I need to get row id of these maximum or minimum on the same row.

SELECT MIN([Value]), MAX([Value]), id 
FROM [AnalystEstimates].[dbo].[AnalystEstimateValues] 
GROUP BY indicatorid
  • 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-13T07:33:18+00:00Added an answer on May 13, 2026 at 7:33 am

    It’s very unclear what you want from your question. Do you really want the GROUP BY indicatorid? If not then it’s quite simple and you already have many answers. But if you do want to GROUP BY then it’s more difficult and no-one has got it quite right yet. I also assume that you only want one row per indicatorid, and if there are duplicate rows that have the same max/min then it’s better to just choose one of them arbitrarily instead of returning both.

    Here’s my attempt, using CTEs (requires SQL Server 2005 or newer):

    WITH
        RowNumbers AS (
            SELECT ROW_NUMBER() OVER (ORDER BY indicatorid, value) AS RowNumber, *
            FROM [AnalystEstimates].[dbo].[AnalystEstimateValues]),
        MinRowNumbers AS (
            SELECT indicatorid, MIN(RowNumber) AS RowNumber FROM RowNumbers GROUP BY indicatorid),
        MaxRowNumbers AS (
            SELECT indicatorid, MAX(RowNumber) AS RowNumber FROM RowNumbers GROUP BY indicatorid)
    SELECT
        MinRowNumbers.indicatorid,
        RN1.Value AS MinValue,
        RN1.ID AS MinValueId,
        RN2.Value AS MaxValue,
        RN2.ID AS MaxValueId
    FROM MinRowNumbers
    JOIN MaxRowNumbers ON MinRowNumbers.indicatorid = MaxRowNumbers.indicatorid
    JOIN RowNumbers RN1 ON MinRowNumbers.RowNumber = RN1.RowNumber
    JOIN RowNumbers RN2 ON MaxRowNumbers.RowNumber = RN2.RowNumber
    

    Here is some data I used to test it:

    CREATE TABLE AnalystEstimateValues (ID int, indicatorid int, Value int);
    
    INSERT INTO AnalystEstimateValues (ID, indicatorid , Value) VALUES
    (1, 1, 4),
    (2, 1, 4),
    (3, 2, 6),
    (4, 1, 2),
    (5, 2, 2),
    (6, 2, 5),
    (7, 3, 0);
    

    And here’s the output I get:

    indicatorid MinValue MinValueId MaxValue MaxValueId
              1        2          4        4          2
              2        2          5        6          3
              3        0          7        0          7
    

    If this isn’t what you want, can you please try to improve your question to tell us what you do want?


    Update: Here’s an alternative solution based on Craig Young’s answer but using joins instead of subselects:

    WITH
        UniqueIds AS (
            SELECT IndicatorId, Value, MIN(id) AS Id
            FROM AnalystEstimateValues
            GROUP BY IndicatorId, Value)
    SELECT
        lims.IndicatorId,
        MinValue,
        T1.Id AS MinValueId,
        MaxValue,
        T2.Id AS MaxValueId 
    FROM (
            SELECT
                IndicatorId,
                MIN(Value) as MinValue,
                MAX(Value) as MaxValue
            FROM AnalystEstimateValues
            GROUP BY IndicatorId) lims
    JOIN UniqueIds T1 ON lims.IndicatorId = T1.IndicatorId AND lims.MinValue = T1.Value
    JOIN UniqueIds T2 ON lims.IndicatorId = T2.IndicatorId AND lims.MaxValue = T2.Value
    

    This is cleaner and probably also faster than my first version, although I haven’t run performance tests to verify this.

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

Sidebar

Ask A Question

Stats

  • Questions 296k
  • Answers 296k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As CommonsWare said, your emulator is missing the library. The… May 13, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer You only import things you need, and @class is doing… May 13, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer You can do that like this: <?php echo '<span style="font-size:… May 13, 2026 at 7:02 pm

Related Questions

I want to store a list of numbers (essentially, a set in mathematical terms)
Say I have a class public class TimestampedTrackId { private readonly int trackId; private
I need to write a gauge control in WPF for a project at work.
I load an image and add it to the MC someMC. If something is
I have a setter method (setMinimumNumberOfSides) that I want to override after using synthesize.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.