pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedGetting one row for each subquery row...?
4+ messages / 2 participants
[nested] [flat]
* Getting one row for each subquery row...?
@ 2003-06-20 05:12 Együd Csaba <csegyud@freemail.hu>
0 siblings, 2 replies; 4+ messages in thread
From: Együd Csaba @ 2003-06-20 05:12 UTC (permalink / raw)
To: pgsql-sql
Hi All,
here are my three tables. I wold like to list them in the following way: I
need all the columns from t_stockchanges, and one field for the
productgroup
the t_stockchanges.productid belongs to.
But one product can belong to many groups, so i would need one row for each
group for each product.
My bad query is:
------------------------
DB=# select t_stockchanges.productid, (select name from t_productgroups
where id=(select productgroupid from t_prod_in_pgr where
productid=t_stockchanges.productid)) as pgroup from t_stockchanges;
ERROR: More than one tuple returned by a subselect used as an expression.
DB=#
---------------
Yes, this is absolutelly true, but I would like postgres to give me all the
tuples found. How can I ask him to do so?
Thank you,
-- Csaba
---------------------------------------------------------------------------
-
----------------------------------------------------
Table "public.t_stockchanges"
Column | Type | Modifiers
---------------+------------------+----------------------------------------
-
------------
id | integer | not null
stockid | integer | not null
productid | integer | not null
changeid | integer | not null
quantity | double precision | not null
date | character(19) | not null
purchaseprice | double precision | not null
correction | double precision | not null
userid | integer | not null
time | character(19) | default to_char(now(), 'YYYY.mm.dd
hh:mi:ss'::text)
prooftype | character(10) | not null default ''
proofid | integer | default 0
Indexes: t_stockchanges_pkey primary key btree (id),
t_stockchanges_date btree (date),
t_stockchanges_productid btree (productid)
---------------------------------------------------------------------------
-
----------------------------------------------------
Table "public.t_productgroups"
Column | Type | Modifiers
-------------+-----------------------+-----------
id | integer | not null
name | character varying(30) | not null
description | character varying |
root | boolean |
Indexes: t_productgroups_pkey primary key btree (id)
---------------------------------------------------------------------------
-
----------------------------------------------------
Table "public.t_prod_in_pgr"
Column | Type | Modifiers
----------------+---------+-----------
productgroupid | integer | not null
productid | integer | not null
---------------------------------------------------------------------------
-
----------------------------------------------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.489 / Virus Database: 288 - Release Date: 2003. 06. 10.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Getting one row for each subquery row...?
@ 2003-06-20 06:07 Tomasz Myrta <jasiek@klaster.net>
parent: Együd Csaba <csegyud@freemail.hu>
1 sibling, 0 replies; 4+ messages in thread
From: Tomasz Myrta @ 2003-06-20 06:07 UTC (permalink / raw)
To: Együd Csaba <csegyud@freemail.hu>; +Cc: pgsql-sql
Dnia 2003-06-20 07:12, Użytkownik Együd Csaba napisał:
> Hi All,
> here are my three tables. I wold like to list them in the following way: I
> need all the columns from t_stockchanges, and one field for the
> productgroup
> the t_stockchanges.productid belongs to.
>
> But one product can belong to many groups, so i would need one row for each
> group for each product.
>
> My bad query is:
> ------------------------
> DB=# select t_stockchanges.productid, (select name from t_productgroups
> where id=(select productgroupid from t_prod_in_pgr where
> productid=t_stockchanges.productid)) as pgroup from t_stockchanges;
> ERROR: More than one tuple returned by a subselect used as an expression.
> DB=#
> ---------------
> Yes, this is absolutelly true, but I would like postgres to give me all the
> tuples found. How can I ask him to do so?
>
> Thank you,
> -- Csaba
>
Sure, use "limit 1" in a subquery.
"limit" and "offset" are well described in Postgresql documentation.
Regards,
Tomasz Myrta
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Getting one row for each subquery row...?
@ 2003-06-20 06:11 Tomasz Myrta <jasiek@klaster.net>
parent: Együd Csaba <csegyud@freemail.hu>
1 sibling, 1 reply; 4+ messages in thread
From: Tomasz Myrta @ 2003-06-20 06:11 UTC (permalink / raw)
To: Együd Csaba <csegyud@freemail.hu>; +Cc: pgsql-sql
Dnia 2003-06-20 07:12, Użytkownik Együd Csaba napisał:
> Hi All,
> here are my three tables. I wold like to list them in the following way: I
> need all the columns from t_stockchanges, and one field for the
> productgroup
> the t_stockchanges.productid belongs to.
>
> But one product can belong to many groups, so i would need one row for each
> group for each product.
>
> My bad query is:
> ------------------------
> DB=# select t_stockchanges.productid, (select name from t_productgroups
> where id=(select productgroupid from t_prod_in_pgr where
> productid=t_stockchanges.productid)) as pgroup from t_stockchanges;
> ERROR: More than one tuple returned by a subselect used as an expression.
> DB=#
> ---------------
> Yes, this is absolutelly true, but I would like postgres to give me all the
> tuples found. How can I ask him to do so?
>
> Thank you,
> -- Csaba
Sorry, if you want all combination of grups and products, you need to rewrite
your query:
select
t_stockchanges.productid,
t_productgroups.name as pgroup
from
t_stockchanges
join t_prod_in using (productid)
join t_productgroups on (id=productgroupid)
or something like this.
Tomasz
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Getting one row for each subquery row...?
@ 2003-06-20 07:02 Együd Csaba <csegyud@freemail.hu>
parent: Tomasz Myrta <jasiek@klaster.net>
0 siblings, 0 replies; 4+ messages in thread
From: Együd Csaba @ 2003-06-20 07:02 UTC (permalink / raw)
To: Tomasz Myrta <jasiek@klaster.net>; +Cc: pgsql-sql
Thank you Tomasz,
this last one seems to be good - with a little change.
select distinct t_stockchanges.productid, t_productgroups.name as pgroup
from t_stockchanges
join t_prod_in_pgr using (productid)
join t_productgroups on (t_productgroups.id=productgroupid);
Thank you again.
By,
-- Csaba
> Sorry, if you want all combination of grups and products, you need to
rewrite
> your query:
>
> select
> t_stockchanges.productid,
> t_productgroups.name as pgroup
> from
> t_stockchanges
> join t_prod_in using (productid)
> join t_productgroups on (id=productgroupid)
>
> or something like this.
>
> Tomasz
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.489 / Virus Database: 288 - Release Date: 2003. 06. 10.
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2003-06-20 07:02 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-06-20 05:12 Getting one row for each subquery row...? Együd Csaba <csegyud@freemail.hu>
2003-06-20 06:07 ` Tomasz Myrta <jasiek@klaster.net>
2003-06-20 06:11 ` Tomasz Myrta <jasiek@klaster.net>
2003-06-20 07:02 ` Együd Csaba <csegyud@freemail.hu>
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