2016-04-11 49 views
0

當我運行下面的查詢,我需要使用裝飾功能上的日期, 輸出的順序不正確ORDER BY子句中沒有表現出預期的結果

select trim(man_date_created)as createddate,count(*) recordcount 
    from man 
    where man_date_created>sysdate-15 
    group by trim(man_date_created) ORDER BY createddate; 

這個出來把我從這個讓我查詢

01-APR-16 
02-APR-16 
03-APR-16 
04-APR-16 
05-APR-16 
06-APR-16 
07-APR-16 
08-APR-16 
09-APR-16 
10-APR-16 
11-APR-16 
27-MAR-16 
28-MAR-16 
29-MAR-16 
30-MAR-16 
31-MAR-16 

在這裏你可以看到,在4月11日之後,它顯示3月份的條目。
是否有任何解決方案,以便我不能得到所有狀態的計數?

+0

你想要產生什麼結果? – rhavendc

+0

@rhavendc rsult應該按照某種順序,比如3月31日之後它會從4月1日開始,4月2日...等 – suraha

+0

MySQL或oracle? –

回答

2

您應該將字符串轉換日期

SELECT TO_DATE('12-4-2016','YYYY-MM-DD'); 



select trim(DATE(date,'YYYY-MM-DD'))as createddate,count(*) recordcount 
from man 
where man_date_created>sysdate-15 
group by trim(man_date_created) ORDER BY createddate; 

你的情況試試這個

select DATE(mandate,'YYYY-MM-DD') createddate, count(*) recordcount, 
count(case when man_status = 'A' then 1 end) as a, 
count(case when man_status = 'S' then 1 end) as s, 
count(case when man_status = 'C' then 1 end) as c, 
count(case when man_status = 'R' then 1 end) as r 
from man 
where man_status IN ('A','S','C','R') and mandate>sysdate-15 
group bycreateddate ORDER BY createddate; 
+0

它不工作。 – suraha

+0

你有一個錯誤...,請顯示它..無論如何,我有更新的答案與一個光的建議.. – scaisEdge

+0

她的錯誤它越來越= ORA-00904:「STR_TO_DATE」:無效標識符 00904. 00000 - 「% s:無效的標識符「 *原因: – suraha

1

您必須將字符串轉換爲日期在ORDER BY子句:

select trim(date)as createddate,count(*) recordcount 
from man 
where man_date_created>sysdate-15 
group by trim(man_date_created) ORDER BY TO_DATE(date, 'DD/Month/YYYY'); 
相關問題