Difference between two values in pivot table

Qlikview Help!!!
I have a pivot table in Qlikview 11.
1) Original - is my original pivot table
2) Final - is the pivoted version of Original
3) Result - this is what the result needs to be

How to calculate the difference columns - DIFF1, DIFF2, DIFF3 in the same format as attached? Please help.

Capture2.PNG
LION KINGDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Are you using a query for pivot table ? Which Database are you using ?
LION KINGDeveloperAuthor Commented:
Yes, Pawan. I'm using a query (load script) in Qlikview. SQL Server 2016 is the database.
Pawan KumarDatabase ExpertCommented:
Please provide me the query. Will modify that for you to the required result. Also add SQL Server as a Topic for future reads.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

LION KINGDeveloperAuthor Commented:
ODBC CONNECT TO TEST_SQL_SERVER;
CUR_QTR:
LOAD
      NAME,
      DATE1,
      COMPANY,
      AMT,
      LEFT(DATE1,4)             AS YR,
         RIGHT(DATE1,2)             AS MTH
;
SELECT *
FROM CUR_QTR;

JOIN(CUR_QTR)
LOAD
NAME,
      DATE1,
      COMPANY,
      AMT,
      LEFT(DATE1,4)             AS YR,
         RIGHT(DATE1,2)             AS MTH


FROM
[C:\TEST.xlsx]
(ooxml, embedded labels, table is Sheet1);

This is the query from Qlikview. CUR_QTR is the table from SQL server that has data for 201703 only. For prior period the data is loaded from Excel file. While creating the pivot table in QLikview, this script is used to create dimensions n measures. Measure is the sum(Amt).
Pawan KumarDatabase ExpertCommented:
Are you doing pivot in SQL server or in QlikView?
LION KINGDeveloperAuthor Commented:
Doing Pivot in Qlikview - within Pivot table (chart).
Pawan KumarDatabase ExpertCommented:
try Like this... (Never did that in UI though)

DIFF1 : {201703} - {201612}
LION KINGDeveloperAuthor Commented:
I tried that earlier. That didn't work. It also creates column under each year month.
Pawan KumarDatabase ExpertCommented:
Please find FULL TESTED SOLUTION.

Table Creation and DATA Insertion.

CREATE TABLE testPivot1
(
	 Name VARCHAR(10)
	,yr_mth VARCHAR(6)
	,TE VARCHAR(10)
	,Company INT
)
GO

INSERT INTO testPivot1 VALUES 
('ABC',201606,'',1100),
('ABC',201609,'',1200),
('ABC',201612,'',1200),
('ABC',201703,'',900),
('DEF',201606,'',300),
('DEF',201609,'',400),
('DEF',201612,'',400),
('DEF',201703,'',200)
GO

Open in new window


SOLUTION

;WITH CTE AS
(
	SELECT *, CONCAT('DIFF',ROW_NUMBER() OVER(PARTITION BY Name ORDER BY yr_mth)) rnk FROM
	(
		SELECT * , Company - LAG(Company) OVER (PARTITION BY Name ORDER BY yr_mth) DIFF
		FROM testPivot1
	)u
)
SELECT Name,MAX([201606]) [201606] ,MAX([201609]) [201609] ,MAX([201612]) [201612] 
,MAX([201703]) [201703],MAX([DIFF2]) [DIFF2] ,MAX([DIFF3]) [DIFF3]  ,MAX([DIFF4]) [DIFF4]
FROM 
CTE r
PIVOT 
( 
	MIN(r.Company)
	FOR [yr_mth] IN ([201606],[201609],[201612],[201703]) 
)t
PIVOT 
( 
	MIN(DIFF)
	FOR [rnk] IN ([DIFF2],[DIFF3],[DIFF4]) 
)t1
GROUP BY Name 

Open in new window


OUTPUT

/*------------------------
OUTPUT
------------------------*/
Name       201606      201609      201612      201703      DIFF2       DIFF3       DIFF4
---------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
ABC        1100        1200        1200        900         100         0           -300
DEF        300         400         400         200         100         0           -200

(2 row(s) affected)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawan KumarDatabase ExpertCommented:
Question abandoned.
Provided tested solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.