-5

更新的问题:

ALTER PROC Proc_Test
(
    @GrpBy as varchar(20) 
)
AS
Declare @TableTax table(
Taxid int,
TaxRange nvarchar(20),
MinLimit int,
MaxLimit int
)

SELECT t1.*     
FROM 
(
    SELECT  
        GroupBy = CASE @GrpBy 
                    WHEN 'JobGrade' THEN Employee.Grade                 
                    WHEN 'Tax Slab' THEN tblTax.TaxRange
                  END, (20 more calculated columns)
    FROM @TableTax AS tblTax, Employee
        LEFT JOIN Grade on Grade.Grade = Employee.Grade     
    WHERE
    Employee.Salary between tblTax.MinLimit and tblTax.Maxlimit     
    GROUP BY    
        CASE @GrpBy
                    WHEN 'JobGrade' THEN Employee.Grade                 
                    WHEN 'Tax Slab' THEN tblTax.TaxRange
                  END   
) AS t1
ORDER BY GroupBy

我想按 TaxId 订购结果。此 order by 仅适用于在前端选择 Tax Slab 的情况。

4

2 回答 2

3

试试这个:

SELECT t1.* FROM t1
INNER JOIN t2
ON t1.JoinColumn = t2.JoinColumn
ORDER BY t2.SortColumn
于 2012-09-14T14:04:02.750 回答
2

您应该添加“where”子句

Select t1.* from t1, t2 where t1.somefield = t2.otherfield order by t2.sortfield
于 2012-09-14T13:49:31.777 回答