I have a problem in getting the correct data...
I have two tables, tblbuilding and tbldata.
Here's my tblbuilding :
Here's my tbldata :
my query:
My fldWeek here is "Week 3":
SELECT tblbuilding.fldBldgName
,tblbuilding.fldNetname
,MAX(tbldata.fldDuration) AS fldDuration
,tbldata.fldWeek
,tbldata.fldYear
FROM tblbuilding AS tblbuilding_1
LEFT OUTER JOIN tbldata ON tblbuilding_1.fldBldgName = tbldata.fldBldgName
CROSS JOIN tblbuilding
WHERE (tblbuilding.fldNetname = 'LRT - 2')
AND (tbldata.fldWeek = 'Week 3' OR tbldata.fldWeek IS NULL)
AND (tbldata.fldYear = '2013' OR tbldata.fldYear IS NULL)
GROUP BY tbldata.fldWeek
,tblbuilding.fldBldgName
,tblbuilding.fldNetname
,tbldata.fldYear
ORDER BY tblbuilding.fldBldgName
The result of the query above is:
Which is correct....
but when I changed the fldWeek to "Week 2"
the result is:
The correct result should be...."assuming that I have only two NOT NULL in WEEK 2"
Thanks....
I think you don't need to get the cartesian product.
Try this using LEFT JOIN
only.
SELECT a.ID,
a.fldBldgName,
b.fldDuration,
b.fldWeek,
b.fldYear
FROM tblBuilding a
LEFT JOIN tblData b
ON a.fldBldgName = b.fldBldgName
AND b.fldWeek = 'Week 3'
AND b.fldYear = '2013'
WHERE a.fldNetname = 'LRT - 2'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.