In SQL Server 2012, I have two tables, one called carthead
and the other called cartrows
. Each table is probably 10k rows. I have identity columns in both tables, idorder
is an identity column in carthead
and idrow
is an identity column in cartrows
(though I don't seem to need to use this, I think, but I'm including it here so you have the background).
I'm trying to find the newest (or last-entered) idorder
number (again, an identity column) where a product # = 97 and imgexists is True (it's a flag that's set if we have an image of it).
This query runs:
SELECT TOP 1 cr.idorder
FROM cartrows cr, carthead ch
WHERE cr.idproduct = 97
AND ch.imgexists IS NOT NULL
ORDER BY cr.idorder DESC
However, the answer spits out a number for the latest row where idproduct = 97 but not taking into consideration ch.imgexists is not null (I've tried other variations, including <> '', is not False, etc with the same result).
I began my search finding this thread: ( How to read the last row with SQL Server ) and just thinking I could add conditions to suit my needs, but apparently I'm doing it wrong..
Any thoughts? Kind thanks to all from Austin, Texas..