我们如何计算 PK 并为整个 PK 列表重复此计数?
How can we count the PK and repeat this count for the entire list of PK?
我们如何计算 PK 并为整个 PK 列表重复此计数?
例如:
Table A(包含3000个寄存器,例如)
ID
0001
0002
0003
0004
0005
...
不使用任何过滤器,我们预计:
COUNT(*) | ID
3000 | 0001
3000 | 0002
3000 | 0003
3000 | 0004
3000 | 0005
...
Bt 当使用过滤器限制结果时,COUNT 必须根据屏幕中的结果反映出来,例如:
select ...
where ID IN (0001,0002,0003,0004,0005)
那么应该是:
COUNT(*) | ID
5 | 0001
5 | 0002
5 | 0003
5 | 0004
5 | 0005
这看起来很简单,但我做不到。
我已经尝试使用 rownum
、count(PK)
、max(rownum)
但没有成功。
谢谢
这个?
SQL> select count(*) from emp;
COUNT(*)
----------
12
SQL> select empno, count(*) over (order by null) cnt from emp;
EMPNO CNT
---------- ----------
7369 12
7499 12
7521 12
7566 12
7654 12
7698 12
7782 12
7839 12
7844 12
7900 12
7902 12
7934 12
12 rows selected.
SQL> select empno, count(*) over (order by null) cnt from emp where deptno = 10;
EMPNO CNT
---------- ----------
7782 3
7839 3
7934 3
SQL>
我们如何计算 PK 并为整个 PK 列表重复此计数?
例如:
Table A(包含3000个寄存器,例如)
ID
0001
0002
0003
0004
0005
...
不使用任何过滤器,我们预计:
COUNT(*) | ID
3000 | 0001
3000 | 0002
3000 | 0003
3000 | 0004
3000 | 0005
...
Bt 当使用过滤器限制结果时,COUNT 必须根据屏幕中的结果反映出来,例如:
select ...
where ID IN (0001,0002,0003,0004,0005)
那么应该是:
COUNT(*) | ID
5 | 0001
5 | 0002
5 | 0003
5 | 0004
5 | 0005
这看起来很简单,但我做不到。
我已经尝试使用 rownum
、count(PK)
、max(rownum)
但没有成功。
谢谢
这个?
SQL> select count(*) from emp;
COUNT(*)
----------
12
SQL> select empno, count(*) over (order by null) cnt from emp;
EMPNO CNT
---------- ----------
7369 12
7499 12
7521 12
7566 12
7654 12
7698 12
7782 12
7839 12
7844 12
7900 12
7902 12
7934 12
12 rows selected.
SQL> select empno, count(*) over (order by null) cnt from emp where deptno = 10;
EMPNO CNT
---------- ----------
7782 3
7839 3
7934 3
SQL>