![]() | |
#11
| |||
| |||
|
|
"Tony" <andrewst (AT) onetel (DOT) net.uk> wrote in message news:c0e3f26e.0402230443.605ca7e9 (AT) posting (DOT) google.com... 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; If he were summing counts and not col1's, that might actually work. |
#12
| |||
| |||
|
|
"Tony" <andrewst (AT) onetel (DOT) net.uk> wrote in message news:c0e3f26e.0402230443.605ca7e9 (AT) posting (DOT) google.com... 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; If he were summing counts and not col1's, that might actually work. |
#13
| |||
| |||
|
|
"Bob Badour" <bbadour (AT) golden (DOT) net> wrote "Tony" <andrewst (AT) onetel (DOT) net.uk> wrote in message news:c0e3f26e.0402230443.605ca7e9 (AT) posting (DOT) google.com... 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; If he were summing counts and not col1's, that might actually work. Damn! Wish I knew how to "recall" a post! I just responded to this with another wrong solution, but now I think I have the correct solution. However, before this one appears you will see my 2nd wrong solution and respond to that... |
|
My mistake was treating the sum(sal) as if it operated on the base table rather than on the result set after grouping. I believe this finally fixes it and still only requires one scan of the table: SQL> select sal, cnt, sum(sal*cnt) over () totsal 2 from 3 ( 4 select sal, count(*) cnt 5 from emp 6 group by sal 7 ); SAL CNT TOTSAL ---------- ---------- ---------- 800 1 29025 950 1 29025 1100 1 29025 1250 2 29025 1300 1 29025 1500 1 29025 1600 1 29025 2450 1 29025 2850 1 29025 2975 1 29025 3000 2 29025 5000 1 29025 12 rows selected. |
![]() |
| Thread Tools | |
| Display Modes | |
| |