dollar chrome symbol

SQL view for sales by item by year

I had a request from a blog reader for a view that shows sales of items by year. This seemed like pretty useful code to create, so I put the following view together. I am making a few assumptions (listed in the view comments), and am hard coding years from 2005 through 2011. You can easily change the years or add new ones by following the example in my code.

~~~~~

create view view_Sales_by_Item_by_Year
as

/******************************************************************
view_Sales_by_Item_by_Year
Created Aug 11, 2011 by Victoria Yudin - Flexible Solutions, Inc.
For updates see http://victoriayudin.com/gp-reports/
- Returns total sales (invoices - returns) for each item by year
- Only posted invoices and returns are included
- Voided transations are excluded
- Item Description is taken from Inventory Item Maintenance
for all inventory items,
and from SOP line items for non-inventory items

******************************************************************/

SELECT 
   D.ITEMNMBR [Item_Number],
   D.Item_Description,
   sum(case when year(D.DOCDATE) = 2005
      then D.SALES else 0 end) as [2005 Sales],
   sum(case when year(D.DOCDATE) = 2006
      then D.SALES else 0 end) as [2006 Sales],
   sum(case when year(D.DOCDATE) = 2007
      then D.SALES else 0 end) as [2007 Sales],
   sum(case when year(D.DOCDATE) = 2008
      then D.SALES else 0 end) as [2008 Sales],
   sum(case when year(D.DOCDATE) = 2009
      then D.SALES else 0 end) as [2009 Sales],
   sum(case when year(D.DOCDATE) = 2010
      then D.SALES else 0 end) as [2010 Sales],
   sum(case when year(D.DOCDATE) = 2011
      then D.SALES else 0 end) as [2011 Sales]

FROM
(SELECT SH.DOCDATE, SD.ITEMNMBR,
 coalesce(I.ITEMDESC, SD.ITEMDESC) Item_Description,
 CASE SD.SOPTYPE
     WHEN 3 THEN SD.XTNDPRCE
     WHEN 4 THEN SD.XTNDPRCE*-1
     END SALES
 FROM SOP30200 SH
 INNER JOIN
     SOP30300 SD
     ON SD.SOPNUMBE = SH.SOPNUMBE
     AND SD.SOPTYPE = SH.SOPTYPE
 LEFT OUTER JOIN
     IV00101 I
     ON I.ITEMNMBR = SD.ITEMNMBR
 WHERE SH.VOIDSTTS = 0 
     AND SH.SOPTYPE IN (3,4) 
     AND SD.XTNDPRCE <> 0 
     AND SD.ITEMNMBR not like 'XXXXXXXXXXXXXXX%') D

GROUP BY D.ITEMNMBR, D.Item_Description

/** the following will grant permissions to this view to DYNGRP, 
leave this section off if you do not want to grant permissions **/ 

GO
GRANT SELECT ON view_Sales_by_Item_by_Year TO DYNGRP

~~~~~

Disclaimer: I tested this on limited data, if you find an issue or have a suggestion for improvement, please let me know and I will post the update here for everyone.

