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. SQL select up to the last 6 dates worth of an order

SQL select up to the last 6 dates worth of an order

Scheduled Pinned Locked Moved Database
databasequestion
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.
  • T Offline
    T Offline
    totig
    wrote on last edited by
    #1

    Hi, assume I have two tables called Orders (ID, DateCreated) and a child table called OrderDetails (ID, OrderID, ProductID, Quantity) What I need to do is calculate the average last 6 orders. Now if there are only 5 dates worth of orders I would need to know this value as I need to divide the sum(Quantity) by the number of orders - which cannot be greater than 6 previous orders. I have tried a few different select statements, but cannot seem to get the result I am looking for. Oh one other thing, I would be selecting these records by ProductID. Any suggestions at all? Thanks

    M 1 Reply Last reply
    0
    • T totig

      Hi, assume I have two tables called Orders (ID, DateCreated) and a child table called OrderDetails (ID, OrderID, ProductID, Quantity) What I need to do is calculate the average last 6 orders. Now if there are only 5 dates worth of orders I would need to know this value as I need to divide the sum(Quantity) by the number of orders - which cannot be greater than 6 previous orders. I have tried a few different select statements, but cannot seem to get the result I am looking for. Oh one other thing, I would be selecting these records by ProductID. Any suggestions at all? Thanks

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      I am not quite sure what you want your result set to look like. It would be easier if you gave a list of the resultant columns. Are you looking for the last 6 orders of a particular product or simply the last 6 orders?

      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