还是那道有趣的题目

数据记录是这样的
1
2
3
4
5
12
17
18
19
20
25
请问sqlserver2000如何显示成这样
1-5
12
17-20
25


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)
END
FROM (
SELECT *, (num -id) AS iid
FROM (
SELECT (SELECT COUNT(1) FROM @t t1 WHERE t1.num<=t2.num) AS id,
*
FROM @t t2
) t3
) t4
GROUP BY
iid




题目来源

 http://blog.csdn.net/fredrickhu/article/details/6481452

posted @ 2011-07-05 21:45  qanholas  阅读(488)  评论(2编辑  收藏  举报