如何检查SQL中的Series中的所有数字
CityFrom CityTo AwbnoFrom AwbNoTo
Delhi Mumbai 10 13
delhi Patna 35 38
Delhi Pune 39 41
我想这样输出: -
CityFrom CityTo TotalAwbNo
Delhi Mumbai 10
Delhi Mumbai 11
Delhi Mumbai 12
Delhi Mumbai 13
Delhi Patna 35
Delhi Patna 36
Delhi Patna 37
Delhi Patna 38
Delhi Pune 39
Delhi Pune 40
Delhi Pune 41
答案 0 :(得分:2)
如果您正在使用SQL Server,那么递归CTE
可能会有用。
WITH CTE AS
(
SELECT CityFrom, CityTo, AwbnoFrom, AwbNoTo from table
UNION ALL
SELECT CityFrom, CityTo, AwbnoFrom+1, AwbNoTo
FROM cte
WHERE AwbnoFrom < AwbNoTo
)
SELECT CityFrom, CityTo, AwbnoFrom AS TotalAwbNo
FROM cte
ORDER BY CityFrom, CityTo, AwbnoFrom
OPTION (MAXRECURSION 0)