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: 

Query Qty per product multiple years

0 Kudos
190
  • SAP Managed Tags:

Sap Business one

I would like to add information to a report that looks like the code below, but for multiple years.

So the output should be something like this:
Year 1 Jan, Year 1 Feb, Year 1 Mar etc... And then Year 2 Jan, Year 2 Feb.

I've tried duplicating the code below with different years using UNION in between, however this puts the products below each other instead of on one line.

  1. SELECT P.[ItemCode], P.[ItemName], P.[OnHand],
  2. [1] as [Jan],
  3. [2] as [Feb],
  4. [3] as [Mar],
  5. [4] as [Apr],
  6. [5] as [May],
  7. [6] as [Jun],
  8. [7] as [Jul],
  9. [8] as [Aug],
  10. [9] as [Sep],
  11. [10] as [Oct],
  12. [11] as [Nov],
  13. [12] as [Dec]
  14. FROM (
  15. SELECT T0.[ItemCode], T0.[ItemName], T0.[OnHand], T1.[Quantity],MONTH(T2.docdate) as [MONTH]
  16. FROM [dbo].[OITM] T0
  17. INNER JOIN dbo.INV1 T1 ON T0.ItemCode = T1.ItemCode
  18. INNER JOIN dbo.OINV T2 ON T1.DocEntry = T2.DocEntry AND Year(T2.docdate)=2010 ) S
  19. PIVOT (SUM(Quantity) FOR [month] IN
  20. ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) P
  21. ORDER BY P.[ItemCode]
1 ACCEPTED SOLUTION

0 Kudos
121
  • SAP Managed Tags:

I've found a solution by using the following Code:

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

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

For totals per year:

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

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



2 REPLIES 2

Former Member
0 Kudos
121
  • 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
122
  • SAP Managed Tags:

I've found a solution by using the following Code:

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

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

For totals per year:

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

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