Skip site navigation (1) Skip section navigation (2)

Need help building this query

From: Rihad <rihad(at)stream(dot)az>
To: pgsql-sql(at)postgresql(dot)org
Subject: Need help building this query
Date: 2012-06-21 17:48:56
Message-ID: 4FE35E88.6010805@stream.az (view raw or flat)
Thread:
Lists: pgsql-sql
Hi, folks. I currently need to join two tables that lack primary keys, 
and columns used to distinguish each record can be duplicated. I need to 
build statistics over the data in those tables. Consider this:


TableA:
row 1: foo: 123, bar: 456, baz: 789, amount: 10.99, date_of_op: date
row 2: foo: 123, bar: 456, baz: 789, amount: 10.99, date_of_op: date
row 3: foo: 123, bar: 456, baz: 789, amount: 10.99, date_of_op: date

TableB:
row 1: foo: 123, bar: 456, baz: 789, amount: 10.99, date_of_op: date


Columns foo + bar + baz are used to distinguish a performed "operation":
TableA.date_of_op isn't, because it can lag behind TableB.

Not all different "operations" are in table B.
Table B is just there so we know which "operations" are complete, so to 
speak (happening under external means and not under any of my control).

Now, for each operation (foo+bar+baz) in table A, only *one* row should 
be matched in table B, because it only has one matching row there.
The other two in TableA should be considered unmatched.

Now the query should be able to get count(*) and sum(amount) every day 
for that day, considering that matched and unmatched operations should 
be counted separately. The report would look something like this:

TableA.date_of_op  TableB.date_of_op
2012-06-21            [empty]                  [count(*) and sum(amount) 
of all data in TableA for this day unmatched in TableB]
2012-06-21            2012-06-20            [count(*) and sum(amount) of 
all data in TableA matched in TableB for the 20-th]
2012-06-21            2012-06-19            [count(*) and sum(amount) of 
all data in TableA matched in TableB for the 19-th]


Can this awkward thing be done in pure SQL, or I'd be better off using 
programming for this?

Thanks, I hope I could explain this.

Responses

pgsql-sql by date

Next:From: David JohnstonDate: 2012-06-21 18:40:35
Subject: Re: Need help building this query
Previous:From: Craig RingerDate: 2012-06-21 02:53:00
Subject: Re: Is there a similarity-function that minds national charsets?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group