2017-06-21 78 views
1

我有這樣的SQL代碼: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 
+2

也許你有小時,分鐘和秒你的約會?日期欄的類型是什麼? – Aleksej

+0

這不應該發生。您的SUBMITS列是DATE還是CHAR/VARCHAR類型?如果是最新的,你可能會在其中看到不可見的字符 –

+0

@ThomasG這是一個DATE類型。 – Rishabh

回答

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 
+0

這個工作。謝謝。 :) – Rishabh