Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Confused with joins
Message
 
To
11/07/2007 12:43:34
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01239367
Message ID:
01239375
Views:
8
>Here is a query built with the query designer
>
>SELECT Sites_1.permid, Sites_1.site_name, Sites_1.status,;
>  Sites_1.startdate, History_1.permid, History_1._year, History_1.xyear,;
>  History_1.xbaseyear, History_1.xsiteid, History_1.surveys_distributed,;
>  History_1.validsurveys, History_1.responserate, History_1.afv,;
> FROM ;
>     NEWBLUEBOOK!SITES Sites_1 ;
>    LEFT OUTER JOIN NEWSURVEYS!HISTORY History_1 ;
>   ON  Sites_1.permid = History_1.permid;
> WHERE  Sites_1.status = ( "TRP" );
>   AND  History_1._year = ( 2005.0000 );
> ORDER BY Sites_1.startdate
>
>It produces 91 records with a startdate in 1989 (and others) which is what we would expect. If I change the _year in the where clause to 2007, it produces 51 records with a startdate in 1989 which is not what I want. With the left join I would expect to still get all of the records where sites.status = "TRP", in this case 91 records, and the history fields as NULL if there is no match. But it looks like the where clause is filtering.
>
>How can I ALWAYS get ALL of the status="TRP" sites matched to just 2007 history data, leaving the history data NULL if there is no match?
>
>Thanks

This is not JOIN issue, it is a WHERE one :-) When you have no matching record in History table Year is NULL what is NOT equal to 200x year, it is not different too :-) so try:
SELECT Sites_1.permid, Sites_1.site_name, Sites_1.status,;
      Sites_1.startdate, History_1.permid, History_1._year, History_1.xyear,;
      History_1.xbaseyear, History_1.xsiteid, History_1.surveys_distributed,;
      History_1.validsurveys, History_1.responserate, History_1.afv,;
FROM  NEWBLUEBOOK!SITES Sites_1 ;
      LEFT JOIN NEWSURVEYS!HISTORY History_1
           ON  Sites_1.permid = History_1.permid AND History_1._year = 2005;
WHERE  Sites_1.status = ( "TRP" );
ORDER BY Sites_1.startdate
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform