Skip to Content

Pivot columns in query SAP B1

I create query like below. I get a table including 4 columns: Item code, Qty, Amount, & Month.

Now I want to pivot this table to convert to 3 columns: Item code, Month_Qty, & Month_Amount.

Please advice. Thanks!

<code>SELECT T1.[ItemCode], T1.[Dscription], T1.[Quantity] as Qty, T1. 
[Quantity]*T1.[Price] as Amount, Month(T0.[DocDate] as 
'Month' FROM OIGE T0  INNER JOIN IGE1 T1 ON T0.[DocEntry] = T1. 
[DocEntry] WHERE T0.[DocDate] >=[%0] and  T0.[DocDate] <=[%1] and T0. 
[Comments] NOT Like '%%Automatically%%'
Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Oct 31, 2019 at 11:46 AM

    Hi,

    You can check this query:

    SELECT 
    	[C] as 'ItemCode',
    	[D] as 'Item Description',
    	P.[Jan 2019],P.[Feb 2019],P.[Mar 2019],P.[Apr 2019],P.[May 2019],P.[Jun 2019],P.[Jul 2019],P.[Aug 2019]
    	,P.[Sep 2019],P.[Oct 2019],P.[Nov 2019],P.[Dec 2019]
    
    FROM
    (SELECT	
    		T1.[ItemCode] as C, 
    		T1.[Dscription] as D, 
    		T1.[Quantity] as Qty, 
    		T1.[Quantity]*T1.[Price] as Amount, 
    		(CONVERT(CHAR(4), T0.DocDate, 100) + CONVERT(CHAR(4), T0.DocDate, 120)) as months 
    FROM OIGE T0  INNER JOIN IGE1 T1 ON T0.[DocEntry]=T1.[DocEntry] 
    ----WHERE T0.[DocDate] >=[%0] and  T0.[DocDate] <=[%1] and T0.[Comments] NOT Like '%%Automatically%%'
    ) S
    
    Pivot
    (SUM(Amount) FOR months  IN ( [Jan 2019],[Feb 2019],[Mar 2019],[Apr 2019],[May 2019],[Jun 2019],[Jul 2019],[Aug 2019]
    ,[Sep 2019],[Oct 2019],[Nov 2019],[Dec 2019] ) 
    ) P
    

    Thank you,

    Aziz

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.