Subject | Strange left outer join behaviour |
---|---|
Author | Balanyi Zsolt |
Post date | 2007-03-06T06:07:39Z |
Hi all!
I have two tables, T1 and T2, which are linked by an ID (integer). T2
has an ID, a TYPE field, which describes the type of the row, and a
VALUE field.
I am interested in all (or some - not important) rows of T1 and the
SUM(VALUE) of T2 that has a given TYPE and is linked to T1.
My query looks like this:
SELECT T1.ID, T1.NAME, COALESCE(SUM(VALUE), 0)
FROM T1 LEFT OUTER JOIN T2 ON T1.ID = T2.ID
WHERE
T2.TYPE = :WANTEDTYPE
GROUP BY T1.ID, T1.NAME
The strange thing is, that if there is no record from T2, then the T1
master row is omitted from the result set as well.
Example:
If T1 has only one record, and T2 is empty, then the query returns no rows.
In this case I would like to see the record from T1 too!
Any ideas?
(Stored procedures are out of question, as the SQL is generated
dynamically with some other WHERE constraints on T1)
Best regards, Zsolt Balanyi
I have two tables, T1 and T2, which are linked by an ID (integer). T2
has an ID, a TYPE field, which describes the type of the row, and a
VALUE field.
I am interested in all (or some - not important) rows of T1 and the
SUM(VALUE) of T2 that has a given TYPE and is linked to T1.
My query looks like this:
SELECT T1.ID, T1.NAME, COALESCE(SUM(VALUE), 0)
FROM T1 LEFT OUTER JOIN T2 ON T1.ID = T2.ID
WHERE
T2.TYPE = :WANTEDTYPE
GROUP BY T1.ID, T1.NAME
The strange thing is, that if there is no record from T2, then the T1
master row is omitted from the result set as well.
Example:
If T1 has only one record, and T2 is empty, then the query returns no rows.
In this case I would like to see the record from T1 too!
Any ideas?
(Stored procedures are out of question, as the SQL is generated
dynamically with some other WHERE constraints on T1)
Best regards, Zsolt Balanyi