In the below query,
SELECT TO_CHAR(hiredate,'MON-YYYY') Year_of_Hiring, COUNT(empno) No_of_Joinees
FROM emp
GROUP BY TO_CHAR(hiredate,'MON-YYYY');
How should I mention the format in ORDER BY clause, if I want to sort the result by hiredate.
You can group by and order by TRUNC(hiredate, 'MM')
and then format it in the SELECT
clause:
SELECT TO_CHAR(TRUNC(hiredate, 'MM'),'MON-YYYY') AS Year_of_Hiring,
COUNT(empno) No_of_Joinees
FROM emp
GROUP BY TRUNC(hiredate, 'MM')
ORDER BY TRUNC(hiredate, 'MM');
ORDER BY
) is (applies to) just eight or sixty rows. That can be fixed with a hint, use_hash_aggregation
.
You can use an aggregation function to order by the original date:
order by min(hiredate)
MON-YYYY
named Year_of_Hiring in the output? That doesn't make a lot of sense, does it? Isn't it rather Month_of_Hiring?