I have a sql query that unions two recordsets. It works when I don't try to adjust the ORDER BY clause using the replace function. However, I want to ensure that a certain item in the list always floats to the top. Can someone help me?
Below is the SQL that does not run. Error is "Invalid column name 'name':
Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace([name],'PRIME','AAA')
Also tried this but got error "ORDER BY items must appear in the select list if the statement contains a UNION operator.":
Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIM E','AAA')
Below is the SQL that works:
Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
[name]
Below is the SQL that does not run. Error is "Invalid column name 'name':
Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace([name],'PRIME','AAA')
Also tried this but got error "ORDER BY items must appear in the select list if the statement contains a UNION operator.":
Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
replace(2,'PRIM E','AAA')
Below is the SQL that works:
Select 0 as [subcontractorID], 'PRIME' as [name]
UNION
Select
subcontractor.s ubcontractorID, subcontractor.[name]
from
subcontractor
order by
[name]
Comment