Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Product sales qty comparing multiple years

0 Kudos
183
  • SAP Managed Tags:

I would like to make a report that shows total sales of a product across multiple years.
So it would look something like this:

Product 1 | Total quantity sold in year 1 | Total quantity sold in year 2 etc.
Product 2 | Total quantity sold in year 1 | Total quantity sold in year 2 etc.

I've tried this but it returns only 0's.

SELECT Distinct T1.ItemCode, (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),4,11) like '2020%' then T4.Quantity else 0 end))

FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2020',

(isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),4,11) like '2021%' then T4.Quantity else 0 end))

FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2021',

(isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),4,11) like '2022%' then T4.Quantity else 0 end))

FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2022'

FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry

Group By SUBSTRING((CONVERT(VARCHAR(11),T0.DOCDATE,106)),4,11), T1.ItemCode

Having (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),4,11) like '% %' then T4.Quantity else 0 end))

FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0))>0

1 ACCEPTED SOLUTION

0 Kudos
119
  • SAP Managed Tags:

I've found the solution. I was converting the string to an incorrect format: SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),4,11) returns MMM-YYYY.

This function would work when calculating totals per month. To calculated per year I changed the start of the string to 8 so it would only return YYYY. SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11)

2 REPLIES 2

Former Member
0 Kudos
119
  • SAP Managed Tags:

Welcome to the SAP Community. Thank you for visiting us to get answers to your questions.

Since you're asking a question here for the first time, I'd like to offer some friendly advice on how to get the most out of your community membership and experience.

First, please see https://community.sap.com/resources/questions-and-answers, as this resource page provides tips for preparing questions that draw responses from our members. Second, feel free to take our Q&A tutorial at https://developers.sap.com/tutorials/community-qa.html, as that will help you when submitting questions to the community.

I also recommend that you include a profile picture. By personalizing your profile, you encourage readers to respond: https://developers.sap.com/tutorials/community-profile.html.

Now for some specific suggestions on how you might improve your question:

* Outline what steps you took to find answers (and why they weren't helpful) -- so members don't make suggestions that you've already tried.

* Share screenshots of what you've seen/done (if possible), as images always helps our members better understand your problem.

* Make sure you've applied the appropriate tags -- because if you don't apply the correct tags, the right experts won't see your question to answer it.

* Use the "insert code" feature when sharing your code, so members have an easier time reading.

Should you wish, you can revise your question by selecting Actions, then Edit.

The more details you provide (in questions tagged correctly), the more likely it is that members will be able to respond. As it stands, I don't know if there is enough information here for members to understand your issue. So please consider revising your question because I'd really like to see you get a solution to your problem!

I hope you find this advice useful, and we're happy to have you as part of SAP Community!

0 Kudos
120
  • SAP Managed Tags:

I've found the solution. I was converting the string to an incorrect format: SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),4,11) returns MMM-YYYY.

This function would work when calculating totals per month. To calculated per year I changed the start of the string to 8 so it would only return YYYY. SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11)