If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > beginner question!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-21-09, 10:16
wrehn wrehn is offline
Registered User
 
Join Date: Oct 2009
Posts: 2
beginner question!

Hi! I need help with a query with LEFT JOIN

Kurs
kid (pk)


Kurstfillf
ktillfid (pk)
kid (sk)
year

I'm going to show all kurstillf per kurs during 2008! All the courses should be included

SELECT kurs.kursnamn, Count(kurstillf.kid) AS number
FROM kurs LEFT JOIN kurstillf ON kurs.kid=kurstillf.kid
WHERE Year=2008
GROUP BY kurs.kursnamn;

It works but how do i also get the "null" ones in my result. It seems like the left join takes away all with number=0. I'm using ms acces.
Reply With Quote
  #2 (permalink)  
Old 10-21-09, 11:58
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Two options

1) Move the where clause to the JOIN predecate
Code:
SELECT kurs.kursnamn
     , Count(kurstillf.kid) As number
FROM   kurs
 LEFT
  JOIN kurstillf
    ON kurs.kid = kurstillf.kid
   AND kurstillf.year = 2008 
GROUP
    BY kurs.kursnamn
2) Move the WHERE clause in to a derived table
Code:
SELECT kurs.kursnamn
     , Count(kurstillf.kid) As number
FROM   kurs
 LEFT
  JOIN (
        SELECT *
        FROM   kurstillf
        WHERE  year = 2008 
       ) As kurstillf
    ON kurs.kid = kurstillf.kid
GROUP
    BY kurs.kursnamn
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-21-09, 13:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
another stellar post, george, i dunno if the punters appreciate it as much as i do but your SQL is gorgeous

keep up da good work, eh

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-21-09, 14:29
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The problem is that the WHERE clause contains a predicate "year = 2008". If a row in table KURS does not contain any matching row in KURSTFILLF, then the year column of that row will be populated with NULL. But NULL = 2008 evaluates to false in the predicate in the WHERE clause so that the row will be excluded from the result.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 10-22-09, 04:18
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by r937
another stellar post, george, i dunno if the punters appreciate it as much as i do but your SQL is gorgeous

keep up da good work, eh

Oh Rudy, you'll make me blush



__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 10-22-09, 10:29
wrehn wrehn is offline
Registered User
 
Join Date: Oct 2009
Posts: 2
thank you very much for the help guys! Very appriciated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On