25 Responses to “SQL view for sales by item by year”

  1. Victoria -

    If I wanted to look at customers and vendors in the same manner, what is the SQL code? I have been trying with no luck – I can only get individual rows with customers/vendors and their annual sales but would prefer to see my years in columns.

    • Mark,

      The trick to getting the data into yearly columns is to use the “sum” code, as in the example above. Give me a few days and I will publish some new posts to help with this.

      -Victoria

  2. How about the same but for customers? I’ve been trying to figure out how and which table to pull from but have not been able to figure out (dollar value).

    Thanks!!!

    • Hi Mark,

      The inventory sales are coming from the SOP (Sales Order Processing) module. I can change this easily enough to do it by customer…but then a few questions then come up:

      • What about anything not entered in the SOP module? (For example anything entered directly in the RM module.)
      • What amount should be used? (Subtotal, Invoice Amount, something else?)

      -Victoria

      • I had not considered your points. Could RM and COP be combined?

        I would be looking at subtotal not invoice amount. The invoice amount would give freight, tax, miscellaneous.

        • Mark,

          You cannot really ‘combine’ RM and SOP, as SOP is a subset of RM. So if you want information from both, you would most likely be better off simply looking at RM data.

          -Victoria

  3. Thanks a lot Victoria I tested and it works perfect! If you need any help related to network and Windows Servers let me know to pay it forward! =D

    - George

  4. Hi Victoria, i need this but instead of year by month, what do i need to change?

    Thanks,

    George

  5. Victoria-

    Once again you continue to astound me with your knowledge. Wat part of this can I change to get it to reflect number of units (or quantity of item) rather than dollars? Dollars are good, but as prices change, the dollars can skew whether it is worthwhile to carry an item.

    I’m blushing that you actually chose my idea to use for a blog topic. :)

    Mark

    • Mark,

      Thanks for the great idea! You know, I was actually wondering whether to do this for dollars or quantities when I was writing it. :-)

      You can change this to quantities instead by changing all instances you find of SD.XTNDPRCE (there should be 3 of them) to be SD.QUANTITY instead.

      -Victoria

  6. Great idea !!! I am getting this error though trying to create it

    Msg 102, Level 15, State 1, Procedure view_Sales_by_Item_by_Year, Line 26
    Incorrect syntax near ‘=’.
    Msg 102, Level 15, State 1, Procedure view_Sales_by_Item_by_Year, Line 50
    Incorrect syntax near ‘D’.

Trackbacks/Pingbacks

  1. SQL view for sales quantities by item by year | Victoria Yudin - January 25, 2012

    [...] 23, 2012 by Victoria Yudin 1 Comment Even since I published my view for sales by item by year I started receiving requests for the same type of view showing quantities instead of amounts. There [...]

  2. Interesting Findings & Knowledge Sharing » SQL view for sales quantities by item by year - January 23, 2012

    [...] since I published my view for sales by item by year I started receiving requests for the same type of view showing quantities instead of amounts. There [...]

  3. SQL view for sales quantities by item by year - Victoria Yudin - GP Technical Blogs - Microsoft Dynamics Community - January 23, 2012

    [...] 0 Even since I published my view for sales by item by year I started receiving requests for the same type of view showing quantities instead of amounts. There [...]

  4. SQL view for sales quantities by item by year | Victoria Yudin - January 23, 2012

    [...] 23, 2012 by Victoria Yudin 0 Comments Even since I published my view for sales by item by year I started receiving requests for the same type of view showing quantities instead of amounts. There [...]

  5. SQL view for sales by item by month - Victoria Yudin - GP Technical Blogs - Microsoft Dynamics Community - August 29, 2011

    [...] 0 Here is a new Dynamics GP SQL view in reponsse to a reader that liked my view for sales by item by year, but wanted to see the same thing by month. I am making a few assumptions (listed in the view [...]

  6. SQL view for sales by item by month | Victoria Yudin - August 29, 2011

    [...] 0 Comments Here is a new Dynamics GP SQL view in reponsse to a reader that liked my view for sales by item by year, but wanted to see the same thing by month. I am making a few assumptions (listed in the view [...]

  7. SQL view for sales by item by year | Victoria Yudin - DynamicAccounting.net - GP Technical Blogs - Microsoft Dynamics Community - August 15, 2011

    [...] Comments 0 Victoria Yudin adds a new SQL view for sales by item by year [...]

  8. Everything Dynamics GP #19 - About Dynamics, Development and Life - GP Technical Blogs - Microsoft Dynamics Community - August 12, 2011

    [...] SQL view for sales by item by year (Victoria Yudin) has posted this useful SQL View.  [...]

  9. SQL view for sales by item by year | Interesting Findings & Knowledge Sharing - August 11, 2011

    [...] from: SQL view for sales by item by year VN:F [1.9.10_1130]Please wait while your rating is cast…Rating: 0.0/10 (0 votes cast)VN:F [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 486 other followers