I'm trying to run the following query and I'm getting this error:
Msg 1033, Level 15, State 1, Line 14
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
Here is the query:
WITH cbt_users AS
(
SELECT
mo, eid, rate, cat, quantity AS cbt_hours
FROM
crew.crew_rates
WHERE
rate_type = 'HOMSTUDY_HRS'
),
tot_hrs AS
(
SELECT
mo, eid, rate, quantity AS tot_hrs
FROM
crew.crew_rates
WHERE
rate_type = 'TOT HRS'
),
GU_cbt_compare AS
(
SELECT
cbt_users.mo, cbt_users.eid, cbt_users.rate, cbt_hours, tot_hrs,
CASE WHEN cbt_users.cat = 'REG' THEN 72 ELSE 75 END AS min_GU
FROM
cbt_users
LEFT JOIN
tot_hrs ON cbt_users.mo = tot_hrs.mo AND cbt_users.eid = tot_hrs.eid
ORDER BY
cbt_users.mo, cbt_users.eid
)
SELECT
mo, eid, rate, cbt_hours, tot_hrs, min_GU
FROM
GU_cbt_compare
ORDER BY
mo, eid