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

  • Home
  • SEARCH
  • 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 6073371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:15:49+00:00 2026-05-23T10:15:49+00:00

Sometimes finding the best way to communicate a ‘problem’ is as difficult as finding

  • 0

Sometimes finding the best way to communicate a ‘problem’ is as difficult as finding the solution… LOL but here goes…

I have a table with companies, I have a related table with some history for each company…
I want to return a query with a record set of the last 3 ‘history’ entries per company…

so the recordset will look something like this…

company A
  history Az
  history Ay
  history Ax
company B
  history Bz
  history By
  history Bx
company C
  history Cz
  history Cy
  history Cx

The issue I’m running into is if I LIMIT 3 – with the joins being in there I JUST get the LAST 3 records of all the records, not the last 3 for EACH company…

Here’s what I have last tried – but it’s just pulling back ONE company, 3 records.. and that’s it…(IBM DB2 9 – but the only thing that should affect is the syntax on the limit of 3..)

SELECT 
   C.CompanyName   
  ,H.*

FROM Companies C
JOIN CompanyTypes CT ON C.fkTypeID = CT.ID

INNER JOIN (
    SELECT   sCH.*
    FROM     CompanyHistory sCH
    ORDER BY sCH.DATE DESC 
    FETCH FIRST 3 ROWS ONLY
) H ON H.fkCompanyID = C.ID 


WHERE CT.Type = 'bookstore'

What am I doing wrong?

  • 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-23T10:15:49+00:00Added an answer on May 23, 2026 at 10:15 am
    SELECT C.CompanyName,H.*
    FROM Companies C
    JOIN CompanyTypes CT ON C.fkTypeID = CT.ID
    INNER JOIN (
        SELECT   sCH.*
        FROM     CompanyHistory sCH    
        ORDER BY sCH.DATE DESC                   -- order desc so we can count
    ) H ON H.fkCompanyID = C.ID 
    WHERE CT.Type = 'bookstore'
      and 3>(select count(*)                     -- at most 2 previous occurances
           from CompanyHistory ich
           where ich.fkCompanyID=C.ID            -- same company
              and datediff(d,ich.date,H.date)<0) -- but dates less than the row's date
    

    Basically I’m counting the previous rows for each company, and dropping rows once we get past 2 (+the current row makes 3). You need to sort the CompanyHistory table by date desc for this to work.

    You may need to replace datediff with the function for your SQL flavor, I only really know SQL Server.

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

Sidebar

Related Questions

I have recently started using coffeescript with Rails and I am finding that sometimes
I know that similar questions have been asked all over the place, but I'm
I am using a std::map. Sometimes I will do an operation like: finding the
Sometimes I have come across this issue that whenever I change the order of
I've been using the Silverlight Toolkit but I'm finding the quality lacking; in particular
Finding lots of mixed answers on this from Google - but mostly 2-3 years
I am having a real hard time finding a way to start, stop, and
I'm finding that sometimes debug_backtrace() is not including the line number for a call.
Has anyone else found VIM's syntax highlighting of Javascript sub-optimal? I'm finding that sometimes
Sometimes when I make coding mistakes I spend a lot of time finding the

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.