Thread: sum but not grouped by?

sum but not grouped by?

From
Henry Ortega
Date:
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.


Re: sum but not grouped by?

From
Mischa Sandberg
Date:
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)




Re: sum but not grouped by?

From
Henry Ortega
Date:
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
>


Re: sum but not grouped by?

From
PFC
Date:
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...


Re: sum but not grouped by?

From
Mischa Sandberg
Date:
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?



Re: sum but not grouped by?

From
Luca Rasconi Master
Date:
let ff your table...<br /> try this:<br /><b>select </b><br />     a.id, <br />     a.date, <br />     a.hours, <br />
   (<b>select </b>sum(b.hours) <br />         <b>from </b>ff as b<br />         <b>where </b>a.id = b.id <br />        
<b>group</b>by b.id <br />     ) as a <br />     <b>from </b>ff as a;<br /><br /> bye,<br /> Luca<br /><br /> Henry
Ortegaha scritto:<br /><blockquote cite="mid2bffcc33050805092862a01b93@mail.gmail.com" type="cite"><pre wrap="">I have
theff 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?
              <a class="moz-txt-link-freetext"
href="http://www.postgresql.org/docs/faq">http://www.postgresql.org/docs/faq</a></pre></blockquote><br />