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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:32:38+00:00 2026-05-14T02:32:38+00:00

I need to do what to me is an advanced sort. I have this

  • 0

I need to do what to me is an advanced sort. I have this two tables:

Table: Fruit  

fruitid | received | basketid  
  1       20100310   2  
  2       20091205   3  
  3       20100220   1  
  4       20091129   2  

Table: Basket  
id | name  
1    Big Discounts  
2    Premium Fruit  
3    Standard Produce  

I’m not even sure I can plainly state how I want to sort (which is probably a big part of the reason I can’t seem to write code to do it, lol).

I do a join query and need to sort so everything is organized by basketid. The basketid that has the oldest fruit.received date comes first, then the other rows with the same basketid by date asc, then the basketid with the next earliest fruit.received date followed by the other rows with the same basketid, and so on.

So the output would look like this:

Fruitid | Received  |   Basket  
   4      20091129      Premuim Fruit  
   1      20100310      Premuim Fruit  
   2      20091205      Standard Produce  
   3      20100220      Big Discounts  

Any ideas how to accomplish this in a single execution?

  • 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-14T02:32:38+00:00Added an answer on May 14, 2026 at 2:32 am

    try this (sql server table setup code, but query should work in any database)

    DECLARE @Fruit table (fruitid int, received int, basketid int)
    INSERT @Fruit VALUES(1,       20100310,   2 )
    INSERT @Fruit VALUES(2,       20091205,   3 )
    INSERT @Fruit VALUES(3,       20100220,   1 )
    INSERT @Fruit VALUES(4,       20091129,   2 )
    
    DECLARE @Basket table (id int,basket varchar(20))
    INSERT @Basket VALUES (1,    'Big Discounts'  )
    INSERT @Basket VALUES (2,    'Premium Fruit'  )
    INSERT @Basket VALUES (3,    'Standard Produce')
    
    
    SELECT
        f.Fruitid ,f.received,b.basket  
        FROM @Fruit f
          INNER JOIN (SELECT
                          basketid, MIN(received) AS received
                          FROM @Fruit
                          GROUP BY basketid  
                     ) o ON f.basketid = o.basketid
          INNER JOIN @Basket b ON o.basketid=b.id
        ORDER BY o.received
    

    OUTPUT

    Fruitid     received    basket
    ----------- ----------- --------------------
    4           20091129    Premium Fruit
    1           20100310    Premium Fruit
    2           20091205    Standard Produce
    3           20100220    Big Discounts
    
    (4 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have combined two arrays using array_merge, and now need to sort them alphabetically.
I have a feeling this might involve some sort of advanced selectors, but I
I need some advanced CSS help here. I have a login button and a
I need to have a textbox (sort of) where the user can enter text
I need to migrate to RAD Studio and have this installed on a new
I will have to create an offline app from a received html5 file (sort
I suppose everyone runs into this problem once in a while: you have two
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need to use own imaged markers instead built-in pins. I have several questions. 1.
Currently, I have osmething like this in all of my classes: # Import logging

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.