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

Related Questions

I need to find maximum and minimum of 8 float values I get. I
I need to get the length of a .wav file. Using: sox output.wav -n
Hey guys, Im trying to get the maximum number of days in a database
I am working with mysql. I need queries that fetch a list of maximum
I am concerned about buffer overflows, and I need to get some characters out
Imagine such situation. You get some legacy code or get some new framework. You
I have a datagrid in Flex 4 with a NumericStepper as editor for a
This much I have already cobbled together (thanks to Stack Overflowers): I have a
I'm currently coding a game that makes use of Google Maps' Static API -
I'm trying to build some sort of webservice on google apps. Now the problem

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.