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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:53:20+00:00 2026-05-21T03:53:20+00:00

Here is my scenario: I have a single table with 2 columns. ID and

  • 0

Here is my scenario:

I have a single table with 2 columns. ID and Value. ID is int and value is real.

ID    Value
1     6.7
2     8.9
3     4.5
5     3.2
8     2.5
9     2.1
10    1.0
15    2.3
18    2.4
19    4.0
20    3.2

I would like to compose a SP that receives a grouping number (Group) and an operation (Op) and returns a new table in the following manner:

Group = 2,
Op = Max

IDstart   IDend    Value
1         2        8.9
3         5        4.5
8         9        2.5
10        15       2.3
18        19       4.0 
20        20       3.2

Group = 3,
Op = Min

IDstart   IDend    Value
1         3        4.5
5         9        2.1
10        18       1.0
19        20       3.2

Group defines how many rows to combine into a single row (in the new table) and operation defines what operation to do on the group of rows, the operations I need are maximum, minimum and average.
The last group may contain less rows than all the rest of the groups. if the last group has a single value IDstart = IDEnd. ID is unique but may have ‘gaps’.

I’m looking for the fastest way to do this, any help will be appreciated.

Using SQL Server 2008 R2

Gilad.

  • 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-21T03:53:21+00:00Added an answer on May 21, 2026 at 3:53 am

    Reasoning goes like this

    • Using the ROW_NUMBER() function and some arithmetic allows you to create a dummy column placing each ID in a group of the size you specify.
    • the result of this statement can be grouped and the operator you specify can be applied using a CASE statement. Should you need additional operators, you would only have to expand this CASE statement.

    Script

    DECLARE @Group INTEGER
    DECLARE @Op VARCHAR(3)
    
    SET @Group = 3
    SET @Op = 'MIN'
    
    ;WITH q(ID, Value) AS (
      SELECT 1,     6.7
      UNION ALL SELECT 2,     8.9
      UNION ALL SELECT 3,     4.5
      UNION ALL SELECT 5,     3.2
      UNION ALL SELECT 8,     2.5
      UNION ALL SELECT 9,     2.1
      UNION ALL SELECT 10,    1.0
      UNION ALL SELECT 15,    2.3
      UNION ALL SELECT 18,    2.4
      UNION ALL SELECT 19,    4.0
      UNION ALL SELECT 20,    3.2
    )
    SELECT  [IDStart] = MIN(ID)
            , [IDEnd] = MAX(ID)
            , [Value] = CASE  WHEN @Op = 'MAX' THEN MAX(Value)
                              WHEN @Op = 'MIN' THEN MIN(Value)
                              WHEN @Op = 'AVG' THEN AVG(Value)
                        END
    FROM    (
              SELECT ID
                     , Value
                     , GroupRow = (ROW_NUMBER() OVER (ORDER BY ID) - 1) / @Group
              FROM    q
            ) q          
    GROUP BY
            GroupRow
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my scenario - I have an SSIS job that depends on another prior
Here's the scenario: You have a Windows server that users remotely connect to via
Here's the scenario: I have a set of buttons that I want to bind
Here's my scenario: I have a simple stored procedure that removes a specific set
Here's the scenario: I have a textbox and a button on a web page.
Here's the scenario: You have an ASP.Net application supported by a Microsoft SQL Server
Here's the scenario I have a Grid with some TextBlock controls, each in a
Here's my scenario: I have a page containing several links; each link is meant
Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx
Here's the scenario: We have users login on a secure connection. Could an unathorized

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.