pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedproblem with join & count
7+ messages / 4 participants
[nested] [flat]
* problem with join & count
@ 1999-02-04 20:39 pat@patoche.org
1999-02-04 21:49 ` Re: [SQL] problem with join & count Jan Wieck <jwieck@debis.com>
0 siblings, 1 reply; 7+ messages in thread
From: pat@patoche.org @ 1999-02-04 20:39 UTC (permalink / raw)
To: pgsql-sql
Hi,
I don't succeed to build an SQL to do the task i describe below.
If someone can helps, thanks in advance.
I have two tables
number | name
--------------
1 | toto
2 | titi
3 | tutu
(here the number is a primary key)
and
number | tag
---------------
1 | alpha
1 | beta
2 | gamma
3 | zeta
3 | epsilon
the number in the left column is the same of the number in the first table,
but can appear in that second table many times.
i need an sql query that will return:
name | how_often
-------------------
toto | 2
titi | 1
tutu | 2
that is, the result table should contain all names present in the first table
and then the number of times the number associated with the name appears in
the second table.
Thanks a lot for your help.
Patrick
/\//\/\/\\/\/\//\/\\/\/\\/\\/\//\/\\/\//\/\\/\//\/\\/\//\/\\
Patrick M. pat@patoche.org http://www.patoche.org/
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: [SQL] problem with join & count
1999-02-04 20:39 problem with join & count pat@patoche.org
@ 1999-02-04 21:49 ` Jan Wieck <jwieck@debis.com>
1999-02-04 22:10 ` Re: [SQL] problem with join & count Dimitri <dimitri@france.Sun.COM>
0 siblings, 1 reply; 7+ messages in thread
From: jwieck@debis.com @ 1999-02-04 21:49 UTC (permalink / raw)
To: pat@patoche.org; +Cc: pgsql-sql
> i need an sql query that will return:
>
> name | how_often
> -------------------
> toto | 2
> titi | 1
> tutu | 2
>
> that is, the result table should contain all names present in the first table
> and then the number of times the number associated with the name appears in
> the second table.
Postgres does not (and v6.5 will not) support outer joins or
subselects in the targetlist, what's required to do that in a
single SQL statement.
What you could do is using a SQL function that covers the
count() like this:
create function num_refs (int4) returns int4 as '
select count(*) from tab2 where id = $1;
' language 'SQL';
select item_name, num_refs(id) as how_often from tab1;
I changed 'number' into id and name into item_name because
both are reserved words.
Jan
--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#======================================== jwieck@debis.com (Jan Wieck) #
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: [SQL] problem with join & count
1999-02-04 20:39 problem with join & count pat@patoche.org
1999-02-04 21:49 ` Re: [SQL] problem with join & count Jan Wieck <jwieck@debis.com>
@ 1999-02-04 22:10 ` Dimitri <dimitri@france.Sun.COM>
0 siblings, 0 replies; 7+ messages in thread
From: Dimitri @ 1999-02-04 22:10 UTC (permalink / raw)
To: Jan Wieck <jwieck@debis.com>; +Cc: pat@patoche.org; pgsql-sql
Hm...
Why do be complex where you can be sample?
Your query is:
select name, count(*) as how_often
from table1, table2
where table1.number = table2.number
group by name;
and that's all, folks!
P.S. Regarding your example, I can say that you are French! Isnt it? :))
"toto, titi" - very unique as names :))
Jan Wieck wrote:
>
> > i need an sql query that will return:
> >
> > name | how_often
> > -------------------
> > toto | 2
> > titi | 1
> > tutu | 2
> >
> > that is, the result table should contain all names present in the first table
> > and then the number of times the number associated with the name appears in
> > the second table.
>
> Postgres does not (and v6.5 will not) support outer joins or
> subselects in the targetlist, what's required to do that in a
> single SQL statement.
>
> What you could do is using a SQL function that covers the
> count() like this:
>
> create function num_refs (int4) returns int4 as '
> select count(*) from tab2 where id = $1;
> ' language 'SQL';
>
> select item_name, num_refs(id) as how_often from tab1;
>
> I changed 'number' into id and name into item_name because
> both are reserved words.
>
> Jan
>
> --
>
> #======================================================================#
> # It's easier to get forgiveness for being wrong than for being right. #
> # Let's break this rule - forgive me. #
> #======================================== jwieck@debis.com (Jan Wieck) #
--
=====================================================
Dimitri KRAVTCHUK (dim) Sun Microsystems
Benchmark Engineer France
dimitri@France.Sun.COM
=====================================================
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: [SQL] problem with join & count
@ 1999-02-04 21:00 Jackson, DeJuan <djackson@cpsgroup.com>
1999-02-04 22:14 ` Re: [SQL] problem with join & count Dimitri <dimitri@france.Sun.COM>
0 siblings, 1 reply; 7+ messages in thread
From: Jackson, DeJuan @ 1999-02-04 21:00 UTC (permalink / raw)
To: pat@patoche.org; pgsql-sql
try:
SELECT t1.name, count(t2.tag)
FROM t1, t2
WHERE t1.number = t2.number
GROUP BY t1.number, t1.name
-DEJ
> -----Original Message-----
> From: pat@patoche.org [mailto:pat@patoche.org]
> Sent: Thursday, February 04, 1999 2:39 PM
> To: pgsql-sql@postgreSQL.org
> Subject: [SQL] problem with join & count
>
>
> Hi,
>
> I don't succeed to build an SQL to do the task i describe below.
> If someone can helps, thanks in advance.
>
> I have two tables
>
> number | name
> --------------
> 1 | toto
> 2 | titi
> 3 | tutu
>
> (here the number is a primary key)
>
> and
>
> number | tag
> ---------------
> 1 | alpha
> 1 | beta
> 2 | gamma
> 3 | zeta
> 3 | epsilon
>
> the number in the left column is the same of the number in
> the first table,
> but can appear in that second table many times.
>
> i need an sql query that will return:
>
> name | how_often
> -------------------
> toto | 2
> titi | 1
> tutu | 2
>
> that is, the result table should contain all names present in
> the first table
> and then the number of times the number associated with the
> name appears in
> the second table.
>
> Thanks a lot for your help.
>
> Patrick
>
> /\//\/\/\\/\/\//\/\\/\/\\/\\/\//\/\\/\//\/\\/\//\/\\/\//\/\\
> Patrick M. pat@patoche.org http://www.patoche.org/
>
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: [SQL] problem with join & count
1999-02-04 21:00 RE: [SQL] problem with join & count Jackson, DeJuan <djackson@cpsgroup.com>
@ 1999-02-04 22:14 ` Dimitri <dimitri@france.Sun.COM>
0 siblings, 0 replies; 7+ messages in thread
From: Dimitri @ 1999-02-04 22:14 UTC (permalink / raw)
To: Jackson, DeJuan <djackson@cpsgroup.com>; +Cc: pat@patoche.org; pgsql-sql
Jackson, DeJuan wrote:
>
> try:
> SELECT t1.name, count(t2.tag)
> FROM t1, t2
> WHERE t1.number = t2.number
> GROUP BY t1.number, t1.name
______________^
your query will never work :)), you have to miss t1.number...
Best regards!
(dim)
>
> -DEJ
>
> > -----Original Message-----
> > From: pat@patoche.org [mailto:pat@patoche.org]
> > Sent: Thursday, February 04, 1999 2:39 PM
> > To: pgsql-sql@postgreSQL.org
> > Subject: [SQL] problem with join & count
> >
> >
> > Hi,
> >
> > I don't succeed to build an SQL to do the task i describe below.
> > If someone can helps, thanks in advance.
> >
> > I have two tables
> >
> > number | name
> > --------------
> > 1 | toto
> > 2 | titi
> > 3 | tutu
> >
> > (here the number is a primary key)
> >
> > and
> >
> > number | tag
> > ---------------
> > 1 | alpha
> > 1 | beta
> > 2 | gamma
> > 3 | zeta
> > 3 | epsilon
> >
> > the number in the left column is the same of the number in
> > the first table,
> > but can appear in that second table many times.
> >
> > i need an sql query that will return:
> >
> > name | how_often
> > -------------------
> > toto | 2
> > titi | 1
> > tutu | 2
> >
> > that is, the result table should contain all names present in
> > the first table
> > and then the number of times the number associated with the
> > name appears in
> > the second table.
> >
> > Thanks a lot for your help.
> >
> > Patrick
> >
> > /\//\/\/\\/\/\//\/\\/\/\\/\\/\//\/\\/\//\/\\/\//\/\\/\//\/\\
> > Patrick M. pat@patoche.org http://www.patoche.org/
> >
> >
--
=====================================================
Dimitri KRAVTCHUK (dim) Sun Microsystems
Benchmark Engineer France
dimitri@France.Sun.COM
=====================================================
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: [SQL] problem with join & count
@ 1999-02-04 22:52 Jackson, DeJuan <djackson@cpsgroup.com>
1999-02-05 10:16 ` Re: [SQL] problem with join & count Dimitri <dimitri@france.Sun.COM>
0 siblings, 1 reply; 7+ messages in thread
From: Jackson, DeJuan @ 1999-02-04 22:52 UTC (permalink / raw)
To: Dimitri <dimitri@france.Sun.COM>; +Cc: pat@patoche.org; pgsql-sql
djackson=> create table t1 (i SERIAL, n TEXT);
NOTICE: CREATE TABLE will create implicit sequence t1_i_seq for SERIAL
column t1.i
NOTICE: CREATE TABLE/UNIQUE will create implicit index t1_i_key for
table t1
CREATE
djackson=> insert into t1(n) VALUES ('toto');
INSERT 231199 1
djackson=> insert into t1(n) VALUES ('tutu');
INSERT 231200 1
djackson=> insert into t1(n) VALUES ('titi');
INSERT 231201 1
djackson=> insert into t1(n) VALUES ('toto');
INSERT 231202 1
djackson=> create table t2 (i INT, tag TEXT);
CREATE
djackson=> insert into t2(i, tag) VALUES(1, 'alpha');
INSERT 231213 1
djackson=> insert into t2(i, tag) VALUES(1, 'beta');
INSERT 231214 1
djackson=> insert into t2(i, tag) VALUES(2, 'gamma');
INSERT 231215 1
djackson=> insert into t2(i, tag) VALUES(3, 'zeta');
INSERT 231216 1
djackson=> insert into t2(i, tag) VALUES(3, 'epsilon');
INSERT 231217 1
djackson=> insert into t2(i, tag) VALUES(4, 'omega');
INSERT 231218 1
djackson=> select * from t1;
i|n
-+----
1|toto
2|tutu
3|titi
4|toto
(4 rows)
djackson=> select * from t2;
i|tag
-+-------
1|alpha
1|beta
2|gamma
3|zeta
3|epsilon
4|omega
(6 rows)
*****************MY QUERY****************
djackson=> select t1.n, count(t2.tag) from t1, t2 where t1.i=t2.i group
by t1.i, t1.name;
ERROR: func_get_detail: No such attribute or function 'name'
djackson=> select t1.n, count(t2.tag) from t1, t2 where t1.i=t2.i group
by t1.i, t1.n;
n |count
----+-----
toto| 2
tutu| 1
titi| 2
toto| 1
(4 rows)
****************YOUR QUERY AS WRITTEN TO JAN**************
djackson=> select n, count(*) as how_often from t1, t2 where t1.i=t2.i
group by n;
n |how_often
----+---------
titi| 2
toto| 3
tutu| 1
(3 rows)
Get the point?
-DEJ
> -----Original Message-----
> From: Dimitri [mailto:dimitri@france.Sun.COM]
> Sent: Thursday, February 04, 1999 4:14 PM
> To: Jackson, DeJuan
> Cc: pat@patoche.org; pgsql-sql@postgreSQL.org
> Subject: Re: [SQL] problem with join & count
>
>
> Jackson, DeJuan wrote:
> >
> > try:
> > SELECT t1.name, count(t2.tag)
> > FROM t1, t2
> > WHERE t1.number = t2.number
> > GROUP BY t1.number, t1.name
> ______________^
> your query will never work :)), you have to miss t1.number...
>
> Best regards!
> (dim)
>
> >
> > -DEJ
> >
> > > -----Original Message-----
> > > From: pat@patoche.org [mailto:pat@patoche.org]
> > > Sent: Thursday, February 04, 1999 2:39 PM
> > > To: pgsql-sql@postgreSQL.org
> > > Subject: [SQL] problem with join & count
> > >
> > >
> > > Hi,
> > >
> > > I don't succeed to build an SQL to do the task i describe below.
> > > If someone can helps, thanks in advance.
> > >
> > > I have two tables
> > >
> > > number | name
> > > --------------
> > > 1 | toto
> > > 2 | titi
> > > 3 | tutu
> > >
> > > (here the number is a primary key)
> > >
> > > and
> > >
> > > number | tag
> > > ---------------
> > > 1 | alpha
> > > 1 | beta
> > > 2 | gamma
> > > 3 | zeta
> > > 3 | epsilon
> > >
> > > the number in the left column is the same of the number in
> > > the first table,
> > > but can appear in that second table many times.
> > >
> > > i need an sql query that will return:
> > >
> > > name | how_often
> > > -------------------
> > > toto | 2
> > > titi | 1
> > > tutu | 2
> > >
> > > that is, the result table should contain all names present in
> > > the first table
> > > and then the number of times the number associated with the
> > > name appears in
> > > the second table.
> > >
> > > Thanks a lot for your help.
> > >
> > > Patrick
> > >
> > > /\//\/\/\\/\/\//\/\\/\/\\/\\/\//\/\\/\//\/\\/\//\/\\/\//\/\\
> > > Patrick M. pat@patoche.org http://www.patoche.org/
> > >
> > >
>
> --
> =====================================================
> Dimitri KRAVTCHUK (dim) Sun Microsystems
> Benchmark Engineer France
> dimitri@France.Sun.COM
> =====================================================
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: [SQL] problem with join & count
1999-02-04 22:52 RE: [SQL] problem with join & count Jackson, DeJuan <djackson@cpsgroup.com>
@ 1999-02-05 10:16 ` Dimitri <dimitri@france.Sun.COM>
0 siblings, 0 replies; 7+ messages in thread
From: Dimitri @ 1999-02-05 10:16 UTC (permalink / raw)
To: Jackson, DeJuan <djackson@cpsgroup.com>; +Cc: pat@patoche.org; pgsql-sql
Jackson, why do you prefer to mix the eggs and balls?
If you want to know HOW OFTEN the name is used in your database
it doesn't metter if it has a different ID. Second, from Patrick's
example is not seen that he will use duplicated names, and I think
he uses numbers just to save a place, so for which reason did you add
the record 'toto' with number 4? An third, any fields used in GROUP BY
clause
must to be present in SELECT clause (according to SQL standard), that's
why I sad
your query will never work and if it works under PSQL - we have a bug...
Best regards!
(dim)
Jackson, DeJuan wrote:
>
> djackson=> create table t1 (i SERIAL, n TEXT);
> NOTICE: CREATE TABLE will create implicit sequence t1_i_seq for SERIAL
> column t1.i
> NOTICE: CREATE TABLE/UNIQUE will create implicit index t1_i_key for
> table t1
> CREATE
> djackson=> insert into t1(n) VALUES ('toto');
> INSERT 231199 1
> djackson=> insert into t1(n) VALUES ('tutu');
> INSERT 231200 1
> djackson=> insert into t1(n) VALUES ('titi');
> INSERT 231201 1
> djackson=> insert into t1(n) VALUES ('toto');
> INSERT 231202 1
> djackson=> create table t2 (i INT, tag TEXT);
> CREATE
> djackson=> insert into t2(i, tag) VALUES(1, 'alpha');
> INSERT 231213 1
> djackson=> insert into t2(i, tag) VALUES(1, 'beta');
> INSERT 231214 1
> djackson=> insert into t2(i, tag) VALUES(2, 'gamma');
> INSERT 231215 1
> djackson=> insert into t2(i, tag) VALUES(3, 'zeta');
> INSERT 231216 1
> djackson=> insert into t2(i, tag) VALUES(3, 'epsilon');
> INSERT 231217 1
> djackson=> insert into t2(i, tag) VALUES(4, 'omega');
> INSERT 231218 1
> djackson=> select * from t1;
> i|n
> -+----
> 1|toto
> 2|tutu
> 3|titi
> 4|toto
> (4 rows)
>
> djackson=> select * from t2;
> i|tag
> -+-------
> 1|alpha
> 1|beta
> 2|gamma
> 3|zeta
> 3|epsilon
> 4|omega
> (6 rows)
> *****************MY QUERY****************
> djackson=> select t1.n, count(t2.tag) from t1, t2 where t1.i=t2.i group
> by t1.i, t1.name;
> ERROR: func_get_detail: No such attribute or function 'name'
> djackson=> select t1.n, count(t2.tag) from t1, t2 where t1.i=t2.i group
> by t1.i, t1.n;
> n |count
> ----+-----
> toto| 2
> tutu| 1
> titi| 2
> toto| 1
> (4 rows)
> ****************YOUR QUERY AS WRITTEN TO JAN**************
> djackson=> select n, count(*) as how_often from t1, t2 where t1.i=t2.i
> group by n;
> n |how_often
> ----+---------
> titi| 2
> toto| 3
> tutu| 1
> (3 rows)
>
> Get the point?
> -DEJ
> > -----Original Message-----
> > From: Dimitri [mailto:dimitri@france.Sun.COM]
> > Sent: Thursday, February 04, 1999 4:14 PM
> > To: Jackson, DeJuan
> > Cc: pat@patoche.org; pgsql-sql@postgreSQL.org
> > Subject: Re: [SQL] problem with join & count
> >
> >
> > Jackson, DeJuan wrote:
> > >
> > > try:
> > > SELECT t1.name, count(t2.tag)
> > > FROM t1, t2
> > > WHERE t1.number = t2.number
> > > GROUP BY t1.number, t1.name
> > ______________^
> > your query will never work :)), you have to miss t1.number...
> >
> > Best regards!
> > (dim)
> >
> > >
> > > -DEJ
> > >
> > > > -----Original Message-----
> > > > From: pat@patoche.org [mailto:pat@patoche.org]
> > > > Sent: Thursday, February 04, 1999 2:39 PM
> > > > To: pgsql-sql@postgreSQL.org
> > > > Subject: [SQL] problem with join & count
> > > >
> > > >
> > > > Hi,
> > > >
> > > > I don't succeed to build an SQL to do the task i describe below.
> > > > If someone can helps, thanks in advance.
> > > >
> > > > I have two tables
> > > >
> > > > number | name
> > > > --------------
> > > > 1 | toto
> > > > 2 | titi
> > > > 3 | tutu
> > > >
> > > > (here the number is a primary key)
> > > >
> > > > and
> > > >
> > > > number | tag
> > > > ---------------
> > > > 1 | alpha
> > > > 1 | beta
> > > > 2 | gamma
> > > > 3 | zeta
> > > > 3 | epsilon
> > > >
> > > > the number in the left column is the same of the number in
> > > > the first table,
> > > > but can appear in that second table many times.
> > > >
> > > > i need an sql query that will return:
> > > >
> > > > name | how_often
> > > > -------------------
> > > > toto | 2
> > > > titi | 1
> > > > tutu | 2
> > > >
> > > > that is, the result table should contain all names present in
> > > > the first table
> > > > and then the number of times the number associated with the
> > > > name appears in
> > > > the second table.
> > > >
> > > > Thanks a lot for your help.
> > > >
> > > > Patrick
> > > >
> > > > /\//\/\/\\/\/\//\/\\/\/\\/\\/\//\/\\/\//\/\\/\//\/\\/\//\/\\
> > > > Patrick M. pat@patoche.org http://www.patoche.org/
> > > >
> > > >
> >
> > --
> > =====================================================
> > Dimitri KRAVTCHUK (dim) Sun Microsystems
> > Benchmark Engineer France
> > dimitri@France.Sun.COM
> > =====================================================
> >
--
=====================================================
Dimitri KRAVTCHUK (dim) Sun Microsystems
Benchmark Engineer France
dimitri@France.Sun.COM
=====================================================
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~1999-02-05 10:16 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-02-04 20:39 problem with join & count pat@patoche.org
1999-02-04 21:49 ` Jan Wieck <jwieck@debis.com>
1999-02-04 22:10 ` Dimitri <dimitri@france.Sun.COM>
1999-02-04 21:00 RE: [SQL] problem with join & count Jackson, DeJuan <djackson@cpsgroup.com>
1999-02-04 22:14 ` Dimitri <dimitri@france.Sun.COM>
1999-02-04 22:52 RE: [SQL] problem with join & count Jackson, DeJuan <djackson@cpsgroup.com>
1999-02-05 10:16 ` Dimitri <dimitri@france.Sun.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