[GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-28 Thread Håkan Jacobsson
Hi all, I have three tables like this: table1 with column table1_ID table2 with columns table1_ID, date ..etc table3 with columns table1_ID, date ..etc I would like to create one query to retrieve the rowcount ( count(*) ) from both table2 and table3 WHERE date BETWEEN fromdate1 AND todate1

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-28 Thread Adam Rich
> Resulting in 4 columns in the ResultSet like: > > count(*)_from_table2_between_fromdate1_and_todate1 = X > count(*)_from_table2_between_fromdate2_and_todate2 = Y > count(*)_from_table3_between_fromdate1_and_todate1 = Z > count(*)_from_table3_between_fromdate2_and_todate2 = V > > Is thi

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-28 Thread nathan wagner
Håkan Jacobsson wrote: > I don't get it=). How do I input the second daterange in this query? Through whatever mechanism you are using to issue the query. > Also, I have the ID from table1. Its known in the query. Oops. I forgot that part in my reply. So my where clause is wrong, though eas

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-28 Thread Håkan Jacobsson
2008 15:22 Till: Håkan Jacobsson; pgsql-general@postgresql.org Ämne: RE: [GENERAL] Getting the count(*) from two tables and two date ranges in same query > Resulting in 4 columns in the ResultSet like: > > count(*)_from_table2_between_fromdate1_and_t

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-28 Thread nathan wagner
Adam Rich wrote: Resulting in 4 columns in the ResultSet like: count(*)_from_table2_between_fromdate1_and_todate1 = X count(*)_from_table2_between_fromdate2_and_todate2 = Y count(*)_from_table3_between_fromdate1_and_todate1 = Z count(*)_from_table3_between_fromdate2_and_todate2 = V

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-28 Thread Sam Mason
On Mon, Jan 28, 2008 at 03:11:10PM +0100, H??kan Jacobsson wrote: > Resulting in 4 columns in the ResultSet like: > > count(*)_from_table2_between_fromdate1_and_todate1 = X > count(*)_from_table2_between_fromdate2_and_todate2 = Y > count(*)_from_table3_between_fromdate1_and_todate1 = Z > count(*

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-29 Thread Håkan Jacobsson
97 50 || Fax (+46) 8 661 19 22 -Ursprungligt meddelande- Från: Adam Rich [mailto:[EMAIL PROTECTED] Skickat: den 28 januari 2008 16:27 Till: Håkan Jacobsson; pgsql-general@postgresql.org Ämne: Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query Håkan, You ca

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-30 Thread Håkan Jacobsson
pgsql-general@postgresql.org Ämne: Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query Håkan, You can add as many date ranges as you need: Select t1.id, sum(case when t2.date between d1 and d2 then 1 else 0 end) as sum1, sum(case when t2.date between d3 and d4 t

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-30 Thread Håkan Jacobsson
Adam, I just realised that issuing the SQL on one table produces the correct count. SELECT sum(case when table2.date between '2007-07-13' and '2007-07-13' then 1 else 0 end) as sumx FROM table2 WHERE id = n; This is working alright. So the problem should lie in the last part: from table2, tabl

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-30 Thread Tom Lane
=?iso-8859-1?Q?H=E5kan_Jacobsson?= <[EMAIL PROTECTED]> writes: > I just realised that issuing the SQL on one table produces the correct count. > SELECT sum(case when table2.date between '2007-07-13' and '2007-07-13' then 1 > else 0 > end) as sumx FROM table2 WHERE id = n; > This is working alrig

Re: [GENERAL] Getting the count(*) from two tables and two date ranges in same query

2008-01-31 Thread Sam Mason
On Wed, Jan 30, 2008 at 09:52:17AM +0100, H??kan Jacobsson wrote: > I just realised that issuing the SQL on one table produces the correct count. > > SELECT sum(case when table2.date between '2007-07-13' and '2007-07-13' then 1 > else 0 > end) as sumx FROM table2 WHERE id = n; > > This is workin