| Author |
Topic |
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2008-08-11 : 10:46:52
|
Hi All,I need to do paging, with the following condition.The number of records in each page, should bea. 5 (max)b. same data (in a particular column) containing records should not go to the next pageThere can be 1~3 records having same data.Here is an example (the column, of which data should not go to next page is Col1):Records would look likeCol1 Col2 Col3aaa pre 123aaa hgg 456aaa sre 223bbb dhd 563bbb dhr 349bbb ehr 385ccc wew 766ddd wyw 761ddd rei 436 I need the data as follows in 3 pagesPage1Col1 Col2 Col3aaa pre 123aaa hgg 456aaa sre 223 Page2Col1 Col2 Col3bbb dhd 563bbb dhr 349bbb ehr 385ccc wew 766 Page3Col1 Col2 Col3ddd wyw 761ddd rei 436 ThanksSrinika |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 11:27:23
|
[code]DECLARE @Sample TABLE (Col1 VARCHAR(3), Col2 VARCHAR(3), Col3 INT)INSERT @SampleSELECT 'aaa', 'pre', 123 UNION ALLSELECT 'aaa', 'hgg', 456 UNION ALLSELECT 'aaa', 'sre', 223 UNION ALLSELECT 'bbb', 'dhd', 563 UNION ALLSELECT 'bbb', 'dhr', 349 UNION ALLSELECT 'bbb', 'ehr', 385 UNION ALLSELECT 'ccc', 'wew', 766 UNION ALLSELECT 'ddd', 'wyw', 761 UNION ALLSELECT 'ddd', 'rei', 436;WITH Yak (Col1, Items, SeqID)AS ( SELECT Col1, COUNT(*), ROW_NUMBER() OVER (ORDER BY Col1) FROM @Sample GROUP BY Col1), cte (Col1, Items, PageItems, SeqID, PageNum)AS ( SELECT Col1, Items, Items, SeqID, 1 FROM Yak WHERE SeqID = 1 UNION ALL SELECT y.Col1, y.Items, CASE WHEN c.PageItems + y.Items <= 5 THEN c.Items + y.Items ELSE y.Items END, y.SeqID, CASE WHEN c.PageItems + y.Items <= 5 THEN c.PageNum ELSE c.PageNum + 1 END FROM cte AS c INNER JOIN Yak AS y ON y.SeqID = c.SeqID + 1)SELECT s.col1, s.col2, s.col3, c.PageNumFROM @Sample as sinner join cte AS c ON c.Col1 = s.Col1[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2008-08-13 : 12:11:15
|
| Thanks Peso,It worked.Srinika |
 |
|
|
|
|
|