【发布时间】:2022-11-02 19:44:00
【问题描述】:
我有一个包含以下数据的表,我需要为 TSQL 中的每一行计算一个排序索引(整数)
| type | code |
|---|---|
| Fruit | 030 |
| Fruit | 040 |
| Fruit | Banana |
| Fruit | Apple 1 |
| Fruit | Apple 2 |
| Soda | 050 |
| Soda | 1 |
| Soda | 054 |
| Soda | Sprite |
| Soda | Fanta |
下面的 sort_index 列应按类型(每种类型从 1 开始)和可解析整数代码始终优先于字母数字代码的代码计算:
| type | code | sort_index |
|---|---|---|
| Fruit | 030 | 1 |
| Fruit | 040 | 2 |
| Fruit | Apple 1 | 3 |
| Fruit | Apple 2 | 4 |
| Fruit | Banana | 5 |
| Soda | 1 | 1 |
| Soda | 050 | 2 |
| Soda | 054 | 3 |
| Soda | Fanta | 4 |
| Soda | Sprite | 5 |
任何帮助将不胜感激。
【问题讨论】:
-
数据/规则太乱了。在将数据导入 SQL Server 之前,您应该定义
sort_index。 -
不幸的是,我真的无法做到这一点。在我发布这个问题后,我找到了答案。您可以在下面查看它 - 也许它会使问题更有意义。