我如何包括小组计数?

时间:2015-04-21 14:42:42

标签: sql oracle oracle11g

第一个SQL语句按照预期将col5的结果集分组。我想在组中包含每行的计数,因此我使用第二个SQL语句。

SELECT col1, col2, col3, col4, Q1.col5, col6
FROM TABLE_NAME, 
   (SELECT col5 FROM TABLE_NAME WHERE col6 = 123456 GROUP BY col5) Q1
WHERE col6 = 123456 AND TABLE_NAME.col5 = Q1.col5;


SELECT count(1), col1, col2, col3, col4, Q1.col5, col6
FROM TABLE_NAME, 
   (SELECT col5 FROM TABLE_NAME WHERE col6 = 123456 GROUP BY col5) Q1
WHERE col6 = 123456 AND TABLE_NAME.col5 = Q1.col5;

当我运行第二个语句时,我得到ORA-00937: not a single-group group function。如何添加包含每个组中行数的列?

2 个答案:

答案 0 :(得分:3)

我的水晶球说你的意思是

select col1, col2, col3, col4, col5, 
       count(*) over (partition by col5)
from table_name
where col6 = 123456

答案 1 :(得分:3)

根据您的单一要求: “添加一个包含每个组中行数的列?”

你可以这样做:

  with w_data as (
           select 123 col1, 234 col2, 345 col3, 123 col4, 345 col5, 123456 col6 from dual union all
           select 123 col1, 345 col2, 456 col3, 123 col4, 345 col5, 123456 col6 from dual union all
           select 234 col1, 456 col2, 567 col3, 123 col4, 456 col5, 123456 col6 from dual union all
           select 234 col1, 456 col2, 678 col3, 123 col4, 456 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 678 col3, 123 col4, 678 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 789 col3, 123 col4, 678 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 890 col3, 123 col4, 678 col5, 123456 col6 from dual
           )
  select col5, count(*)
    from w_data
   where col6 = 123456
   group by col5
   order by col5
  /

结果

        COL5   COUNT(*)
  ---------- ----------
         345          2
         456          2
         678          3

  3 rows selected.

但是,这不会返回任何其他列...,因此您可以使用分析来处理它 (注意:这个与桑德的答案相同)

  with w_data as (
           select 123 col1, 234 col2, 345 col3, 123 col4, 345 col5, 123456 col6 from dual union all
           select 123 col1, 345 col2, 456 col3, 123 col4, 345 col5, 123456 col6 from dual union all
           select 234 col1, 456 col2, 567 col3, 123 col4, 456 col5, 123456 col6 from dual union all
           select 234 col1, 456 col2, 678 col3, 123 col4, 456 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 678 col3, 123 col4, 678 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 789 col3, 123 col4, 678 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 890 col3, 123 col4, 678 col5, 123456 col6 from dual
           )
  select col1, col2, col3, col4, col5, col6,
         count(*) over (partition by col5) cnt
    from w_data
   where col6 = 123456
  /

结果:

              COL1       COL2       COL3       COL4       COL5       COL6        CNT
  ---------- ---------- ---------- ---------- ---------- ---------- ----------
         123        234        345        123        345     123456          2
         123        345        456        123        345     123456          2
         234        456        567        123        456     123456          2
         234        456        678        123        456     123456          2
         345        567        678        123        678     123456          3
         345        567        789        123        678     123456          3
         345        567        890        123        678     123456          3

  7 rows selected.

然而,它返回每个组的重复计数值(因此,如果col5 = 345的组有2行,则计数为2,2行显示计数为2。)

如果你真的只想要每组中的最新,最新等行......你也可以更进一步:

  with w_data as (
           select 123 col1, 234 col2, 345 col3, 123 col4, 345 col5, 123456 col6 from dual union all
           select 123 col1, 345 col2, 456 col3, 123 col4, 345 col5, 123456 col6 from dual union all
           select 234 col1, 456 col2, 567 col3, 123 col4, 456 col5, 123456 col6 from dual union all
           select 234 col1, 456 col2, 678 col3, 123 col4, 456 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 678 col3, 123 col4, 678 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 789 col3, 123 col4, 678 col5, 123456 col6 from dual union all
           select 345 col1, 567 col2, 890 col3, 123 col4, 678 col5, 123456 col6 from dual
           ),
     w_sub as (
        select col1, col2, col3, col4, col5, col6,
               count(*) over (partition by col5) cnt,
               row_number() over (partition by col5 order by col1 desc, col2 desc, col3 desc ) rnum
          from w_data
         where col6 = 123456
       )
  select col1, col2, col3, col4, col5, col6, cnt
    from w_sub
   where rnum = 1
  /

结果:

        COL1       COL2       COL3       COL4       COL5       COL6        CNT
  ---------- ---------- ---------- ---------- ---------- ---------- ----------
         123        345        456        123        345     123456          2
         234        456        678        123        456     123456          2
         345        567        890        123        678     123456          3

  3 rows selected.

(在上述所有查询中,“w_data”只是我创建的一些“虚假”数据)