Average Sold Per Month Query

  • Come on, that's over 2 weeks ago on a thread I didn't participate in!!! How am I supposed to remember that??

  • Actually, these queries aren't returning what i'm looking for.  Can i put it in English to explain my need ? :

    I want to return the average monthly qty sold by product for each product.

    The queries above return the product multi-times by either year or month

    ie.

    PRODA    2004    34

    PRODA    2005    45

    PRODB    2004     23

    PRODC   2003     23

    PRODC    2004   45

    PRODC    2005   56

    What i need is an average monthly qty sold by product 'over the years'

    ie:

    PRODA     23

    PRODB   45

    PRODC   45

    ...being:  product, average sold on a monthly basis

    ???

     

  • Noel is right Query should be

    SELECT Product, SUM(SaleQty)/12.0 AvgSalesPerMonth

    FROM

     orderdetail

    GROUP BY  Product

    Corrected in next post.

    Regards,
    gova

  • See Noeld's solution :

    SELECT Product, SUM(SaleQty) / NumberOfMonths AvgSales

    FROM

    dbo.orderdetail

    GROUP BY Product

    I'd probabely use DateDiff(M, getDate(), 'static date when the system was brough online)'

  • SELECT Product, SUM(SaleQty)/COUNT(DISTINCT CONVERT(VARCHAR, DATEPART(MONTH, SaleDate)) + CONVERT(VARCHAR, DATEPART(YEAR, SaleDate))) AvgSalesPerMonth

    FROM

     orderdetail

    GROUP BY  Product

    Will be more accurate

    DateDiff Remi posted is better since if there are no sales on a month that is also considered in that.

    Regards,
    gova

  • SELECT Product, 1.0*SUM(SaleQty)/datediff(m,Min(SaleDate)-Max(SaleDate))AvgSalesPerMonth

    FROM

     orderdetail

    GROUP BY  Product

    Will be simpler

     


    * Noel

  • SELECT Product, 1.0*SUM(SaleQty)/datediff(m,Min(SaleDate), Max(SaleDate))AvgSalesPerMonth

    FROM

    orderdetail

    GROUP BY Product

    Will be simpler correct

    Damn are we having a bad day or what??????

  • Damn are we having a bad day or what??????

    It's hard to beat your typing...  no time to loose

     


    * Noel

  • This was exactly what i wanted - cheers govinn

  • Check out my version of Noelds' code... might run a little faster and it is easier to understand.

  • That'll never change .

  • I could improve my typing skills ... which could give me an edge ?

     


    * Noel

  • I have a double edge, I think fast and type faster. Now you may have more experience but that doesn't atomatically translates in speed .

  • You're right Remi,

    I see what you are doing and it is a tad more sensible.

    .... No offence intended Noeld - actually thought what you suggested was also quite clever - "there's many ways to skin a cat"

    Thanks all

  • I just corrected his typo... he deserves the credit this time.

Viewing 15 posts - 16 through 29 (of 29 total)

You must be logged in to reply to this topic. Login to reply