pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Combining queries while preserving order in SQL - Help!
2+ messages / 2 participants
[nested] [flat]

* Combining queries while preserving order in SQL - Help!
@ 2002-12-02 18:46 Casey Allen Shobe <cshobe@osss.net>
  2002-12-02 19:23 ` Re: Combining queries while preserving order in SQL - Help! Joel Burton <joel@joelburton.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Casey Allen Shobe @ 2002-12-02 18:46 UTC (permalink / raw)
  To: pgsql-sql

Hi there,

I need to do the following in one SQL query:

select field1, field2, field3, field4, field5 from table where field6 < 5
order by field1

And a totals line which shows the sum for each column.

The important part is that I need to preserve the order by of the first query.

Is there any way to do this in one query?

Thank you,

-- 
Casey Allen Shobe, Open Source Software Solutions
cshobe@osss.net / http://www.osss.net / 770-653-4526



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

* Re: Combining queries while preserving order in SQL - Help!
  2002-12-02 18:46 Combining queries while preserving order in SQL - Help! Casey Allen Shobe <cshobe@osss.net>
@ 2002-12-02 19:23 ` Joel Burton <joel@joelburton.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Joel Burton @ 2002-12-02 19:23 UTC (permalink / raw)
  To: Casey Allen Shobe <cshobe@osss.net>; +Cc: pgsql-sql

On Mon, Dec 02, 2002 at 01:46:38PM -0500, Casey Allen Shobe wrote:
> Hi there,
> 
> I need to do the following in one SQL query:
> 
> select field1, field2, field3, field4, field5 from table where field6 < 5
> order by field1
> 
> And a totals line which shows the sum for each column.
> 
> The important part is that I need to preserve the order by of the first query.
> 
> Is there any way to do this in one query?

create table foo (a int, b int, c int );
insert into foo values (1,2,3);
insert into foo values (4,5,6);

select '' as label, 
       * 
from   foo 
union all 
select 'TOTAL', 
       sum(a), 
       sum(b),
       sum(c) 
from   foo 
order by 1,2;

(you wouldn't need the label column to sort by, except that a might
contain negative numbers, so the sum might be _less_ than some/all of
the numbers. by first sorting on this junk column, we can force the
totals at the bottom).

p.s. don't forget the "union __all__", otherwise you'll get rid of
duplicate entries in the your table.

-- 

Joel BURTON  |  joel@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant



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


end of thread, other threads:[~2002-12-02 19:23 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-12-02 18:46 Combining queries while preserving order in SQL - Help! Casey Allen Shobe <cshobe@osss.net>
2002-12-02 19:23 ` Joel Burton <joel@joelburton.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