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. Only include the latest rows

Only include the latest rows

Scheduled Pinned Locked Moved Database
questionsaleshelp
6 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
    Tony Pottier
    wrote on last edited by
    #1

    Probably a dumb question, but I can't figure this out. Suppose I have 2 tables: ORDERS(id,cust_id,date,price) CUSTOMERS(cust_id,name) I want to know how much money the customer #3 spent, and how many orders he did, okay: SELECT COUNT(*), SUM(price) FROM orders WHERE cust_id=3 So far so good, now I want to know this, but I want to narrow it down to the latest 50 orders he did (or to the latest x orders if x < 50), how can I do that? SELECT COUNT(*), SUM(price) WHERE cust_id=3 ORDER BY date DESC LIMIT 50 doesn't work, because there's still only 1 row returned. I'm a bit clueless :confused: Thanks for your help!

    W 1 Reply Last reply
    0
    • T Tony Pottier

      Probably a dumb question, but I can't figure this out. Suppose I have 2 tables: ORDERS(id,cust_id,date,price) CUSTOMERS(cust_id,name) I want to know how much money the customer #3 spent, and how many orders he did, okay: SELECT COUNT(*), SUM(price) FROM orders WHERE cust_id=3 So far so good, now I want to know this, but I want to narrow it down to the latest 50 orders he did (or to the latest x orders if x < 50), how can I do that? SELECT COUNT(*), SUM(price) WHERE cust_id=3 ORDER BY date DESC LIMIT 50 doesn't work, because there's still only 1 row returned. I'm a bit clueless :confused: Thanks for your help!

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

      Limit doesn't work because you are actually groupping everything into a single row. If you want to restrict the data that goes into the group operation, you have to do it in where or for example use inline views. If you want to use where condition, it could be something like:

      SELECT COUNT(*), SUM(price)
      FROM Orders o1
      WHERE cust_id=3
      AND 50 <= (select count(*)
      from Orders o2
      where o2.cust_id = o1.cust_id
      and o2.date < o1.date)

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

      T 1 Reply Last reply
      0
      • W Wendelius

        Limit doesn't work because you are actually groupping everything into a single row. If you want to restrict the data that goes into the group operation, you have to do it in where or for example use inline views. If you want to use where condition, it could be something like:

        SELECT COUNT(*), SUM(price)
        FROM Orders o1
        WHERE cust_id=3
        AND 50 <= (select count(*)
        from Orders o2
        where o2.cust_id = o1.cust_id
        and o2.date < o1.date)

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

        T Offline
        T Offline
        Tony Pottier
        wrote on last edited by
        #3

        I see. Is subquerying the only solution? I'm not sure this is going to be run very fast.

        W 1 Reply Last reply
        0
        • T Tony Pottier

          I see. Is subquerying the only solution? I'm not sure this is going to be run very fast.

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

          Tony_P wrote:

          Is subquerying the only solution

          No it's not. I think there are several ways. Depending on your indexing an inline view may be faster. Then it would be something like:

          SELECT COUNT(*), SUM(price)
          FROM (SELECT TOP 50 Price
          FROM Orders
          WHERE cust_id=3
          ORDER BY date DESC) tableAlias

          Check the execution plan to see which one is better in your case. Additionally add relevant indexes if you need.

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

          T 1 Reply Last reply
          0
          • W Wendelius

            Tony_P wrote:

            Is subquerying the only solution

            No it's not. I think there are several ways. Depending on your indexing an inline view may be faster. Then it would be something like:

            SELECT COUNT(*), SUM(price)
            FROM (SELECT TOP 50 Price
            FROM Orders
            WHERE cust_id=3
            ORDER BY date DESC) tableAlias

            Check the execution plan to see which one is better in your case. Additionally add relevant indexes if you need.

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

            T Offline
            T Offline
            Tony Pottier
            wrote on last edited by
            #5

            Ha right I like this solution. I'm going to benchmark both and see how it goes. Thank you!

            W 1 Reply Last reply
            0
            • T Tony Pottier

              Ha right I like this solution. I'm going to benchmark both and see how it goes. Thank you!

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

              You're welcome.

              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