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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:04:04+00:00 2026-05-27T05:04:04+00:00

I’ve been struggling to work this out all afternoon – it seems pretty simple

  • 0

I’ve been struggling to work this out all afternoon – it seems pretty simple but I must be missing something!

I’ve got a query which returns some data, two of the columns it returns are “PackageWeight” and “PackageGroup”. Essentially, I want to filter this data down to show only one row for each “PackageGroup” – that should be the row with the highest value in the “PackageWeight” column.

It seems simple but I just can’t get it to work in SQL Server using a combination of TOP 1 and GROUP BY. I must be missing something!

    SELECT VendorID, PackageID, PackageWeight, PackageGroup
  FROM (SELECT VendorID, COUNT(*) AS qty
          FROM VendorServices
         GROUP BY VendorID
       ) cs
  JOIN (SELECT PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup, COUNT(*) AS qty
          FROM PackageServices
          JOIN lookupPackages ON PackageServices.PackageID = lookupPackages.PackageID
          GROUP BY PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup
       ) ps ON cs.qty >= ps.qty
  WHERE (SELECT COUNT(*)
          FROM VendorServices cs2
          JOIN PackageServices ps2 ON cs2.ServiceTypeID = ps2.ServiceID
         WHERE cs2.VendorID = cs.VendorID
           AND ps2.PackageID = ps.PackageID
       ) = ps.qty

This query returns me the complete dataset, that I need to filter down. However my attempts so far have failed 🙁

Any help much appreciated!

EDIT – Thanks to contributors below, so far I have the following query:

with result_cte as
(
SELECT VendorID, PackageID, PackageWeight, PackageGroup,
    RANK() over (partition by PackageGroup order by PackageWeight desc) as [rank]
FROM (SELECT VendorID, COUNT(*) AS qty
    FROM VendorServices
    GROUP BY VendorID
    ) cs
JOIN (SELECT PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup, COUNT(*) AS qty
    FROM PackageServices
    JOIN lookupPackages ON PackageServices.PackageID = lookupPackages.PackageID
    GROUP BY PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup
    ) ps ON cs.qty >= ps.qty
WHERE (SELECT COUNT(*)
    FROM VendorServices cs2
    JOIN PackageServices ps2 ON cs2.ServiceTypeID = ps2.ServiceID
    WHERE cs2.VendorID = cs.VendorID
    AND ps2.PackageID = ps.PackageID
    ) = ps.qty
)

select *
from result_cte
WHERE [rank] = 1
ORDER BY VendorID

So far, so good. I’ll still take a look at the APPLY operator suggested by @gbn as this is new to me – and I still need to do some testing to ensure that this query works 100% of the time. However initial indications are good!

Thanks to all who have contributed so far.

EDIT 2 – Sadly, after populating the database with more example data, this query failed to work. It seems to miss out some entries.

Perhaps I need to explain a little more about what is going on here. The data being returned by my original query lists every customer in the system, along with the derived PackageID (calculated by that query) and the weight and group assigned to that Package in a lookup table.

I need to filter the original results table so that I get no more than one package from each group, for each customer (each customer may have a package from one or more group but might not have a package from every group)

I’ll take a fresher look at this tomorrow as I think I might be in a ‘Can’t see the wood for the trees’ situation!

Thanks all.

  • 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-27T05:04:05+00:00Added an answer on May 27, 2026 at 5:04 am

    Can you try this? It’s not bulletproof if you have multiple records with the same weight in the same group. There are other ways to handle it.

    with result_cte as
    (
    SELECT VendorID, PackageID, PackageWeight, PackageGroup
    FROM (SELECT VendorID, COUNT(*) AS qty
        FROM VendorServices
        GROUP BY VendorID
        ) cs
    JOIN (SELECT PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup, COUNT(*) AS qty
        FROM PackageServices
        JOIN lookupPackages ON PackageServices.PackageID = lookupPackages.PackageID
        GROUP BY PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup
        ) ps ON cs.qty >= ps.qty
    WHERE (SELECT COUNT(*)
        FROM VendorServices cs2
        JOIN PackageServices ps2 ON cs2.ServiceTypeID = ps2.ServiceID
        WHERE cs2.VendorID = cs.VendorID
        AND ps2.PackageID = ps.PackageID
        ) = ps.qty
    )
    
    select *
    from result_cte
    where result_cte.PackageWeight = (select top 1 highestweight.PackageWeight from result_cte highestweight
                                    where highestweight.PackageGroup = result_cte.PackageGroup
                                    order by highestweight.PackageWeight desc)
    

    Or you can do this:

    with result_cte as
    (
    SELECT VendorID, PackageID, PackageWeight, PackageGroup,
        ROW_NUMBER() over (partition by PackageGroup order by PackageWeight desc) as [row]
    FROM (SELECT VendorID, COUNT(*) AS qty
        FROM VendorServices
        GROUP BY VendorID
        ) cs
    JOIN (SELECT PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup, COUNT(*) AS qty
        FROM PackageServices
        JOIN lookupPackages ON PackageServices.PackageID = lookupPackages.PackageID
        GROUP BY PackageServices.PackageID, lookupPackages.PackageWeight, lookupPackages.PackageGroup
        ) ps ON cs.qty >= ps.qty
    WHERE (SELECT COUNT(*)
        FROM VendorServices cs2
        JOIN PackageServices ps2 ON cs2.ServiceTypeID = ps2.ServiceID
        WHERE cs2.VendorID = cs.VendorID
        AND ps2.PackageID = ps.PackageID
        ) = ps.qty
    )
    
    select *
    from result_cte
    where [row] = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.