SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5502 Visits: 2768
Hi,I required help in creating this type query
Create table #temp (ID int,VoucherNo varchar(10), Status varchar(10), VoucherType int)
Insert into #temp
Values (1,'VVB00001','New',1),(2,'VVB00002','New',1),(3,'VVB00003','Active',1),(4,'VVB00004','Active',1),
(5,'VVB00005','New',1),(6,'VVB00006','New',1)

Required output:
FirstVoucherNo LastVoucher Status VoucherType
VVB00001 VVB00002 New 1
VVB00003 VVB00004 Active 1
VVB00005 VVB00006 New 1

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6748 Visits: 25653
WITH CTE AS (
SELECT ID,VoucherNo,Status,VoucherType,
ROW_NUMBER() OVER(ORDER BY ID) AS rn1,
ROW_NUMBER() OVER(PARTITION BY Status ORDER BY ID) AS rn2
FROM #temp)
SELECT MIN(VoucherNo) AS FirstVoucherNo,
MAX(VoucherNo) AS LastVoucher,
Status,
MIN(VoucherType) AS VoucherType
FROM CTE
GROUP BY Status,rn1-rn2
ORDER BY MIN(rn1);



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5502 Visits: 2768
Mark-101232 (5/9/2013)


WITH CTE AS (
SELECT ID,VoucherNo,Status,VoucherType,
ROW_NUMBER() OVER(ORDER BY ID) AS rn1,
ROW_NUMBER() OVER(PARTITION BY Status ORDER BY ID) AS rn2
FROM #temp)
SELECT MIN(VoucherNo) AS FirstVoucherNo,
MAX(VoucherNo) AS LastVoucher,
Status,
MIN(VoucherType) AS VoucherType
FROM CTE
GROUP BY Status,rn1-rn2
ORDER BY MIN(rn1);



Can you plz explain about this rn1-rn2?

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6748 Visits: 25653
kapil_kk (5/9/2013)
Mark-101232 (5/9/2013)


WITH CTE AS (
SELECT ID,VoucherNo,Status,VoucherType,
ROW_NUMBER() OVER(ORDER BY ID) AS rn1,
ROW_NUMBER() OVER(PARTITION BY Status ORDER BY ID) AS rn2
FROM #temp)
SELECT MIN(VoucherNo) AS FirstVoucherNo,
MAX(VoucherNo) AS LastVoucher,
Status,
MIN(VoucherType) AS VoucherType
FROM CTE
GROUP BY Status,rn1-rn2
ORDER BY MIN(rn1);



Can you plz explain about this rn1-rn2?


Have a look here

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search