LEFT OUTER JOIN - revisited

I a previous question, I got a query that seemd to work perfect...

Now that I have tested it with a better dataset I can see that it does not :

The query is here :
SELECT TOP 1 a.docid,a.defname,a.status,a.blobdata FROM documents a LEFT JOIN web_timelock b ON a.docid = b.docid and (b.id = NULL OR (b.done=0 AND DATEDIFF (minute, b.ts, GETDATE())  > 5)) WHERE a.status = 4 AND ( a.defname = 'O_LEM' OR a.defname = 'N_LEM' OR a.defname = 'N_LEM_11' OR a.defname = 'O_LEM_11') ORDER BY a.createtime ASC;

The tables are:
dbo.documents
---------------------------------
docid :: <bigint>
status :: <int>
defname :: <varchar>
blobdata :: <binary data>

dbo.web_timelock
---------------------------------
id :: <bigint>
docid :: <bigid>
ts :: <datetime>
userid :: <int>
done :: <tinyint>

The problem is that it comes up with the same record even though web_timelock contains a record with the specific docid where done=1 (or ts is less that 5 minutes old)

Any takers ?
LVL 2
trg_dkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

momi_sabagCommented:
SELECT TOP 1 a.docid,a.defname,a.status,a.blobdata
FROM documents a
LEFT JOIN web_timelock b
ON a.docid = b.docid and (b.done=0 or DATEDIFF (minute, b.ts, GETDATE())  > 5)

WHERE a.status = 4 AND ( a.defname = 'O_LEM' OR a.defname = 'N_LEM' OR a.defname = 'N_LEM_11' OR a.defname = 'O_LEM_11')
ORDER BY a.createtime ASC;

0
trg_dkAuthor Commented:
>momi_sbaq - Hi again :-)

The query you propose does not work. It returns a row that has a related row in web_timelock where done=1.

What I need is to find a row in documents that does NOT have a corresponding row in web_timelock (OR that corresponding row has done==0 and ts older than 5 minutes)

Cheers,
Mark
0
momi_sabagCommented:
SELECT TOP 1 a.docid,a.defname,a.status,a.blobdata
FROM documents a
LEFT JOIN web_timelock b
ON a.docid = b.docid and b.done=0 and DATEDIFF (minute, b.ts, GETDATE())  > 5

WHERE a.status = 4 AND ( a.defname = 'O_LEM' OR a.defname = 'N_LEM' OR a.defname = 'N_LEM_11' OR a.defname = 'O_LEM_11')
ORDER BY a.createtime ASC;
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

trg_dkAuthor Commented:
Same result :-(
0
trg_dkAuthor Commented:
A simple version like this:
SELECT     TOP (1) a.docid, a.defname, a.status, a.blobdata
FROM         documents AS a LEFT OUTER JOIN
                      web_timelock AS b ON a.docid = b.docid AND b.id = NULL

Also fails - as I see it it should return a row from documents that DONT have a row in web_timelock ?!?!
0
momi_sabagCommented:
SELECT     TOP (1) a.docid, a.defname, a.status, a.blobdata
FROM         documents AS a LEFT OUTER JOIN
                     web_timelock AS b ON a.docid = b.docid
where b.id = NULL

0
momi_sabagCommented:
you need to understand how left outer join works and you will figure it out

left outer join works this way:
for every row from the left table (the one to the left of the join clause) :
   search for a matching row in the right table (according to the conditions in the ON clause)
     if found - add the row from the right table to the result
    else add  nulls to the result instead of the row from the right table

so filtering
where b.id = null
can't be done in the ON clause, only in the where clause
0
trg_dkAuthor Commented:
SELECT     TOP (1) a.docid, a.defname, a.status, a.blobdata
FROM         documents AS a LEFT OUTER JOIN
                     web_timelock AS b ON a.docid = b.docid
where b.id = NULL

Returns 0 records ?? (and there should be at least 20.000)
0
momi_sabagCommented:
SELECT     TOP (1) a.docid, a.defname, a.status, a.blobdata
FROM         documents AS a LEFT OUTER JOIN
                    web_timelock AS b ON a.docid = b.docid
where b.id is NULL
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trg_dkAuthor Commented:
:-O

The problem was b.id = NULL (wrong) instead on b.id IS NULL

Thanks for your help :-) (again)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.