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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:10:36+00:00 2026-06-01T22:10:36+00:00

I have a weird requirement which I need to use inside my Stored Procedure

  • 0

I have a weird requirement which I need to use inside my Stored Procedure in SQL Server 2008 R2.

I need a FIRST aggregate function which returns the first element of a sequence and I will use that with HAVING clause.

Let me give you an example:

DECLARE @fooTable AS TABLE(
    ID INT,
    CategoryName NVARCHAR(100),
    Name NVARCHAR(100),
    MinAllow INT,
    Price DECIMAL(18,2)
);

INSERT INTO @fooTable  VALUES(1, 'Cat1', 'Product1', 2, 112.2);
INSERT INTO @fooTable  VALUES(2, 'Cat2', 'Product2', 4, 12.34);
INSERT INTO @fooTable  VALUES(3, 'Cat1', 'Product3', 5, 233.32);
INSERT INTO @fooTable  VALUES(4, 'Cat3', 'Product4', 4, 12.43);
INSERT INTO @fooTable  VALUES(5, 'Cat3', 'Product5', 1, 13.00);

DECLARE @minAllowParam AS INT = 3;

SELECT ft.CategoryName, SUM(ft.Price) FROM @fooTable ft
GROUP BY ft.CategoryName;

As you see, we have a table and some dummy values. Inside the SELECT query, we group the categories together and sum the price of the products up.

This query returns the following result:

CategoryName     TotalPrice
---------------- ----------------
Cat1              345.52
Cat2              12.34
Cat3              25.43

What I need here is something like this:

SELECT ft.CategoryName, SUM(ft.Price) FROM @fooTable ft
GROUP BY ft.CategoryName
HAVING GetFIRST(MinAllow) >= @minAllowParam;

In that our case with a query something like this, we should be able to select following results:

INSERT INTO @fooTable  VALUES(2, 'Cat2', 'Product2', 4, 12.34);
INSERT INTO @fooTable  VALUES(4, 'Cat3', 'Product4', 4, 12.43);
INSERT INTO @fooTable  VALUES(5, 'Cat3', 'Product5', 1, 13.00);

As the INSERT INTO @fooTable VALUES(1, 'Cat1', 'Product1', 2, 112.2); record is the first element of a sequence and has the value of 2 for MinAllow column, Cat1 should be out of scope here. On the other hand, INSERT INTO @fooTable VALUES(5, 'Cat3', 'Product5', 1, 13.00); record has the value of 1 for MinAllow column but is the second element of the sequence. So, Cat3 is safe and can be selected.

Note: MIN or MAX is not what I am looking for!

I know that this example logically does not make sense but I have a situation which it totally does and hard to explain here on the other hand.

Any thoughts?

Edit:

I am assuming that I can achieve what I want here by creating a CLR
User-Defined Aggregate Function but I do not want to do this if there
is any other choice

  • 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-01T22:10:38+00:00Added an answer on June 1, 2026 at 10:10 pm

    How about

    SELECT f1.CategoryName, SUM(f1.Price) 
    FROM @fooTable AS f1
    INNER JOIN (
        SELECT MinAllow, CategoryName
        FROM (
             SELECT MinAllow, CategoryName, ROW_NUMBER() OVER (PARTITION BY CategoryName ORDER BY ID) AS m
             FROM @fooTable
        ) AS f
        WHERE m = 1
    ) AS f2 ON f1.CategoryName = f2.CategoryName
    WHERE f2.MinAllow >= @minAllowParam
    GROUP BY f1.CategoryName
    

    I know not a very elegant query. Maybe I can tweak it a little if I work on it a little longer!

    Edit: Ok the inner most subquery should be unnecessary. This should also work:

    SELECT f1.CategoryName, SUM(f1.Price) 
    FROM @fooTable AS f1
    INNER JOIN (
        SELECT MinAllow, CategoryName, ROW_NUMBER() OVER (PARTITION BY CategoryName ORDER BY ID) AS m
        FROM @fooTable
    ) AS f2 ON f1.CategoryName = f2.CategoryName
    WHERE f2.m = 1 AND f2.MinAllow >= @minAllowParam
    GROUP BY f1.CategoryName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one weird requirement. In a SQL table, I have two columns of
I have a somewhat weird requirement to be able to listen to a number
I have a kind of funny and weird requirement this time. I have my
I am working on a requirement where I need to have a TableView with
I have a very weird problem with core text, which sometimes randomly and sometimes
I have a weird requirement for setting up a password rule.. :| My requirement
This is a weird requirement I have. I know even my question is quite
have a weird situation. I'm using Glassfish server for my Enterprise application. In that
I have weird problem with this code $('img').hover(function(){ var $cap = $(this).parent().find('.cap'); window.setTimeout(function(){$cap.stop(true,false).animate({bottom:164},500)},500); },function(){
I have the following weird requirement. I am given: A list of some method

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.