I often have to write queries with a fairly complex, constructed column that I will aggregate by. For example:
SELECT
EXTRACT(week FROM to_timestamp("Date Created"/1000)) AS week
...
I know that you cannot use aliases in the GROUP BY clause (this Why doesn't Oracle SQL allow us to use column aliases in GROUP BY clauses? question explains logically why), but is there anything else I can do other than re-doing the column calculation, or am I stuck with this:
SELECT COUNT(*), EXTRACT(week FROM to_timestamp("Date Created"/1000)) AS week
FROM mytable
GROUP BY EXTRACT(week FROM to_timestamp("Date Created"/1000))
Often, I break complexity by using sub-queries.
Divide and conquer approach has paid off pretty well so far.
Update
Alternatives to solving this issue:
Computed columns (as @gbn stated in his answer).
CTEs