pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Expression with aggregate
4+ messages / 4 participants
[nested] [flat]

* Expression with aggregate
@ 1999-08-23 13:42 Albert REINER <areiner@tph.tuwien.ac.at>
  0 siblings, 0 replies; 4+ messages in thread

From: Albert REINER @ 1999-08-23 13:42 UTC (permalink / raw)
  To: pgsql-sql

Hi!

This is actually a follow-up question on my posting "[NOVICE] Join
with aggregate" a couple of days ago; thanks again to all of you who
tried to help me, and actually did help me a lot. This time, my
problem is in using in a join the aggregate result in conjunction with
a non-aggregate field in a mathematical expression. More clearly (and
again I am stripping down the example to the simplest form; actually I
am thinking of recordings on video tapes, and I also take into account
the distinction between SP/LP recordings in the summation over playing
times):

I'm using Postgres 6.5.1, and I have tables like the following:

asdf=> create table a (id int4, name text, d int2);
CREATE
asdf=> create table b (a_id int4, c int2);
CREATE

with sample data:

asdf=> insert into a values (1, 'Number one', 800);
INSERT 418805 1
asdf=> insert into b values (1, 100);
INSERT 418806 1
asdf=> insert into b values (1, 200);
INSERT 418807 1

From this I want to produce a table having

id | name       |diff
---+------------+----
1  | Number one | 500

, where diff = 800 - sum(100, 200) = 500. I know that I can achieve
this with a temporary table, or with a view, using:

asdf=> create view c as select id, name, d, sum(b.c) from a, b where
id = a_id group by id, name, d;
CREATE
asdf=> select id, name, d-sum as diff from c;
id|name      |diff
--+----------+----
 1|Number one| 500
(1 row)

But what I really want to do is something like:

asdf=> select id, name, d-sum(b.c) as diff from a, b where id = a_id
group by id, name, diff;
ERROR:  Aggregates not allowed in GROUP BY clause

or:

asdf=> select id, name, d-sum(b.c) as diff from a, b where id = a_id
group by id, name;
ERROR:  Illegal use of aggregates or non-group column in target list

Is there a way to do this without the detour via the view? Or are
there views just for this reason?

By the way, when I use \d to list the tables, why is a view always
shown with a '?' like in

asdf=> \d
Database    = asdf
 +------------------+----------------------------------+----------+
 |  Owner           |             Relation             |   Type   |
 +------------------+----------------------------------+----------+
 | albert           | a                                | table    |
 | albert           | b                                | table    |
 | albert           | c                                | view?    |
 +------------------+----------------------------------+----------+

? Why doesn't it just say '| albert | c | view |'?

I'd really appreciate any hints with this, even though I know how to
do it (as demonstrated above) with the use of the view.

Thanks in advance for your help,

Albert.

-- 

---------------------------------------------------------------------------
  Post an / Mail to / Skribu al: Albert Reiner <areiner@tph.tuwien.ac.at>
