pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedright join problem
3+ messages / 2 participants
[nested] [flat]
* right join problem
@ 2009-10-02 05:33 Greenhorn <user.postgresql@gmail.com>
2009-10-02 05:45 ` Re: right join problem A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 1 reply; 3+ messages in thread
From: Greenhorn @ 2009-10-02 05:33 UTC (permalink / raw)
To: pgsql-sql
Hi,
I'm trying to retrieve all meter_id from table meter, and for meter_id
without amount, I'd like it to show nothing, 'null'. I've used right
join here but it's not giving me my desired result. Is there another
way to do this? Here's the query I'm trying.
Thanks in advance!
select m.meter_id, mp.meter_id, sum(amount_in_cents) as am
from meter m
right join transaction_mpark mp on (m.meter_id = mp.meter_id)
where date_time between '2009-09-28 00:00:00' and '2009-10-04 23:59:59'
group by m.meter_id, mp.meter_id
order by m.meter_id
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: right join problem
2009-10-02 05:33 right join problem Greenhorn <user.postgresql@gmail.com>
@ 2009-10-02 05:45 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2009-10-02 06:36 ` Re: right join problem A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 1 reply; 3+ messages in thread
From: A. Kretschmer @ 2009-10-02 05:45 UTC (permalink / raw)
To: pgsql-sql
In response to Greenhorn :
> Hi,
>
> I'm trying to retrieve all meter_id from table meter, and for meter_id
> without amount, I'd like it to show nothing, 'null'. I've used right
> join here but it's not giving me my desired result. Is there another
> way to do this? Here's the query I'm trying.
>
> Thanks in advance!
>
> select m.meter_id, mp.meter_id, sum(amount_in_cents) as am
> from meter m
> right join transaction_mpark mp on (m.meter_id = mp.meter_id)
> where date_time between '2009-09-28 00:00:00' and '2009-10-04 23:59:59'
> group by m.meter_id, mp.meter_id
> order by m.meter_id
Use a left join instead?
test=*# select * from master;
id | name
----+----------
1 | master 1
2 | master 2
(2 rows)
test=*# select * from slave;
id | value
----+-------
1 | 1
1 | 2
1 | 3
(3 rows)
test=*# select m.id, s.id, sum(s.value) from master m right join slave s on (m.id=s.id) group by m.id, s.id;
id | id | sum
----+----+-----
1 | 1 | 6
(1 row)
test=*# select m.id, s.id, sum(s.value) from master m left join slave s on (m.id=s.id) group by m.id, s.id;
id | id | sum
----+----+-----
1 | 1 | 6
2 | |
(2 rows)
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: right join problem
2009-10-02 05:33 right join problem Greenhorn <user.postgresql@gmail.com>
2009-10-02 05:45 ` Re: right join problem A. Kretschmer <andreas.kretschmer@schollglas.com>
@ 2009-10-02 06:36 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 0 replies; 3+ messages in thread
From: A. Kretschmer @ 2009-10-02 06:36 UTC (permalink / raw)
To: Greenhorn <user.postgresql@gmail.com>; pgsql-sql
In response to Greenhorn :
> >
> > Use a left join instead?
> Hi,
> Yes, but when i use a where clause on my query it only selects joined
> records. :(
> Thanks.
Please, answer to the list and not directly to me.
The WHERE-Clause works after the JOIN - so your select contains (without
the WHERE) all rows.
Maybe i don't understand you, can you show a simple example with data?
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2009-10-02 06:36 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-10-02 05:33 right join problem Greenhorn <user.postgresql@gmail.com>
2009-10-02 05:45 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2009-10-02 06:36 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox