Jump to content

SELECT only ONE row with multiple results


Go to solution Solved by bsamson,

Recommended Posts

Here's my database:

Table: sapImport
id    invoiceNo    soldOn
1     123456       11/18/2013
2     156541       11/15/2013
Table: conStatus
id    record     statusCode
1     2          999
2     2          000
3     1          213

I have a query that looks like this:

SELECT *
FROM sapImport
JOIN conStatus
ON sapImport.id = record
WHERE store = '2'

So, the row record matches the ID in sapImport. I only want to display the latest record the corresponds to the row with the highest id. I've been trying to accomplish this for a few hours, but still can't figure it out. Any help is greatly appreciated!

 

try

SELECT si.invoiceNo, si.soldOn, cs.statusCode
FROM sapImport si
INNER JOIN conStatus cs ON si.id = cs.record
INNER JOIN
    (
    SELECT record, MAX(id) as id
    FROM conStatus
    GROUP BY record
    ) mx ON cs.record = mx.record AND cs.id = mx.id
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.