Edit: nevermind, figured it out, answer below if you’re interested.
Using Postgresql 8.4.
Here’s the format of the query I need to run (names and faces have been changed to protect my paranoia. they can be provided if necessary, but this is a direct copy of my query with simple substitution for schema, table and column names):
SELECT
t1.field,
SUM(v3.quantity) as current_qty
FROM
schema1.child_table t1
JOIN (SELECT DISTINCT t1key FROM schema2.child_transaction t2 WHERE datefield BETWEEN '2012-04-01' AND '2012-04-19') t2 USING (t1key)
LEFT JOIN schema1.child_current_status v3 ON t1.t1key = v3.t1key
JOIN schema1.parent_table t4 ON t1.t4key = t4.t4key
WHERE
t4.criteria = 763
GROUP BY
t1.field
… basically, I need the field from child_table when criteria matches my provided data in parent_table, and when there is at least one relevant transaction in child_transaction during the requested period. If there are quantities in the child_current_status view, we want those as well.
t1.field can be considered a human readable equivalent to numeric key t4.criteria.
My problem is that the query as provided fails to provide quantity data from child_current_status even when there is data there.
Edit: to clarify, the above query returns field, NULL, where it should return field, current_qty
The problem is related to the t2 subquery, because when I change that to a LEFT JOIN, it returns the relevant quantity data:
SELECT
t1.field,
SUM(v3.quantity) as current_qty
FROM
schema1.child_table t1
LEFT JOIN (SELECT DISTINCT t1key FROM schema2.child_transaction t2 WHERE datefield BETWEEN '2012-04-01' AND '2012-04-19') t2 USING (t1key)
LEFT JOIN schema1.child_current_status v3 ON t1.t1key = v3.t1key
JOIN schema1.parent_table t4 ON t1.t4key = t4.t4key
WHERE
t4.criteria = 763
GROUP BY
t1.field
Edit: this modified query returns field, current_qty, but will do so whether there are transactions in child_transaction for the requested time period or not
… all I’ve done is turn the first JOIN into a LEFT JOIN, and that query returns with the relevant quantities in child_current_status. If I change the child_current_status to an INNER JOIN, the query returns nothing. The problem with making the first JOIN a LEFT JOIN is that I need it to only return results when there is data returned from the sub query, which there is in this case for the requested time period.
To muddy the waters, it works properly for some t4.criteria, 763 is just one that it fails on.
What am I missing here?
Short version: From the first query above, when the first JOIN is an INNER JOIN, it causes the LEFT JOIN to return nothing. When it’s a LEFT JOIN, the LEFT JOIN on child_current_status returns the proper data, but that negates the purpose of the first JOIN.
Edit: I’ve tried moving the LEFT JOIN to the end of the FROM clause, behind the INNER JOINs, with no change
Ugh, nevermind, I figured it out, the query is working properly and there’s not enough data here for anyone else here to figure it out. Basically, there is one transaction for one child in the
child_table, and there are current quantities for other children in thechild_table. So the query appropriately indicates that there are no current quantities for any children for which there was a transaction during the specified time for the specified parent.The problem came up because in processing the result of this query, I went back and asked for current data relating to all children (rather than just the ones with relevant transactions, like I should have), and got information even when this query (correctly) told me there was none.
Nothing to see here, move along.