如何在sql 2000中获得以下结果?

时间:2013-04-04 09:18:15

标签: sql sql-server-2000

表1

2列

pack no     20  20  20  20  20  20  30  30  30  30  30
Serial no   12  13  14  15  16  17  18  19  20  21  22

我需要的结果是

pack no      20        30
serial no    12-17     18-22

3 个答案:

答案 0 :(得分:0)

如果这些字段都是数字,

SELECT  packNo,
        CAST(min_serial AS VARCHAR(12)) + '-' + CAST(min_serial AS VARCHAR(12)) serial_no
FROM
        (
            SELECT  packNo, 
                    MIN(serialNo) min_serial,
                    MAX(serialNo) max_serial
            FROM    TableName
            GROUP   BY packNo
        ) subtable

答案 1 :(得分:0)

您可以使用min()max()函数来执行此操作。如果在列名中使用下划线而不是空格,那会更好。

<强> FIDDLE DEMO

select [pack no], convert(varchar(10),min([Serial no])) + '-' + 
                  convert(varchar(10),max([Serial no])) as [Serial no]
from yourTable
group by [pack no]

答案 2 :(得分:0)

请尝试:

select 
    [pack no], 
    CAST(MIN([Serial no]) AS NVARCHAR(10))+'-'+CAST(MAX([Serial no]) AS NVARCHAR(10)) as [Serial no]
from 
    YourTable
group by [pack no]