我有一个非常简单的要求。
预期结果集:
我需要的是:我想在查询结果集中有序的顶部添加一行。
问题:
SELECT NULL as ProjectId, 'All' as ProjectName
UNION
(
SELECT Project.ProjectId, Project.ProjectName
FROM Project Order by 2
)
条目的原始顺序:
ProjectId ProjectName
24 Beta
56 Alpha
57 Gamma
120 Aap
预期结果集:
ProjectId ProjectName
______________________________
NULL All
120 Aap
56 Alpha
24 Beta
57 Gamma
我需要的是:我想在查询结果集中有序的顶部添加一行。
问题:
- Subquerys are not allowed to have Order By clause
- Doing Top 100 Percent destroys the order, and row having 'All' doesn't come at top
Declaring a Table variable inserting all entries in order and then performing union on this table i.e.
Select NULL as ProjectId, 'All' as ProjectName... UNION select * from @myTable
again destroys the order