SQLTeam.com | Weblogs | Forums

Pivot query result unexpected

sql2012

#1

Hello everyone.
I'm quite new user of SQL Server and T-SQL.

I have a query with PIVOT and I have coumn with quantities splitted by year.
Then I have a column with quantities for last year (2015) but only filled until MAY.

So the Q_progr_prev should be filled equal to 2015_Q until MAY.
But the vaues of the 2 column don't match and I don't understand why.

Here is the code:

[SELECT *
FROM (SELECT myyear, CONVERT(VARCHAR(10), CONVERT(smallint, myyear)) + '_Q' AS eseQ, Q,
CASE WHEN myyear = DATEPART(YEAR, GETDATE()) - 1 AND mymonth <= DATEPART(MONTH, GETDATE()) - 1 THEN Q ELSE 0 END AS Q_progr_prev
FROM mytable) AS c PIVOT (sum(q) FOR EseQ IN ([2015_Q],[2016_Q])) AS Q]
.

Thank you,
Marco.


#2

Nevermind.
I was missing some fields in PIVOT query that were in previous view.