1

表格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
4

3 回答 3

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
于 2013-04-04T09:21:32.740 回答
0

你可以使用min()max()函数来做到这一点。如果在列名中使用下划线而不是空格会更好。

小提琴演示

select [pack no], convert(varchar(10),min([Serial no])) + '-' + 
                  convert(varchar(10),max([Serial no])) as [Serial no]
from yourTable
group by [pack no]
于 2013-04-04T09:23:38.680 回答
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]
于 2013-04-04T09:25:13.187 回答