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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:44:25+00:00 2026-06-18T07:44:25+00:00

I recently posted a question about a SQL Where Statement/Grouping here: SQL statement using

  • 0

I recently posted a question about a SQL Where Statement/Grouping here:

SQL statement using WHERE from a GROUP or RANK

Now I’ve got somewhat of a follow-up.

So similar to the previous question, let’s assume I have a table of say 35,000 rows with these columns:

Sales Rep | Parent Account ID| Account ID | Total Contract Value | Date

Each row is individual by account id but multiple account IDs can fall under a parent account ID.

Similar to the responses on the first question, this is probably going to be a table w/i a table. So first, everything has to be grouped by Sales Rep. From that, everything needs to be grouped by Parent Account ID where the grouped total contract value of all the accounts is >= 10,000. Then everything will be displayed and ranked by the total TCV of the Parent account ID and I need the top 35 Parent account IDs by agent.

So the first couple of lines of data may look like this:

Sales Rep | Parent Account ID| Account ID | Total Contract Value | Date      | Rank
John Doe  | ParentABC12345   | ABC425     | 5,000                | 1/2/2013  |1
John Doe  | ParentABC12345   | ABC426     | 10,000               | 1/2/2013  |1
John Doe  | ParentDJE12345   | DJE523     | 11,000               | 1/2/2013  |2
John Doe  | ParentFBC12345   | FBC6723    | 4,000                | 1/2/2013  |3
John Doe  | ParentFBC12345   | FBC6727    | 4,000                | 1/2/2013  |3

Notice how the ranking works based off of the parent Account ID. The account ID DJE523 has the single greatest TCV but it’s ranked second b/c the grouped value of parent account ID ParentABC12345 is greater. So there would be a ranking of 35 parent account IDs but in that ranking their could be say 100+ lines of actual data.

Any thoughts?

  • 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-18T07:44:26+00:00Added an answer on June 18, 2026 at 7:44 am

    Always nice to follow up. The “parent rank” is added as an INNER JOIN.

    Edit: As correctly mentioned by Dan Bracuk, my first answer was not correct. I altered the query to meet the correct conditions. I also applied the timespan to the Parent Account’s.

    DECLARE @minimumValue decimal(20,2) = 10000
    DECLARE @numberOfAccounts int = 35
    DECLARE @from datetime = '1/1/2013'
    DECLARE @till datetime = DATEADD(MONTH, 1, @from)
    
    SELECT 
      [sub].[Sales Rep],
      [sub].[Rank],
      [sub].[Account ID],
      [sub].[Total Contract Value],
      [sub].[Parent Account ID],
      [sub].[Total],
      [sub].[ParentRank]
    FROM
    (
      SELECT
        [s].[Sales Rep],
        [s].[Account ID],
        [s].[Total Contract Value],
        DENSE_RANK() OVER (PARTITION BY [s].[Sales Rep] ORDER BY [s].[Total Contract Value] DESC) AS [Rank],
        [p].[Parent Account ID],
        [p].[Total],
        [p].[ParentRank]
      FROM [Sales] [s]
      INNER JOIN 
      (
        SELECT
          [Parent Account ID],
          SUM([Total Contract Value]) AS [Total],
          RANK() OVER(ORDER BY SUM([Total Contract Value]) DESC) AS [ParentRank]
        FROM [Sales]
        WHERE[Date] > @from AND [Date] < @till
        GROUP BY [Parent Account ID]
        HAVING SUM([Total Contract Value]) > @minimumValue
      ) AS [p] ON [s].[Parent Account ID] = [p].[Parent Account ID]
      WHERE [Date] > @from AND [Date] < @till
    ) AS [sub]
    WHERE [sub].[Rank] <= @numberOfAccounts
    ORDER BY 
      [Sales Rep] ASC,
      [ParentRank] ASC,
      [Rank] ASC
    

    And here is a new Fiddle.

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

Sidebar

Related Questions

I recently posted this question about summing arrays in JavaScript using d3.nest() I got
I recently posted a question here; however, in the great answers that I got,
I recently posted a question about reading Word files here . The app runs
Recently I posted a question about the html helper dropdownlist and got it working
I recently posted a question regarding moving multiple images and got some advice from
(This question is similar to the one I recently posted here except that now
I recently posted a question here about some memory issues I was having. I've
I recently posted a question about screen rotation in my live wallpaper. To test
I recently posted a question about centering a page with CSS. I figured out
I recently posted a question about getting a NullPointerException whenever I called an array

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.