Hi Kapil,
Can you try this:
SELECT TempID,TransNo,MIN(VoucherNo) AS FirsvoucherNo,MAX(VoucherNo) AS LastVoucherNo,COUNT(TempID) AS Quantity
FROM
(
SELECT *
,CASE WHEN EXISTS (SELECT TOP 1 VoucherNo FROM T1 tmp2 where RIGHT(tmp2.VoucherNo,3) - 1 = RIGHT(tmp.VoucherNo,3) OR RIGHT(tmp2.VoucherNo,3) + 1 = RIGHT(tmp.VoucherNo,3))
THEN 1 ELSE 0 END AS TempID
FROM T1 tmp
) TEST
GROUP BY TempID,TransNo
ORDER BY TempID DESC
Regards Harsh
No comments:
Post a Comment