-1

我需要帮助才能获得这种类型的输出:

Create table T1
(Id int identity primary key,
VoucherNo varchar(10),
TransNo varchar(10)
)

Insert into T1 values ('V100','Trns1'),('V101','Trns1'),('V102','Trns1'),('V103','Trns1'),('V104','Trns1'),('V106','Trns1')

结果输出:

TransNo FirsvoucherNo  LastVoucherNo Quantity
trans1    V100     V104  5
trans1   V106      V106   1
4

1 回答 1

1

如果凭证号是固定位数的,可以通过一个简单的GROUP BY查询来解决:

SELECT
    TransNo,
    MIN(VoucherNo) as FirsvoucherNo,
    MAX(VoucherNo) as LastVoucherNo,
    COUNT(*) as Quantity
FROM T1
GROUP BY TransNo

这是关于 sqlfiddle 的演示。

于 2013-06-29T10:39:24.410 回答