数据记录是这样的
1 2 3 4 5 12 17 18 19 20 25 请问sqlserver2000如何显示成这样1-51217-2025 DECLARE @t TABLE (num INT ) INSERT INTO @t SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 12 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 25
SELECT CASE WHEN MIN(num) = MAX(num) THEN CAST(MIN(num) AS VARCHAR)ELSE CAST(MIN(num) AS VARCHAR) + '-' + CAST(MAX(num) AS VARCHAR)ENDFROM (SELECT *, (num -id) AS iidFROM (SELECT (SELECT COUNT(1) FROM @t t1 WHERE t1.num<=t2.num) AS id,*FROM @t t2 ) t3 ) t4GROUP BY iid
题目来源