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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:18:37+00:00 2026-05-29T07:18:37+00:00

I have a query that needs to get the first and second highest sku

  • 0

I have a query that needs to get the first and second highest sku in each members wishlist. The below query works, but it takes way too long because there’s about 9 million users and each user has about 10 wishlist items, so you can see that the query below will never finish.

SELECT MAX(CASE WHEN wl.rank = 1 THEN wl.SKU ELSE NULL END) AS [highestSku],
       MAX(CASE WHEN wl.rank = 2 THEN wl.SKU ELSE NULL END) AS [secondHighestSku],
FROM Member m
     LEFT JOIN (SELECT *
                 FROM (SELECT DENSE_RANK() OVER (PARTITION BY wl.MemberID ORDER BY wli.Price DESC) AS rank, wl.MemberID, wli.SKU
                       FROM WishListItem wli
                            INNER JOIN WishList wl ON wli.WishListID = wl.ID) T1) w ON w.MemberID = m.ID

My question is, is there a better way to get the top first and second records for each user? If not, is there a way I can optimize this query? Ideally, if I can restirct the number of tiems pulled back from the ranking query (the one with the DENSE_RANK()) that will help me out. I wanted to do something like WHERE DENDS_RANK() <= 2, but that’s not possible, and doing it outside of the brackets defeats the purpose of the soultion.

Also, this is just part of the query. I actually have even more left joins across more tables that have just as many items, and I need to get the top 1 and 2 records for each user.

And this needs to be done in one query, or as much as possible in one because I’m throwing it in a data table. I can also reduce the number of records, ie. TOP 1000, and break up the query, but I will need to be able to continue from where I left off… also, I did try TOP 1000, and after 10 minutes, I cancelled the query because I need to get all 9 million records out.

  • 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-29T07:18:38+00:00Added an answer on May 29, 2026 at 7:18 am

    I’d grab a relatively small subset of the data, stick it in a table variable, and run the query off that instead of the main (and likely very “busy”) tables:

    DECLARE @Member TABLE
    (
        ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
        -- add necessary columns to this definition.
    )
    
    INSERT INTO @Member (field1, field2...)
    SELECT field1, field2  -- etc. 
    FROM YourTables
    WHERE SomeCriteria = Whatever
    

    Make sure that the WHERE clause defines a narrower subset of data than your production tables. If performance still suffers, you could create table variables for the other tables you’re joining, then use those in the final query.

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

Sidebar

Related Questions

I have below a working query that needs to be simplified. The reason is
I have a query such that I need to get A specific dog All
Here is the issue I am having: I have a large query that needs
I have a query that does what i want joining table but i need
I am using in C# MYsql .I have query that works if I run
I'm writing an Oracle query that needs to get the most recent event based
I have a working query that retrieves the data that I need, but unfortunately
I have this mysql query using PHP and first part is working fine, but
So I have an SQL dump file that needs to be loaded using mysql_query().
I have a query that I need to execute that I do not know

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.