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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:15:37+00:00 2026-06-16T22:15:37+00:00

I have the following query: Select MAX(Counter) as Counter, DestinationId FROM ( Select t1.Counter,

  • 0

I have the following query:

Select MAX("Counter") as "Counter", "DestinationId" FROM (          
 Select t1."Counter", t2.* From
           (SELECT "HierarchyNodeId", (SELECT "Description"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "Description", "DestinationId",
         (SELECT "DestinationTypeId"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "DestinationTypeId"
    FROM "HierarchyNode" hn
WHERE "DestinationId" IN (SELECT "DestinationId" From(SELECT COUNT ("HierarchyNodeId"), "DestinationId" FROM "HierarchyNode" GROUP BY "DestinationId" HAVING COUNT ("HierarchyNodeId") > 1))) t2, 
(select COUNT (*) "Counter", "HierarchyNodeId" From "HierarchyDetail"
Group By "HierarchyNodeId") t1
Where t1."HierarchyNodeId" = t2."HierarchyNodeId"
ORDER BY "DestinationId", "Counter" Desc
)
GROUP BY "DestinationId"

The output shows the Maximum Counter for each Destination ID.
The extra information that I need is the HierarchyId that corresponds to that Particular Row (With that specific Destination Id and Counter).

If I change it to:

Select MAX("Counter") as "Counter", "DestinationId", "HierarchyId" FROM ( ....

I get an error because I didn’t include HierarchyId in the Group By. But if I include a get the Max Counter for each combination of DestinationId – HierarchyId (not each DestinationId only).

So I tried this:

Select * From (
Select t1."Counter", t2.* From
           (SELECT "HierarchyNodeId", (SELECT "Description"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "Description", "DestinationId",
         (SELECT "DestinationTypeId"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "DestinationTypeId"
    FROM "HierarchyNode" hn
WHERE "DestinationId" IN (SELECT "DestinationId" From(SELECT COUNT ("HierarchyNodeId"), "DestinationId" FROM "HierarchyNode" GROUP BY "DestinationId" HAVING COUNT ("HierarchyNodeId") > 1))) t2, 
(select COUNT (*) "Counter", "HierarchyNodeId" From "HierarchyDetail"
Group By "HierarchyNodeId") t1
Where t1."HierarchyNodeId" = t2."HierarchyNodeId"
ORDER BY "DestinationId", "Counter" Desc
) test1
Inner Join
(
Select MAX("Counter") as "Counter", "DestinationId" FROM (          
 Select t1."Counter", t2.* From
           (SELECT "HierarchyNodeId", (SELECT "Description"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "Description", "DestinationId",
         (SELECT "DestinationTypeId"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "DestinationTypeId"
    FROM "HierarchyNode" hn
WHERE "DestinationId" IN (SELECT "DestinationId" From(SELECT COUNT ("HierarchyNodeId"), "DestinationId" FROM "HierarchyNode" GROUP BY "DestinationId" HAVING COUNT ("HierarchyNodeId") > 1))) t2, 
(select COUNT (*) "Counter", "HierarchyNodeId" From "HierarchyDetail"
Group By "HierarchyNodeId") t1
Where t1."HierarchyNodeId" = t2."HierarchyNodeId"
ORDER BY "DestinationId", "Counter" Desc
)
GROUP BY "DestinationId"
) test2
On test1."DestinationId" = test2."DestinationId"
And test1."Counter" = test2."Counter"

This one gives me 1456 records when the FIRST query gives me 1256…
But I only want 1 HierachyNodeId for each row of the first query. So it doesn’t make sense.
It should be 1256 rows.

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

    Use keep dense_rank:

    Select 
        MAX("Counter") as "Counter", 
        "DestinationId" 
        max("HierarchyId") keep (dense_rank first order by counter desc) as "HierarchyId"
    FROM ( query)
    GROUP BY "DestinationId";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query select t1.file_name, max(create_date), t1.id from dbo.translation_style_sheet AS t1 GROUP
I have the following query: SELECT m.*, COUNT(c.chapter_nr) as chapters, MAX(c.chapter_nr) as latest_chapter FROM
I have the following query: SELECT o.ClientId, o.MAX(Date), o.OrderNumber FROM dbo.tblOrders GROUP BY o.ClientId,
I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
I have the following query: SELECT o.id,o.name FROM object o WHERE ( o.description LIKE
I have the following query: SELECT MAX([LastModifiedTime]) FROM Workflow There are approximately 400M rows
I have the following query: SELECT (RAND() * (SELECT MAX(id) FROM frag.test)) Results in:
I have a table I am using the following query select max(dblVersion),sName,fCompleted,ixLastModifiedBy,dtCreatedAt,dtLastModifiedAt,fStatus from tblechecklisttemplateversion
I have the following query SELECT P.ID, MAX(ENTERDATE) as ENTERDATE, MAX(B.CostID) as CostID FROM
I have the following query: SELECT AVG(time) FROM (SELECT UNIX_TIMESTAMP(max(datelast)) - UNIX_TIMESTAMP(min(datestart)) AS time

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.