0

我遇到的问题是通过查询关联一个包含数字和日期时间值的顺序,并将对 ASC 进行排序,并且还有一个字符串值需要按特定顺序从高到低排序。我能够在使用案例的查询中对优先级本身进行排序,但不能为其他值添加任何其他条件。另一个问题是在运行时决定了它们的排序顺序,因此顺序会改变。

单独排序优先级

SELECT [Task], [Start Date] AS Start_Date, [End Date] AS End_Date, [Priority], [Time Allowance] AS Time_Allowance, [Details] FROM [schedulerData0]
ORDER BY (CASE Priority
 WHEN 'Very High' Then 1  
 WHEN 'High' Then 2 
 WHEN 'Medium' Then 3 
 WHEN 'Low' Then 4 
 WHEN 'Very Low' Then 5
END)

尝试合并案例

SELECT [Task], [Start Date] AS Start_Date, [End Date] AS End_Date, [Priority], [Time Allowance] AS Time_Allowance, [Details] FROM [schedulerData0]
ORDER BY
-- numeric columns
CASE _orderby WHEN 'Time_Allowance' THEN Time_Allowance END ASC;
-- string columns
CASE _orderby Priority
 WHEN Priority = 'Very High' THEN 1,
 WHEN Priority = 'High' THEN 2,
 WHEN Priority = 'Medium' THEN 3,
 WHEN Priority = 'Low' THEN 4,
 WHEN Priority = 'Very Low' THEN 5;
-- datetime columns
CASE _orderby WHEN 'Start_Date' THEN Start_Date END ASC,
CASE _orderby WHEN 'End_Date' THEN End_Date END DESC;
END

当前在下面有声明,其中第一个第二个和第三个选择所声明的属性。因为这将对所有数字数据进行排序,但按字母顺序排序优先级。

SELECT [Task], [Start Date] AS Start_Date, [End Date] AS End_Date, [Priority], [Time Allowance] AS Time_Allowance, [Details] FROM [schedulerData0] ORDER BY [" + firstSelect + "], [" + secondSelect + "], [" + thirdSelect + "]"
4

1 回答 1

0

试试这个。

SELECT [Task], [Start Date] AS Start_Date, [End Date] AS End_Date, [Priority], [Time     Allowance] AS Time_Allowance, [Details] FROM [schedulerData0]
ORDER BY (CASE Priority
  WHEN 'Very High' Then 1  
  WHEN 'High' Then 2 
  WHEN 'Medium' Then 3 
  WHEN 'Low' Then 4 
  WHEN 'Very Low' Then 5
END), [Start Date] ASC, [End Date] DESC
于 2013-01-28T09:52:54.140 回答