Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> tricky SQL question: how to return "virtual" computed columns

tricky SQL question: how to return "virtual" computed columns

From: David Michaels <david_at_shockmarket.com>
Date: Thu, 24 Jun 1999 21:08:17 -0700
Message-ID: <377300B1.299EFC93@shockmarket.com>


I'd like to return colums that don't actually correspond to columns in my table, but instead are computed from a group of rows within the table.

For example, assume the table:

NAME SCORE DAY
------- --------- ------

david    11        monday
david    10        monday
david    5          monday
mark    4          tuesday
mark    9          monday
eric      5          monday
eric      11        wednesday

I want to return one row per NAME, with two columns: one col for the sum(score) for all rows for that name, and one col for the sum(score) for rows matching that name and also matching a particular day of week.

so, for example, the query to get me david's total score, and total score for all mondays would return:
david 26 21

if david didn't have any scores from any mondays, then this query could return no rows.

I know I can do it in two queries:
SELECT name, SUM(score) FROM mytable WHERE DAY=monday GROUP BY day; SELECT name, SUM(score) FROM mytable WHERE NAME IN(SELECT name FROM mytable where DAY=monday) GROUP BY day;

Is this possible in a single SQL statement?

Thanks!

David
--

David Michaels <david_at_shockmarket.com>
Director of Technology
ShockMarket Corporation (650) 330-4665


Received on Thu Jun 24 1999 - 23:08:17 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US