---------------------------------------------------------------------------



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] Expression with aggregate
@ 1999-08-23 22:28 Scott Barron <kain224@yahoo.com>
  1999-08-24 04:24 ` Re: [SQL] Expression with aggregate Bruce Momjian <maillist@candle.pha.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Scott Barron @ 1999-08-23 22:28 UTC (permalink / raw)
  To: Albert REINER <areiner@tph.tuwien.ac.at>; pgsql-sql

Hi Albert,

Try this one:

SELECT a.id, a.name, a.d-sum(b.c) AS diff FROM a, b
WHERE a.id=1 AND a.id = b.a_id GROUP BY a.id, a.name,
a.d;

That should provide the desired results.  As for the
question about the view in the \d output, I don't know
why its like that, I've wondered myself.

Cheers,
Scott

--- Albert REINER <areiner@tph.tuwien.ac.at> wrote:
> Hi!
> 
> This is actually a follow-up question on my posting
> "[NOVICE] Join
> with aggregate" a couple of days ago; thanks again
> to all of you who
> tried to help me, and actually did help me a lot.
> This time, my
> problem is in using in a join the aggregate result
> in conjunction with
> a non-aggregate field in a mathematical expression.
> More clearly (and
> again I am stripping down the example to the
> simplest form; actually I
> am thinking of recordings on video tapes, and I also
> take into account
> the distinction between SP/LP recordings in the
> summation over playing
> times):
> 
> I'm using Postgres 6.5.1, and I have tables like the
> following:
> 
> asdf=> create table a (id int4, name text, d int2);
> CREATE
> asdf=> create table b (a_id int4, c int2);
> CREATE
> 
> with sample data:
> 
> asdf=> insert into a values (1, 'Number one', 800);
> INSERT 418805 1
> asdf=> insert into b values (1, 100);
> INSERT 418806 1
> asdf=> insert into b values (1, 200);
> INSERT 418807 1
> 
> From this I want to produce a table having
> 
> id | name       |diff
> ---+------------+----
> 1  | Number one | 500
> 
> , where diff = 800 - sum(100, 200) = 500. I know
> that I can achieve
> this with a temporary table, or with a view, using:
> 
> asdf=> create view c as select id, name, d, sum(b.c)
> from a, b where
> id = a_id group by id, name, d;
> CREATE
> asdf=> select id, name, d-sum as diff from c;
> id|name      |diff
> --+----------+----
>  1|Number one| 500
> (1 row)
> 
> But what I really want to do is something like:
> 
> asdf=> select id, name, d-sum(b.c) as diff from a, b
> where id = a_id
> group by id, name, diff;
> ERROR:  Aggregates not allowed in GROUP BY clause
> 
> or:
> 
> asdf=> select id, name, d-sum(b.c) as diff from a, b
> where id = a_id
> group by id, name;
> ERROR:  Illegal use of aggregates or non-group
> column in target list
> 
> Is there a way to do this without the detour via the
> view? Or are
> there views just for this reason?
> 
> By the way, when I use \d to list the tables, why is
> a view always
> shown with a '?' like in
> 
> asdf=> \d
> Database    = asdf
> 
>
+------------------+----------------------------------+----------+
>  |  Owner           |             Relation          
>   |   Type   |
> 
>
+------------------+----------------------------------+----------+
>  | albert           | a                             
>   | table    |
>  | albert           | b                             
>   | table    |
>  | albert           | c                             
>   | view?    |
> 
>
+------------------+----------------------------------+----------+
> 
> ? Why doesn't it just say '| albert | c | view |'?
> 
> I'd really appreciate any hints with this, even
> though I know how to
> do it (as demonstrated above) with the use of the
> view.
> 
> Thanks in advance for your help,
> 
> Albert.
> 
> -- 
> 
>
---------------------------------------------------------------------------
>   Post an / Mail to / Skribu al: Albert Reiner
> <areiner@tph.tuwien.ac.at>
>
---------------------------------------------------------------------------
> 
> ************
> 
> 

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] Expression with aggregate
  1999-08-23 22:28 Re: [SQL] Expression with aggregate Scott Barron <kain224@yahoo.com>
@ 1999-08-24 04:24 ` Bruce Momjian <maillist@candle.pha.pa.us>
  1999-08-24 13:37   ` Re: [SQL] Expression with aggregate José Soares <jose@sferacarta.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Bruce Momjian @ 1999-08-24 04:24 UTC (permalink / raw)
  To: Scott Barron <kain224@yahoo.com>; +Cc: Albert REINER <areiner@tph.tuwien.ac.at>; pgsql-sql

> 
> That should provide the desired results.  As for the
> question about the view in the \d output, I don't know
> why its like that, I've wondered myself.

I think it is view? because it could be a view, or a rule?

> > 
> > By the way, when I use \d to list the tables, why is
> > a view always
> > shown with a '?' like in
> > 
> > asdf=> \d
> > Database    = asdf
> > 
> >
> +------------------+----------------------------------+----------+
> >  |  Owner           |             Relation          
> >   |   Type   |
> > 
> >
> +------------------+----------------------------------+----------+
> >  | albert           | a                             
> >   | table    |
> >  | albert           | b                             
> >   | table    |
> >  | albert           | c                             
> >   | view?    |
> > 
> >
> +------------------+----------------------------------+----------+
> > 
> > ? Why doesn't it just say '| albert | c | view |'?
> > 
> > I'd really appreciate any hints with this, even
> > though I know how to
> > do it (as demonstrated above) with the use of the
> > view.
> > 
> > Thanks in advance for your help,
> > 
> > Albert.
> > 
> > -- 
> > 
> >
> ---------------------------------------------------------------------------
> >   Post an / Mail to / Skribu al: Albert Reiner
> > <areiner@tph.tuwien.ac.at>
> >
> ---------------------------------------------------------------------------
> > 
> > ************
> > 
> > 
> 
> __________________________________________________
> Do You Yahoo!?
> Bid and sell for free at http://auctions.yahoo.com
> 
> 
> ************
> 
> 


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] Expression with aggregate
  1999-08-23 22:28 Re: [SQL] Expression with aggregate Scott Barron <kain224@yahoo.com>
  1999-08-24 04:24 ` Re: [SQL] Expression with aggregate Bruce Momjian <maillist@candle.pha.pa.us>
