Jump to content

SELECT only ONE row with multiple results


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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.