0

我有一个看起来像这样的数据

| FormCode | Description          | MenuPath    |
+----------+----------------------+-------------+
|   PY     | Payroll              |             |
|   PD     | Personal Development |             | 
|   EM     | EmployeeMaster       |     PY      |
|   EB     | EmployeeBank         |     PY      |
|   ED     | EmployeeData         |     PY      |
| Report   | Report               |     PY      |
|  Badge   | Badge Report         |  PY/Report  |
| Process  | Process              |     PY      |
|  Bonus   | Bonus Component      |    PY/ED    | 
|Preference| Preference           |     PY      |
|   Tax    | Tax Result           |    PY/ED    |
|FinalBonus| FinalBonus           | PY/ED/Bonus |
| Annual   | Annual Process       | PY/Process  |
|BonusbyCC | Bonus by Cost Centre | PY/ED/Bonus |
| Period   | Period               |PY/Preference|

我希望这个数据排序如下所示。

| FormCode | Description          | MenuPath    |
+----------+----------------------+-------------+
|   PY     | Payroll              |             |
|   EB     | EmployeeBank         |     PY      |
|   ED     | EmployeeData         |     PY      |
|  Bonus   | Bonus Component      |    PY/ED    |
|BonusbyCC | Bonus by Cost Centre | PY/ED/Bonus |
|FinalBonus| FinalBonus           | PY/ED/Bonus |
|   Tax    | Tax Result           |    PY/ED    |
|   EM     | EmployeeMaster       |     PY      |
|Preference| Preference           |     PY      |
| Period   | Period               |PY/Preference|
| Process  | Process              |     PY      |
| Annual   | Annual Process       | PY/Process  |
| Report   | Report               |     PY      |
|  Badge   | Badge Report         |  PY/Report  |
|   PD     | Personal Development |             | 

这是排序的结果

Explanation : 

我将尝试解释排序。首先我们看一下formcode。它会将 PY 放在第一行。之后,将出现 MenuPath 为 PY 的行(按描述中的字母排序)。在这种情况下,EmployeeBank 首先显示。如果它没有任何子路径,则下一行将是 EmployeeData。之后,EmployeeData 有了一个孩子,我们可以看到下一行有 Bonus Component(sort by Description)。之后,如果没有子项,则下一行将是 Tax Result,但正如您所见,Bonus Component 仍然有一个子项(Bonus by Cost Center 和 FinalBonus),因此这 2 个数据将出现在 Bonus Component 之后。税务结果将出现在下一个。该过程将保持这种状态,直到数据结束。

对不起,如果我的英语搞砸了,我的解释也不好。如果有人理解我的意思并且可以在解释上修正我的英语,请这样做

谢谢您的帮助

4

2 回答 2

0
SELECT * FROM Table1
ORDER BY CASE [FormCode]
WHEN 'PY' THEN 1
WHEN 'EM' THEN 2
WHEN 'EB' THEN 3
WHEN 'ED' THEN 4
WHEN 'Bonus' THEN 5
WHEN 'BonusbyCC' THEN 6
WHEN 'FinalBonus' THEN 7
WHEN 'Tax' THEN 8
WHEN 'Preference' THEN 9
WHEN 'Period' THEN 10
WHEN 'Process' THEN 11
WHEN 'Annual' THEN 12
WHEN 'Report' THEN 13
WHEN 'Badge' THEN 14
WHEN 'PD' THEN 15
END;

样品小提琴

于 2013-10-01T07:07:38.950 回答
0

绝对没有逻辑可以按照您向我们展示的方式进行排序..但是您可以通过为您的字段分配某种 ID 然后对其进行排序来实现

于 2013-10-01T07:03:05.397 回答