Re: Unexpected results with joins on dates - Mailing list pgsql-general

From David Johnston
Subject Re: Unexpected results with joins on dates
Date
Msg-id 003b01cc4097$89ccc700$9d665500$@yahoo.com
Whole thread Raw
In response to Re: Unexpected results with joins on dates  (Tim Uckun <timuckun@gmail.com>)
Responses Re: Unexpected results with joins on dates  (Tim Uckun <timuckun@gmail.com>)
List pgsql-general
You would have to use a UNION or a Function.  Either way, semantically common fields would want to share the same type
sothey could be output using the same column.  If you have additional fields you want to output that are source
specificyou can do so and just output NULL from invalid sources. 

SELECT 'SOURCE1' AS source, common1, common2, common3, source1_1, source1_2, NULL AS source2_1, NULL AS source2_2
FROM source1

UNION

SELECT 'SOURCE2' AS source, common1, common2, common3, NULL, NULL, source2_1, source2_2
FROM source 2

Only the first SELECT is used to define column types and names (in the case of NULL AS source2_* I am not positive if
youneed to cast the NULL or if it will use the type found in the second SELECT) and I generally put a "source" field
intothe output with a textual representation of which table the record originated from. 

Sample result data:
SOURCE1,C1,c2,c3,s11,c12,null,null
SOURCE2,C1,c2,c3,null,null,c21,c22

David J.


-----Original Message-----
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Tim Uckun
Sent: Tuesday, July 12, 2011 6:13 AM
To: David Johnston
Cc: pgsql-general
Subject: Re: [GENERAL] Unexpected results with joins on dates

On Tue, Jul 12, 2011 at 3:01 PM, David Johnston <polobo@yahoo.com> wrote:
> If traffic has 5 records on a date and sales has 4 on the same date you would output 20 records for that date.

What would I have to do in order to get 9 records instead of 20.  Like a union but with dissimilar schema.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


pgsql-general by date:

Previous
From: "David Johnston"
Date:
Subject: Re: ? about Composite Keys + ON DELETE/UPDATE SET NULL
Next
From: Grace Batumbya
Date:
Subject: Re: PostgreSQL JDBC: bytea column getMetaData().getColumns().getIn("COLUMN_SIZE") ~ 2GB