SQL总计数另一个计数

时间:2015-04-02 12:00:53

标签: sql oracle

所以我有一张这样的桌子。 。

Table A
| GROUP_NAME | USERID |
| group_A | user1 |
| group_A | user2 |
| group_B | user3 |
| group_A | user4 |
| group_B | user5 |
| group_C | user6 |
| group_B | user7 |
| group_C | user8 |
| group_C | user9 |
| group_A | user10 |

我想要的是任何特定组中的用户ID总数小于或大于某个数字的总行数。

我能来的最接近的是:

select count(distinct group_name)
from Table_A
group by userid having count(*) < 5;

。 。 。但这给了我一个单独的行,每个结果。

我想要的是返回的所有行的总数。

这适用于Oracle数据库中的表。

4 个答案:

答案 0 :(得分:2)

一种方法是使用 COUNT()OVER()分析函数。

例如,

<强>设置

SQL> CREATE TABLE t
  2      (GROUP_NAME varchar2(7), USERID varchar2(6))
  3  ;

Table created.

SQL>
SQL> INSERT ALL
  2      INTO t (GROUP_NAME, USERID)
  3           VALUES ('group_A', 'user1')
  4      INTO t (GROUP_NAME, USERID)
  5           VALUES ('group_A', 'user2')
  6      INTO t (GROUP_NAME, USERID)
  7           VALUES ('group_B', 'user3')
  8      INTO t (GROUP_NAME, USERID)
  9           VALUES ('group_A', 'user4')
 10      INTO t (GROUP_NAME, USERID)
 11           VALUES ('group_B', 'user5')
 12      INTO t (GROUP_NAME, USERID)
 13           VALUES ('group_C', 'user6')
 14      INTO t (GROUP_NAME, USERID)
 15           VALUES ('group_B', 'user7')
 16      INTO t (GROUP_NAME, USERID)
 17           VALUES ('group_C', 'user8')
 18      INTO t (GROUP_NAME, USERID)
 19           VALUES ('group_C', 'user9')
 20      INTO t (GROUP_NAME, USERID)
 21           VALUES ('group_A', 'user10')
 22  SELECT * FROM dual
 23  ;

10 rows created.

SQL>
SQL> COMMIT;

Commit complete.

SQL>

<强>查询

SQL> SELECT t.*,
  2      COUNT(GROUP_NAME) OVER(PARTITION BY GROUP_NAME ORDER BY GROUP_NAME) cnt
  3    FROM t;

GROUP_N USERID        CNT
------- ------ ----------
group_A user10          4
group_A user2           4
group_A user1           4
group_A user4           4
group_B user3           3
group_B user5           3
group_B user7           3
group_C user8           3
group_C user9           3
group_C user6           3

10 rows selected.

SQL>

过滤输出数量少于所需数量的行:

SQL> WITH DATA AS
  2    (SELECT t.*,
  3      COUNT(GROUP_NAME) OVER(PARTITION BY GROUP_NAME ORDER BY GROUP_NAME) cnt
  4    FROM t
  5    )
  6  SELECT GROUP_NAME, USERID FROM DATA WHERE cnt < 4;

GROUP_N USERID
------- ------
group_B user3
group_B user5
group_B user7
group_C user8
group_C user9
group_C user6

6 rows selected.

SQL>

更新根据评论中的OP请求:

SQL> WITH DATA AS
  2    (SELECT t.*,
  3      COUNT(GROUP_NAME) OVER(PARTITION BY GROUP_NAME ORDER BY GROUP_NAME) cnt
  4    FROM t
  5    )
  6  SELECT count(DISTINCT group_name) cnt FROM DATA WHERE cnt < 4;

       CNT
----------
         2

SQL>

答案 1 :(得分:1)

  select count(distinct group_name) from Table_A  having count(*) < 5;

答案 2 :(得分:1)

我认为你想要通过group_name进行分组:

select count(1) as nr_of_users, group_name 
  from table_a 
 group by group_name 
having count(1) < 4

如果要总计nr行,请将其用作子查询:

select count(1) from (
    select count(1) as nr_of_users, group_name 
      from table_a 
     group by group_name 
    having count(1) < 4
) as a

答案 3 :(得分:1)

如果您希望组中的用户总数小于5(例如),请使用

SELECT group_name,
       COUNT(userid)
  FROM table_a
 GROUP BY group_name
HAVING COUNT(userid) < 5;

对于不同用户的总数,请使用

SELECT group_name,
       COUNT(DISTINCT userid)
  FROM table_a
 GROUP BY group_name
HAVING COUNT(DISTINCT userid) < 5;

从上面的查询返回的总行数,然后使用

SELECT COUNT(1) 
  FROM (SELECT group_name,
               COUNT(DISTINCT userid)
          FROM table_a
         GROUP BY group_name
        HAVING COUNT(DISTINCT userid) < 5);