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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:01:06+00:00 2026-05-12T01:01:06+00:00

I have a table called Stock and another called Listed, within the Stock Table

  • 0

I have a table called Stock and another called Listed, within the Stock Table is a Status Code that indicates when something is at the front of the queue of items of stock – I want to be able to find the most recently added item and set this to be the “front of queue” status.
For example to get all the items listed and then order them by the one most recently listed
I would use this query:

SELECT SKU FROM Stock
INNER JOIN Listed
ON Listed.ListingID = Stock.ListingID
WHERE Stock.StatusCode = 2
ORDER BY Listed.ListDate

However I want to find all the items in my Stock table which need to be at the front of the queue – ie. have a StatusCode of 1 where those items have no SKU with a StatusCode of 1
e.g. I have a few items with various ProductCodes in the Stock table but can have StatusCodes of 1s and 2s – where the 1 indicates the first item in the queue, and 2 indicates the rest of the items with the same ProductCode.

How do I write my query to set all those items which need a StatusCode of 1 where anything with a given ProductCode has nothing with a status code of 1?
I want to set the most recently added Stock item listed with a Status Code of 1 as I have to reset them all to 2 as part of a maintainence process and need to restore the “front-of-queue” item.

Most Recently Added: ListDate
StatusCode: 1 (Front of Queue), 2 (Other Items in Stock of same Product Code)


Here is some sample Data

Stock Table

SKU    ProductCode    StatusCode  
1      111111         1  
2      111111         2  
3      222222         1  
4      222222         2  
5      333333         2  
6      333333         2  

Listed Table

ListID    SKU    ListDate
01        1      01/01/2009
02        2      02/01/2009
03        3      03/01/2009
04        4      04/01/2009
05        5      05/01/2009
06        6      06/01/2009

In the Stock Table SKU 6 with the ProductCode 333333 has two items with the same StatusCode, I want to set the one with the most recent ListDate from the Listed Table
to StatusCode 1. This would apply to all other cases of this where I need the most
recently added item to have this StatusCode

  • 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-12T01:01:07+00:00Added an answer on May 12, 2026 at 1:01 am
    UPDATE S1
    SET S1.StatusCode = 1
    FROM Stock S1
    LEFT JOIN Stock S2
      ON (S1.ProductCode = S2.ProductCode
          AND S2.StatusCode = 1)
    JOIN Listed L1
      ON (S1.SKU = L1.SKU)
    WHERE S2.StatusCode IS NULL
     AND L1.ListDate =
     ( SELECT MIN(L2.ListDate)
         FROM Listed L2
        WHERE L1.SKU = L2.SKU )
    

    Sometimes you say you want to “find” such items (that I guess would be a SELECT) and sometimes you say you want to “set” their status code — I’ve taken the latter operation because it seems a better match for the problem you describe, whence the UPDATE.

    Also, it’s not clear what you want to do when multiple otherwise-satisfactory items have identical dates and thus it’s impossible to uniquely define the latest one; maybe other consraints in your situation make that impossible? Here I’m setting all of their status codes, of course it would also be possible to set none of them or a somewhat arbitrarily chosen one (by ordering on some other criteria?).

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

Sidebar

Ask A Question

Stats

  • Questions 131k
  • Answers 131k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, You need to put the script in the tags,… May 12, 2026 at 6:14 am
  • Editorial Team
    Editorial Team added an answer One of the great benefits of PDO is that you… May 12, 2026 at 6:14 am
  • Editorial Team
    Editorial Team added an answer Using properties is generally good practice as the synthesized setters… May 12, 2026 at 6:14 am

Related Questions

I have an importer process which is running as a windows service (debug mode
I've just been reading Chad Fowler's blog post about 20 Rails Development No-Nos .
I have a problem where a method is getting an undefined variable error, even
I am writing a UDF for Excel 2007 which I want to pass a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.