表格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
表格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
如果这些字段都是数字,
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
你可以使用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]
请试试:
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]