Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to get a specified number of records centered on a specific record in T-SQL

How to get a specified number of records centered on a specific record in T-SQL

Scheduled Pinned Locked Moved Database
databasecsssalestutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2254591
    wrote on last edited by
    #1

    Is is possible to write a query that would retrieve say the 20 records that are centered on a specifed record. In other words if I want orderId=9425 then I want the 5 orders for a particular customer that are less then 9425 and the 5 orders after 9425? I can't just say where orderID between 9420 and 9430 as these orders may not belong to this customer. This customers last order may have been orderID 8550 so would not show up in this query.

    W 1 Reply Last reply
    0
    • U User 2254591

      Is is possible to write a query that would retrieve say the 20 records that are centered on a specifed record. In other words if I want orderId=9425 then I want the 5 orders for a particular customer that are less then 9425 and the 5 orders after 9425? I can't just say where orderID between 9420 and 9430 as these orders may not belong to this customer. This customers last order may have been orderID 8550 so would not show up in this query.

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Hi, One way of doing this could be:

      select *
      from orders a
      where a.customer = 'A'
      and ( a.id in (select top(2) id
      from orders b
      where b.customer = a.customer
      and b.id < 26
      order by b.id desc)
      or
      a.id in (select top(3) id
      from orders b
      where b.customer = a.customer
      and b.id >= 26
      order by b.id asc))

      The above (not tested) should fetch 5 rows. ID 26 and 2 rows from both sides

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups