dbTalk Databases Forums  

how to sum a derived column

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss how to sum a derived column in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
du_bing@hotmail.com
 
Posts: n/a

Default how to sum a derived column - 03-12-2010 , 11:00 AM






Hi,

The following statement was intended to get total count from each
section first and then get the total counts of all sections. The
union part works and returns. But 'select sum(used)' does not. The
error message can be found at the end of this message. Appreciate any
help! Thanks.

------
used
47
73
-----

select sum(used)
from (
select count(*) as "used"
from ur.agncy
where agncy_id >= '2283' and agncy_id <= '2329'

union

select count(*) as "used"
from ur.agncy
where agncy_id >= '2922' and agncy_id <= '2994'
)

An error occurred while running the query.

Illegal symbol "<EMPTY>", Some symbols that might be legal are:
CORRELATION NAME.

(SQL code = -104, SQL state =42601)

Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: how to sum a derived column - 03-12-2010 , 12:40 PM






Quote:
Illegal symbol "<EMPTY>", Some symbols that might be legal are:
CORRELATION NAME.

(SQL code = -104, SQL state =42601)
Perhaps you are using older version of DB2.
The reason of the message would be missing correlation clause.

Try to add "AS q" like in the following example.
("q" could be any valid correlation-name.)

select sum(used)
from (
select count(*) as "used"
from ur.agncy
where agncy_id >= '2283' and agncy_id <= '2329'
union
select count(*) as "used"
from ur.agncy
where agncy_id >= '2922' and agncy_id <= '2994'
) AS q

Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: how to sum a derived column - 03-12-2010 , 01:00 PM



Quote:
select sum(used)
from (
select count(*) as "used"
from ur.agncy
where agncy_id >= '2283' and agncy_id <= '2329'
union
select count(*) as "used"
from ur.agncy
where agncy_id >= '2922' and agncy_id <= '2994'
) AS q
Some issues.
1) Possible syntax error.
SELECT SUM(used) should be SELECT SUM("used")
or
remove double quotations from as "used" in subquery.

2) Possible incorrect result.
UNION should be UNION ALL.
If first and second select in subquery returned same count,
final result SUM(used) would return the count.
(Not two times of the count.)

3) You can use BETWEEN expression.

Here is an example of rewrite.
SELECT SUM(used)
FROM (
SELECT COUNT(*) as used
FROM ur.agncy
WHERE agncy_id BETWEEN '2283' AND '2329'
UNION ALL
SELECT COUNT(*) as used
FROM ur.agncy
WHERE agncy_id BETWEEN '2922' AND '2994'
) AS q
;

Reply With Quote
  #4  
Old   
Tonkuma
 
Posts: n/a

Default Re: how to sum a derived column - 03-12-2010 , 01:05 PM



Quote:
Here is an example of rewrite.
SELECT SUM(used)
* FROM (
* * * *SELECT COUNT(*) as used
* * * * *FROM ur.agncy
* * * * WHERE agncy_id BETWEEN '2283' AND '2329'
* * * *UNION ALL
* * * *SELECT COUNT(*) as used
* * * * *FROM ur.agncy
* * * * WHERE agncy_id BETWEEN '2922' AND '2994'
* * * *) AS q
;
Or, simply...
SELECT COUNT(*)
FROM ur.agncy
WHERE agncy_id BETWEEN '2283' AND '2329'
OR agncy_id BETWEEN '2922' AND '2994'
;

Reply With Quote
  #5  
Old   
Tonkuma
 
Posts: n/a

Default Re: how to sum a derived column - 03-12-2010 , 01:20 PM



Quote:
to get total count from each section first and then get the total counts of all sections
Get "count from each section" and "total counts" at once.

SELECT CASE GROUPING(section)
WHEN 1 THEN
'All sctions'
ELSE section
END AS section
, SUM(used) AS count
FROM (
SELECT 'Section one'
, COUNT(*)
FROM ur.agncy
WHERE agncy_id BETWEEN '2283' AND '2329'
UNION ALL
SELECT 'Section two'
, COUNT(*)
FROM ur.agncy
WHERE agncy_id BETWEEN '2922' AND '2994'
) AS q (section , used)
GROUP BY
ROLLUP(section)
;

Reply With Quote
  #6  
Old   
du_bing@hotmail.com
 
Posts: n/a

Default Re: how to sum a derived column - 03-12-2010 , 01:28 PM



On Mar 12, 1:20*pm, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:
Quote:
to get total count from each section first and then get the total counts of all sections

Get "count from each section" and "total counts" at once.

SELECT CASE GROUPING(section)
* * * *WHEN 1 THEN
* * * * * * 'All sctions'
* * * *ELSE section
* * * *END *AS section
* * *, SUM(used) AS count
* FROM (
* * * *SELECT 'Section one'
* * * * * * , COUNT(*)
* * * * *FROM ur.agncy
* * * * WHERE agncy_id BETWEEN '2283' AND '2329'
* * * *UNION ALL
* * * *SELECT 'Section two'
* * * * * * , COUNT(*)
* * * * *FROM ur.agncy
* * * * WHERE agncy_id BETWEEN '2922' AND '2994'
* * * *) AS q (section , used)
*GROUP BY
* * * *ROLLUP(section)
;
Thanks very much of the help. Also really appreciated providing other
options!

Bing

Reply With Quote
  #7  
Old   
Tonkuma
 
Posts: n/a

Default Re: how to sum a derived column - 03-12-2010 , 10:13 PM



On Mar 13, 4:20*am, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:
Quote:
to get total count from each section first and then get the total counts of all sections

Get "count from each section" and "total counts" at once.

Another example(returns one row):
SELECT COUNT(*) AS "All sections"
, COUNT(CASE
WHEN agncy_id BETWEEN '2283' AND '2329' THEN 0
END
) AS "Section one"
, COUNT(CASE
WHEN agncy_id BETWEEN '2922' AND '2994' THEN 0
END
) AS "Section two"
FROM ur.agncy
WHERE agncy_id BETWEEN '2283' AND '2329'
OR agncy_id BETWEEN '2922' AND '2994'
;

Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.