pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sum but not grouped by?
6+ messages / 4 participants
[nested] [flat]

* sum but not grouped by?
@ 2005-08-05 16:28  Henry Ortega <juandelacruz@gmail.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Henry Ortega @ 2005-08-05 16:28 UTC (permalink / raw)
  To: pgsql-sql

I have the ff data:

id      |     date         |      hours
AAA    07-01-2005           3
AAA    07-02-2005           4
BBB    07-01-2005           6
BBB    07-02-2005           2
BBB    07-03-2005           7

Would it be possible to get the ff:

id      |     date         |      hours   |   id_total
AAA    07-01-2005           3              7
AAA    07-02-2005           4              7
BBB    07-01-2005           6              15
BBB    07-02-2005           2              15
BBB    07-03-2005           7              15

So it's like SUM OF, but not Grouped By? Is this possible at all?
Thank you for any help.



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

* Re: sum but not grouped by?
@ 2005-08-05 16:47  Mischa Sandberg <mischa.sandberg@telus.net>
  parent: Henry Ortega <juandelacruz@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Mischa Sandberg @ 2005-08-05 16:47 UTC (permalink / raw)
  To: pgsql-sql

Quoting Henry Ortega <juandelacruz@gmail.com>:

> I have the ff data:
> 
> id      |     date         |      hours
> AAA    07-01-2005           3
> AAA    07-02-2005           4
> BBB    07-01-2005           6
> BBB    07-02-2005           2
> BBB    07-03-2005           7
> 
> Would it be possible to get the ff:
> 
> id      |     date         |      hours   |   id_total
> AAA    07-01-2005           3              7
> AAA    07-02-2005           4              7
> BBB    07-01-2005           6              15
> BBB    07-02-2005           2              15
> BBB    07-03-2005           7              15
> 
> So it's like SUM OF, but not Grouped By? Is this possible at all?
> Thank you for any help.

You're really joining two sets:

select FFDATA.id, FFDATA.date, FFDATA.hours, FFSUM.id_total
from    FFDATA
join (select id, sum(hours) as id_total
      from FFDATA group by id
     ) as FFSUM using(id)





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

* Re: sum but not grouped by?
@ 2005-08-05 17:53  Henry Ortega <juandelacruz@gmail.com>
  parent: Mischa Sandberg <mischa.sandberg@telus.net>
  0 siblings, 2 replies; 6+ messages in thread

From: Henry Ortega @ 2005-08-05 17:53 UTC (permalink / raw)
  To: pgsql-sql

Is it possible at all to do this without any joins
or subselect?



On 8/5/05, Mischa Sandberg <mischa.sandberg@telus.net> wrote:
> Quoting Henry Ortega <juandelacruz@gmail.com>:
> 
> > I have the ff data:
> >
> > id      |     date         |      hours
> > AAA    07-01-2005           3
> > AAA    07-02-2005           4
> > BBB    07-01-2005           6
> > BBB    07-02-2005           2
> > BBB    07-03-2005           7
> >
> > Would it be possible to get the ff:
> >
> > id      |     date         |      hours   |   id_total
> > AAA    07-01-2005           3              7
> > AAA    07-02-2005           4              7
> > BBB    07-01-2005           6              15
> > BBB    07-02-2005           2              15
> > BBB    07-03-2005           7              15
> >
> > So it's like SUM OF, but not Grouped By? Is this possible at all?
> > Thank you for any help.
> 
> You're really joining two sets:
> 
> select FFDATA.id, FFDATA.date, FFDATA.hours, FFSUM.id_total
> from    FFDATA
> join (select id, sum(hours) as id_total
>      from FFDATA group by id
>     ) as FFSUM using(id)
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly
>



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

* Re: sum but not grouped by?
@ 2005-08-05 18:05  PFC <lists@boutiquenumerique.com>
  parent: Henry Ortega <juandelacruz@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: PFC @ 2005-08-05 18:05 UTC (permalink / raw)
  To: Henry Ortega <juandelacruz@gmail.com>; pgsql-sql

On Fri, 05 Aug 2005 19:53:14 +0200, Henry Ortega <juandelacruz@gmail.com>  
wrote:

> Is it possible at all to do this without any joins
> or subselect?

	I don't think so.
	You could always hide them in a view...



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

* Re: sum but not grouped by?
@ 2005-08-07 06:55  Mischa Sandberg <mischa.sandberg@telus.net>
  parent: Henry Ortega <juandelacruz@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Mischa Sandberg @ 2005-08-07 06:55 UTC (permalink / raw)
  To: pgsql-sql

Quoting Henry Ortega <juandelacruz@gmail.com>:

> Is it possible at all to do this without any joins
> or subselect?

Nope. But I'm curious why you make that a condition?




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

* Re: sum but not grouped by?
@ 2005-08-07 14:52  Luca Rasconi Master <luca.rasconi@gmail.com>
  parent: Henry Ortega <juandelacruz@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Luca Rasconi Master @ 2005-08-07 14:52 UTC (permalink / raw)
  To: Henry Ortega <juandelacruz@gmail.com>; +Cc: pgsql-sql

let ff your table...
try this:
*select *
    a.id,
    a.date,
    a.hours,
    (*select *sum(b.hours)
        *from *ff as b
        *where *a.id = b.id
        *group *by b.id
    ) as a
    *from *ff as a;

bye,
Luca

Henry Ortega ha scritto:

>I have the ff data:
>
>id      |     date         |      hours
>AAA    07-01-2005           3
>AAA    07-02-2005           4
>BBB    07-01-2005           6
>BBB    07-02-2005           2
>BBB    07-03-2005           7
>
>Would it be possible to get the ff:
>
>id      |     date         |      hours   |   id_total
>AAA    07-01-2005           3              7
>AAA    07-02-2005           4              7
>BBB    07-01-2005           6              15
>BBB    07-02-2005           2              15
>BBB    07-03-2005           7              15
>
>So it's like SUM OF, but not Grouped By? Is this possible at all?
>Thank you for any help.
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: Have you checked our extensive FAQ?
>
>               http://www.postgresql.org/docs/faq
>  
>

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


end of thread, other threads:[~2005-08-07 14:52 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-08-05 16:28 sum but not grouped by? Henry Ortega <juandelacruz@gmail.com>
2005-08-05 16:47 ` Mischa Sandberg <mischa.sandberg@telus.net>
2005-08-05 17:53   ` Henry Ortega <juandelacruz@gmail.com>
2005-08-05 18:05     ` PFC <lists@boutiquenumerique.com>
2005-08-07 06:55     ` Mischa Sandberg <mischa.sandberg@telus.net>
2005-08-07 14:52 ` Luca Rasconi Master <luca.rasconi@gmail.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