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 calculate average of qty sold the last 6 months or less

How to calculate average of qty sold the last 6 months or less

Scheduled Pinned Locked Moved Database
cssdatabasehelptutorialquestion
8 Posts 4 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.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    Table [Sold]

    ID PID Qty Date


    1 1 8 jun 1, 2010
    2 3 5 jul 1, 2010
    3 3 2 aug 1, 2010
    4 1 1 sep 1, 2010
    5 3 4 sep 30, 2010
    6 2 3 oct 8, 2011
    7 2 5 nov 1, 2011
    8 3 2 dec 1, 2011
    9 1 8 jan 1, 2011
    10 2 5 feb 1, 2011

    Hi all, I have difficulty composing a rather complex (for me) query. I would like to know the average qty sold of each PID per day based on the last 6-month data. If data is less than 6 month, then adjust accordingly. So on the data above: Today: Mar 24, 2011 6-month cut-off: Sep 24, 2010 I want to get:

    PID Qty/day


    1 8/182 = 0.0440
    2 (3+5+5)/168 = 0.0774 -> 168: because the oldest sale is oct 8
    3 (4+2)/182 = 0.0330

    Is there any way I can do this? Somebody help me, please? Thanks in advance.

    C M 2 Replies Last reply
    0
    • J Joe Smith IX

      Table [Sold]

      ID PID Qty Date


      1 1 8 jun 1, 2010
      2 3 5 jul 1, 2010
      3 3 2 aug 1, 2010
      4 1 1 sep 1, 2010
      5 3 4 sep 30, 2010
      6 2 3 oct 8, 2011
      7 2 5 nov 1, 2011
      8 3 2 dec 1, 2011
      9 1 8 jan 1, 2011
      10 2 5 feb 1, 2011

      Hi all, I have difficulty composing a rather complex (for me) query. I would like to know the average qty sold of each PID per day based on the last 6-month data. If data is less than 6 month, then adjust accordingly. So on the data above: Today: Mar 24, 2011 6-month cut-off: Sep 24, 2010 I want to get:

      PID Qty/day


      1 8/182 = 0.0440
      2 (3+5+5)/168 = 0.0774 -> 168: because the oldest sale is oct 8
      3 (4+2)/182 = 0.0330

      Is there any way I can do this? Somebody help me, please? Thanks in advance.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      select PID
      ,sum(qty)/(sysdate-min(date)) <<-- fixed this to account for sign.
      from (
      select PID
      ,qty
      ,date
      from your_table
      where date >= sysdate - 182
      )
      group by PID;

      :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      modified on Thursday, March 24, 2011 9:13 AM

      J K 2 Replies Last reply
      0
      • J Joe Smith IX

        Table [Sold]

        ID PID Qty Date


        1 1 8 jun 1, 2010
        2 3 5 jul 1, 2010
        3 3 2 aug 1, 2010
        4 1 1 sep 1, 2010
        5 3 4 sep 30, 2010
        6 2 3 oct 8, 2011
        7 2 5 nov 1, 2011
        8 3 2 dec 1, 2011
        9 1 8 jan 1, 2011
        10 2 5 feb 1, 2011

        Hi all, I have difficulty composing a rather complex (for me) query. I would like to know the average qty sold of each PID per day based on the last 6-month data. If data is less than 6 month, then adjust accordingly. So on the data above: Today: Mar 24, 2011 6-month cut-off: Sep 24, 2010 I want to get:

        PID Qty/day


        1 8/182 = 0.0440
        2 (3+5+5)/168 = 0.0774 -> 168: because the oldest sale is oct 8
        3 (4+2)/182 = 0.0330

        Is there any way I can do this? Somebody help me, please? Thanks in advance.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Assuming you are using you are using sql server

        SELECT
        PID,
        AVG(qty)/DATEDIFF (d,MAX(date), MIN(date))
        FROM TableName
        WHERE date > DATEADD(-6, GETDATE())
        GROUP BY pid

        The where clause may not be as accurate as you need. I build this by breaking the requirements down into parts Get the average and no of days for each pid do the calc Then I combined the 2 queries. Note this is untested code.

        Never underestimate the power of human stupidity RAH

        J 1 Reply Last reply
        0
        • M Mycroft Holmes

          Assuming you are using you are using sql server

          SELECT
          PID,
          AVG(qty)/DATEDIFF (d,MAX(date), MIN(date))
          FROM TableName
          WHERE date > DATEADD(-6, GETDATE())
          GROUP BY pid

          The where clause may not be as accurate as you need. I build this by breaking the requirements down into parts Get the average and no of days for each pid do the calc Then I combined the 2 queries. Note this is untested code.

          Never underestimate the power of human stupidity RAH

          J Offline
          J Offline
          Joe Smith IX
          wrote on last edited by
          #4

          Thanks a lot. Your answer pointed me to the answer I am looking for.

          1 Reply Last reply
          0
          • C Chris Meech

            select PID
            ,sum(qty)/(sysdate-min(date)) <<-- fixed this to account for sign.
            from (
            select PID
            ,qty
            ,date
            from your_table
            where date >= sysdate - 182
            )
            group by PID;

            :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            modified on Thursday, March 24, 2011 9:13 AM

            J Offline
            J Offline
            Joe Smith IX
            wrote on last edited by
            #5

            Thanks a lot. Your answer pointed me to the answer I am looking for.

            1 Reply Last reply
            0
            • C Chris Meech

              select PID
              ,sum(qty)/(sysdate-min(date)) <<-- fixed this to account for sign.
              from (
              select PID
              ,qty
              ,date
              from your_table
              where date >= sysdate - 182
              )
              group by PID;

              :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              modified on Thursday, March 24, 2011 9:13 AM

              K Offline
              K Offline
              Klaus Werner Konrad
              wrote on last edited by
              #6

              If there is only one entry for a PID and the sell was today, then date == sysdate --> Division by zero ...

              C 1 Reply Last reply
              0
              • K Klaus Werner Konrad

                If there is only one entry for a PID and the sell was today, then date == sysdate --> Division by zero ...

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #7

                But good catch. Off the top of my head, it was the best I could figure out. Implementation details are left to the user to decide upon. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                K 1 Reply Last reply
                0
                • C Chris Meech

                  But good catch. Off the top of my head, it was the best I could figure out. Implementation details are left to the user to decide upon. :)

                  Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                  K Offline
                  K Offline
                  Klaus Werner Konrad
                  wrote on last edited by
                  #8

                  But wouldn't this be worth a vote ...

                  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