[thelist] trouble w/ JOINs in an Oracle query
Jeremy Weiss
eccentric.one at gmail.com
Wed Sep 30 10:15:43 CDT 2009
I'm baaaack, and need help with another query, please.
Before you see the query know that
1) I didn't write it, just modified it
2) I'm still working on my formatting convention a bit, sorry if
there's too many tabs.
~~~~~~~~~~~~~~~~~~~
SELECT
To_Char(CORR.WORKORDERLABOR.STARTED,'YYYY-MM') AS STARTMONTH
, Sum(CORR.WORKORDERLABOR.TOTALTIME) AS SUMOFTOTALTIME
FROM
CORR.USERS INNER JOIN
(
(
(
CORR.WORKORDERSQUAWKS INNER JOIN CORR.WORKORDERITEMS
ON CORR.WORKORDERSQUAWKS.ITEMKEY = CORR.WORKORDERITEMS.ITEMKEY
) INNER JOIN CORR.WORKORDERCUSTOMERS
ON CORR.WORKORDERITEMS.WORKORDERKEY = CORR.WORKORDERCUSTOMERS.WORKORDERKEY
) INNER JOIN CORR.WORKORDERLABOR
ON CORR.WORKORDERSQUAWKS.SQUAWKKEY = CORR.WORKORDERLABOR.SQUAWKKEY
)
ON CORR.USERS.USERKEY = CORR.WORKORDERLABOR.PERFORMEDBYKEY
WHERE
To_Char(CORR.WORKORDERLABOR.STARTED, 'YYYY-MM') >= '2009-01' AND
To_Char(CORR.WORKORDERLABOR.STARTED, 'YYYY-MM') <= '2009-09' AND
CORR.USERS.DEPARTMENTKEY = $deptkey AND
CORR.WORKORDERITEMS.COSTWORKORDERCUSTOMER LIKE '%$customername%'
GROUP BY
CORR.USERS.DEPARTMENTKEY
, To_Char(CORR.WORKORDERLABOR.STARTED, 'YYYY-MM');
ORDER BY
To_Char(CORR.WORKORDERLABOR.STARTED,'YYYY-MM') ";
~~~~~~~~~~~~~~~~~~~
The above query works great except that it only returns rows when both
columns exist, as it rightly should since it's an INNER JOIN. However,
I need it to return all columns where STARTMONTH exists even if there
aren't any matching entries CORR.WORKORDERLABOR.TOTALTIME. For
example, the current query returns something like:
2009-1|39
2009-3|42
And I'm wanting:
2009-1|39
2009-2|
2009-3|42
I think all I need to do is change a JOIN, but thus far I haven't
managed to get that to work. I've rewritten the FROM clause in quite a
few different ways, changing the order of the tables, trying various
JOIN types, etc. and still haven't been able to get the results I
need. Someone, please save my sanity. ;)
Thanks,
Jeremy
More information about the thelist
mailing list