![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I need to retrieve 1. a total SELECT SUM(COL1) FROM SOME_TABLE 2. most frequent values SELECT COL1, COUNT(*) FROM SOME_TABLE GROUP BY COL1 I want to scan the table only once, so I was trying to accomplish both tasks in one statement. Any ideas? |
#3
| |||
| |||
|
|
You can retrieve both results with a single query, but you can't with a single table scan. |
#4
| |||
| |||
|
|
Lee, You can retrieve both results with a single query, but you can't with a single table scan. how could such a query look like (and don't tell me about UNION ALL)? |
#5
| |||
| |||
|
|
how could such a query look like (and don't tell me about UNION ALL)? |
#6
| |||
| |||
|
|
how could such a query look like (and don't tell me about UNION ALL)? You haven't mentioned which database. So I'm assuming it's ANSI-92 compliant. SELECT S1.COL1 ,S2.SUM_AMT , COUNT(*) FROM SOME_TABLE as S1 ,(SELECT SUM(COL1) as SUM_AMT FROM SOME_TABLE) as S1 GROUP BY S1.COL1 ,S2.SUM_AMT; As Lee pointed out it won't happen in a single scan. Christian. |
#7
| |||
| |||
|
|
Ford Desperado wrote: I want to scan the table only once, so I was trying to accomplish both tasks in one statement. Any ideas? You can retrieve both results with a single query, but you can't with a single table scan. |
#8
| |||
| |||
|
|
christian.maslen (AT) techie (DOT) com (Christian Maslen) wrote in message news:<b9c8cfba.0402230001.5fb27d77 (AT) posting (DOT) google.com>... how could such a query look like (and don't tell me about UNION ALL)? You haven't mentioned which database. So I'm assuming it's ANSI-92 compliant. SELECT S1.COL1 ,S2.SUM_AMT , COUNT(*) FROM SOME_TABLE as S1 ,(SELECT SUM(COL1) as SUM_AMT FROM SOME_TABLE) as S1 GROUP BY S1.COL1 ,S2.SUM_AMT; As Lee pointed out it won't happen in a single scan. Christian. A single scan is possible in Oracle, using a non-ANSI extension to SQL: SQL> select deptno, count(*), sum(count(*)) over () 2 from emp 3 group by deptno; |
#9
| |||
| |||
|
#10
| |||
| |||
|
|
A single scan is possible in Oracle, using a non-ANSI extension to SQL: SQL> select deptno, count(*), sum(count(*)) over () 2 from emp 3 group by deptno; |
![]() |
| Thread Tools | |
| Display Modes | |
| |