SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 11-30-2006, 02:11 PM
spartacus
 
Posts: n/a
Default SQL 92 dialect question:

How to perform aggregation (sum) on a computed column?


How can I make this query work?

SELECT (AGE * 10) AS XYZ, SUM(XYZ) FROM MY_TABLE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 12-01-2006, 04:17 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default SQL 92 answer. Re: How to perform aggregation (sum) on a computed column?

There are several ways you could try this. The easisest however (but probably not the most efficient) is to use a subquery.

SELECT
(AGE * 10) AS XYZ,
(SELECT SUM(AGE * 10) FROM MY_TABLE ) AS XYZ_SUM
FROM MY_TABLE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-01-2006, 04:19 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default MySQL answer. Re: How to perform aggregation (sum) on a computed column?

This is specific to MySQL. This will give you a running sum. The last row will have the total sum of all the ages.

SET @a := 0;
SELECT
(AGE * 10) AS XYZ,
@a := @a + (AGE*10)
 FROM MY_TABLE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-23-2007, 04:00 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: How to perform aggregation (sum) on a computed column?

Not answering the general question, merely a solution to your concrete example:
SELECT age*10 AS xyz,t.* FROM my_table JOIN (SELECT sum(age) * 10 FROM my_table) t
Notes:
1) The sum is computed just once and returned as a table of cardinality 1. The table is then joined with my_table. The whole statement requires two full table scans on my-table.
2) (A x 10) + (B x 10) = (A + B) x 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Moderation Tools:



All times are GMT. The time now is 09:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7