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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:46:53+00:00 2026-05-30T23:46:53+00:00

I hope following sample code is self-explanatory: declare @t1 table (ID int,Price money, Name

  • 0

I hope following sample code is self-explanatory:

declare @t1 table (ID int,Price money, Name varchar(10))
declare @t2 table (ID int,Orders int,  Name varchar(10))
declare @relation  table (t1ID int,t2ID int)
insert into @t1 values(1, 200, 'AAA');
insert into @t1 values(2, 150, 'BBB');
insert into @t1 values(3, 100, 'CCC');
insert into @t2 values(1,25,'aaa');
insert into @t2 values(2,35,'bbb');
insert into @relation values(1,1);
insert into @relation values(2,1);
insert into @relation values(3,2);

select T2.ID AS T2ID
,T2.Name as T2Name
,T2.Orders
,T1.ID AS T1ID
,T1.Name As T1Name
,T1Sum.Price
FROM @t2 T2
INNER JOIN (
    SELECT Rel.t2ID
        ,MAX(Rel.t1ID)AS t1ID 
-- the MAX returns an arbitrary ID, what i need is: 
--      ,ROW_NUMBER()OVER(Partition By Rel.t2ID Order By Price DESC)As PriceList
        ,SUM(Price)AS Price
        FROM @t1 T1 
        INNER JOIN @relation Rel ON Rel.t1ID=T1.ID
        GROUP BY Rel.t2ID
)AS T1Sum ON  T1Sum.t2ID = T2.ID
INNER JOIN @t1 T1 ON T1Sum.t1ID=T1.ID

Result:

T2ID   T2Name   Orders  T1ID    T1Name  Price     
 1      aaa       25     2       BBB    350,00     
 2      bbb       35     3       CCC    100,00

What i need is commented above, a way to get the ROW_NUMBER but also to Group By in the first place. So i need the sum of all T1-prices grouped by T2.ID in the relation-table and in the outer query the t1ID with the highest price.

In other words: How to change MAX(Rel.t1ID)AS t1ID to somewhat returning the ID with the highest price?

So the desired result is(notice that first T1ID changed from 2 to 1 since it has the higher price):

T2ID   T2Name   Orders  T1ID    T1Name  Price     
 1      aaa       25     1       AAA    350,00     
 2      bbb       35     3       CCC    100,00

Note: in case you’re wondering why i don’t multiply Orders with Price: they are not related(so i should have left off this column since it’s a bit ambiguous, please ignore it, i’ve just added it to make all less abstract). Actually Orders must remain unchanged, that’s the reason for the sub-query approach to join both and the reason why i need to group by in the first place.

Conclusion: obviously the core of my question can be answered by the OVER clause that can be applied to any aggregate function like SUM(see Damien’s answer) what was new to me. Thank you all for your working approaches.

  • 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-30T23:46:54+00:00Added an answer on May 30, 2026 at 11:46 pm

    Wow, the other answers look complex – so I’m hoping I’ve not missed something obvious.

    You can use OVER/PARTITION BY against aggregates, and they’ll then do grouping/aggregating without a GROUP BY clause. So I just modified your query to:

    select T2.ID AS T2ID
        ,T2.Name as T2Name
        ,T2.Orders
        ,T1.ID AS T1ID
        ,T1.Name As T1Name
        ,T1Sum.Price
    FROM @t2 T2
    INNER JOIN (
        SELECT Rel.t2ID
            ,Rel.t1ID
     --       ,MAX(Rel.t1ID)AS t1ID 
    -- the MAX returns an arbitrary ID, what i need is: 
          ,ROW_NUMBER()OVER(Partition By Rel.t2ID Order By Price DESC)As PriceList
            ,SUM(Price)OVER(PARTITION BY Rel.t2ID) AS Price
            FROM @t1 T1 
            INNER JOIN @relation Rel ON Rel.t1ID=T1.ID
    --        GROUP BY Rel.t2ID
    )AS T1Sum ON  T1Sum.t2ID = T2.ID
    INNER JOIN @t1 T1 ON T1Sum.t1ID=T1.ID
    where t1Sum.PriceList = 1
    

    Which gives the requested result set.

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

Sidebar

Related Questions

How can I do this? (The following code does NOT work, but I hope
I've the following code in my app. MyEventHandler handler = null; //Declare the handler
I have the following code var aVar = sample text; alert(aVar); $(this).replaceWith( <input type='text'
I have the following XAML code as part of a custom control: <telerik:RadTreeView x:Name=treeModules>
i hope this is a simple question! in the following code all is well
I just hope the following doesn't seem to you like redundant jabber :) Anyway,
i'm trying to do the following hope there's a reg_ex expert around to shed
I hope the title and following text are clear, I'm not very familiar with
I have the following problem and hope someone could help. I have a SQL
I have the following code; var looptijd_1 = de_snelheid * de_afstand; var split_looptijden_1 =

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.