Guest User

Untitled

a guest
Jun 19th, 2013
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. SELECT TOP 10 * FROM Product
  2.  
  3. WITH T AS
  4. (
  5. SELECT TOP 20 name,
  6. row_number() OVER (ORDER BY id) AS RN
  7. FROM Products
  8. ORDER BY id
  9. )
  10. SELECT
  11. MAX(CASE WHEN RN <=10 THEN name END) AS Col1,
  12. MAX(CASE WHEN RN > 10 THEN name END) AS Col2
  13. FROM T
  14. GROUP BY RN % 10
  15.  
  16. ;WITH TopProducts AS
  17. (
  18. SELECT
  19. ProductID, ProductName,
  20. ROW_NUMBER() OVER(ORDER BY --some-column-here-- DESC) 'RN'
  21. FROM dbo.Products
  22. )
  23. SELECT
  24. p1.ProductID, p1.ProductName,
  25. p2.ProductID, p2.ProductName
  26. FROM
  27. TopProducts p1
  28. CROSS JOIN
  29. TopProducts p2
  30. WHERE
  31. p1.RN BETWEEN 1 AND 10 -- get rows 1-10 from the first CTE
  32. -- AND p2.RN BETWEEN 11 AND 20 redundant, as niktrs pointed out
  33. AND p1.RN + 10 = p2.RN -- join rows from P1 and P2 so you don't get a cartesian product
  34.  
  35. select *
  36. from
  37. (
  38. SELECT top 10 ROW_NUMBER() OVER(ORDER BY product) linenum, product
  39. FROM products
  40. ) t1
  41. JOIN
  42. (
  43. SELECT top 20 ROW_NUMBER() OVER(ORDER BY product) linenum, product
  44. FROM products
  45. ) t2 ON t1.linenum+10 = t2.linenum
  46.  
  47. SELECT *
  48. FROM
  49. (
  50. SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY ColName) RowNo, ColName
  51. FROM TableName
  52. ) table1
  53. INNER JOIN
  54. (
  55. SELECT TOP 20 ROW_NUMBER() OVER(ORDER BY ColName) RowNo, ColName
  56. FROM TableName
  57. ) table2 ON table1.RowNo + 10 = table2.RowNo
  58.  
  59. SELECT * FROM Product LIMIT(10,10)
Advertisement
Add Comment
Please, Sign In to add comment