Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何包含组计数?_Sql_Oracle_Oracle11g - Fatal编程技术网

Sql 如何包含组计数?

Sql 如何包含组计数?,sql,oracle,oracle11g,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

第一条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:没有一个组函数
。如何添加包含每个组中的行数的列

我的水晶球说你的意思是

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

我的水晶球说你的意思是

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

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

您可以这样做:

  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.
但是,这不会返回任何其他列…,所以您可以使用分析来处理它 (注:此答案与Sander的答案相同)

结果:

              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.
        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.
但是,这会为每个组返回重复的计数值(因此,如果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        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.
        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_数据”只是我创建的一些“假”数据)

基于您的单一需求: “添加一列,其中包含每个组中的行数?”

您可以这样做:

  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.
但是,这不会返回任何其他列…,所以您可以使用分析来处理它 (注:此答案与Sander的答案相同)

结果:

              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.
        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.
但是,这会为每个组返回重复的计数值(因此,如果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        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.
        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_数据”只是我创建的一些“假”数据)

您能给我们展示一些示例数据和预期输出吗?首先,
count(1)
对性能没有影响,所以使用
count(*)
也没关系。其次,count as aggregate_表达式希望您在选择列表中提到的列组成一个组,但是,我首先希望您提供一个测试用例。要对每行进行运行计数,请使用分析count()OVER()。您能向我们展示一些示例数据和预期输出吗?首先,
count(1)
对性能没有影响,所以使用
count(*)
也没关系。其次,count as aggregate_表达式希望您在我回答的选择列表中提到的列组成一个组,但是,我首先希望您提供一个测试用例。要对每行进行运行计数,请使用解析count()OVER()Nice,+1。正是我建议的。很好,+1。正是我的建议。