@ 1999-08-24 13:37   ` José Soares <jose@sferacarta.com>
  0 siblings, 0 replies; 4+ messages in thread

From: José Soares @ 1999-08-24 13:37 UTC (permalink / raw)
  To: Bruce Momjian <maillist@candle.pha.pa.us>; +Cc: Scott Barron <kain224@yahoo.com>; Albert REINER <areiner@tph.tuwien.ac.at>; pgsql-sql

In PostgreSQL a view is a table with a special rule named: "_RETnametable" where
nametable stands for the table name used by the view.

eg:
CREATE RULE "_RETvista" AS ON SELECT TO "vista" DO INSTEAD SELECT "a" FROM
"tabella";

Actually, psql checks in the field pg_class.relkind to see if it is a table or
other kind of class ('i'=index, S=sequence, 'r'=table/view, etc).
If psql finds a 'r' into pg_class.relkind then it checks in the boolean field
pg_class.relhasrules and prints "table" if its value is FALSE or "view?" if its
value is TRUE.
The field pg_class.relhasrules is also used for every kind of rule in the table,
this is the reason why psql prints "view?" because it doesn't know for sure if
the rule is
a "_RET..." (view) or other kind of rule.


QUERY: SELECT usename, relname, relkind, relhasrules FROM pg_class, pg_user WHER
E usesysid = relowner and ( relkind = 'r' OR relkind = 'i' OR relkind = 'S') and
 relname !~ '^pg_' and (relkind != 'i' OR relname !~ '^xinx')  ORDER BY relname

Database    = prova
 +------------------+----------------------------------+----------+
 |  Owner           |             Relation             |   Type   |
 +------------------+----------------------------------+----------+
 | postgres         | serial                           | sequence |
 | postgres         | t_a_seq                          | sequence |
 | postgres         | tabella                          | table    |
 | postgres         | vista                            | view?    |
 +------------------+----------------------------------+----------+

There's an exactly way to know if the table is a view or a table; the function
pg_get_viewdef()
But I don't know why psql doesn't use it...


prova=> select pg_get_viewdef('vista');
pg_get_viewdef
--------------------------
SELECT "a" FROM "tabella";
(1 row)


prova=> select pg_get_viewdef('tabella');
pg_get_viewdef
--------------
Not a view
(1 row)



José



Bruce Momjian ha scritto:

> >
> > That should provide the desired results.  As for the
> > question about the view in the \d output, I don't know
> > why its like that, I've wondered myself.
>
> I think it is view? because it could be a view, or a rule?
>
> > >
> > > By the way, when I use \d to list the tables, why is
> > > a view always
> > > shown with a '?' like in
> > >
> > > asdf=> \d
> > > Database    = asdf
> > >
> > >
> > +------------------+----------------------------------+----------+
> > >  |  Owner           |             Relation
> > >   |   Type   |
> > >
> > >
> > +------------------+----------------------------------+----------+
> > >  | albert           | a
> > >   | table    |
> > >  | albert           | b
> > >   | table    |
> > >  | albert           | c
> > >   | view?    |
> > >
> > >
> > +------------------+----------------------------------+----------+
> > >
> > > ? Why doesn't it just say '| albert | c | view |'?
> > >
> > > I'd really appreciate any hints with this, even
> > > though I know how to
> > > do it (as demonstrated above) with the use of the
> > > view.
> > >
> > > Thanks in advance for your help,
> > >
> > > Albert.
> > >
> > > --
> > >
> > >
> > ---------------------------------------------------------------------------
> > >   Post an / Mail to / Skribu al: Albert Reiner
> > > <areiner@tph.tuwien.ac.at>
> > >
> > ---------------------------------------------------------------------------
> > >
> > > ************
> > >
> > >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Bid and sell for free at http://auctions.yahoo.com
> >
> >
> > ************
> >
> >
>
> --
>   Bruce Momjian                        |  http://www.op.net/~candle
>   maillist@candle.pha.pa.us            |  (610) 853-3000
>   +  If your life is a hard drive,     |  830 Blythe Avenue
>   +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
>
> ************

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~1999-08-24 13:37 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-08-23 13:42 Expression with aggregate Albert REINER <areiner@tph.tuwien.ac.at>
1999-08-23 22:28 Re: [SQL] Expression with aggregate Scott Barron <kain224@yahoo.com>
1999-08-24 04:24 ` Bruce Momjian <maillist@candle.pha.pa.us>
1999-08-24 13:37   ` José Soares <jose@sferacarta.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