SQL:分组显示相同的值?

时间:2017-06-21 10:09:58

标签: sql group-by oracle-sqldeveloper

我有这个SQL代码:

select count(req_id), SUBMITS from requests where result='REJECT' GROUP BY SUBMITS

我得到的输出是:

1    16-JUN-17
1    19-JUN-17
1    16-JUN-17
1    16-JUN-17

而我想:

3    16-JUN-2017
1    19-JUN-2017

1 个答案:

答案 0 :(得分:2)

您的SUBMITS列中可能有小时,分钟,秒,您的工具没有向您显示;这样,您认为所有行都具有相同的SUBMITS值,但它们只有相同的日期,但时间不同。

如果是这样,您只需要将信息限制为日期,而不是小时,分钟和秒:

select count(req_id), trunc(SUBMITS)
from requests
where result='REJECT'
GROUP BY trunc(SUBMITS)

例如:

SQL> create table requests (submits date, result VARCHAR2(10), req_id NUMBER);

Table created.

SQL> insert into requests (req_id, submits, result) values (1, sysdate, 'REJECT');

1 row created.

SQL> insert into requests (req_id, submits, result) values (2, sysdate, 'REJECT');

1 row created.

SQL> insert into requests (req_id, submits, result) values (3, sysdate, 'REJECT');

1 row created.    

SQL> select count(req_id), trunc(SUBMITS)
  2  from requests
  3  where result='REJECT' 
  4  GROUP BY trunc(SUBMITS);

COUNT(REQ_ID) TRUNC(SUB
------------- ---------
            3 21-JUN-17

SQL> select count(req_id), SUBMITS
  2  from requests
  3  where result='REJECT'
  4  GROUP BY SUBMITS;

COUNT(REQ_ID) SUBMITS
------------- ---------
            1 21-JUN-17
            1 21-JUN-17
            1 21-JUN-17