You cannot select the full hire_date
if you group only the last four digits. Think about what happens if you have two lines:
hire_date ========= 01/01/2001 02/02/2001
On one line generated during grouping, what should hire_date
be hire_date
?
Each selected column must be either a column or an aggregate column. In other words, try:
select substr(hire_date,-4), count(*) from employees group by substr(hire_date,-4) order by empl.hire_date;
I should mention that some string functions are notoriously poor at scaling. If you want to process a year a lot, you should consider dividing it into your column. This can significantly improve performance, but measure, don't guess!
And, as others have already mentioned in the comments, substr
is probably not the best solution, as it may depend on the language (as in: it is possible that the date will be formatted as YYYY-MM-DD
, which will not work well with substring
).
It is better to use something like to_char(hire_date,'YYYY')
or extract (year from hire_date)
, which should be more reliable.
paxdiablo
source share