0

I have paycode numbers and Names from table Paycodes and I have Amount in monthlyTransaction. as follows:

Paycodes
Code     Name
1        Basic Salary
2        Variable Deduction Amount
3        Fixed/Var Insurance PayCode

MonthlyTransaction
Code     Amount
1        3000
2        10000
1        130000
1        150000
3        120000

I want it to be like this using pivot

Basic Salary    Variable Deduction Amount  Fixed/Var Insurance PayCode
31000           10000                      120000

I want to use pivot to sum the Amount of each Paycode and I used this:-

DECLARE @data AS NVARCHAR(MAX)
DECLARE @query  AS NVARCHAR(MAX)


--  DECLARE @data TABLE
--(
--   PaycodeName NVARCHAR(max)
--)
--INSERT INTO @data
--        ( PaycodeName )
--select dbo.Paycode.PayCodeName FROM dbo.Paycode 




select @data = Paycode.PayCodeName FROM Paycode


set @query = 'SELECT * FROM (
    SELECT Paycode.Name , MonthlyTransaction.Amount
    From MonthlyTransaction
    LEFT JOIN dbo.Paycode ON Paycode.code = MonthlyTransaction.Paycode
    ) AS s
    PIVOT
    (
        SUM(Amount)
        FOR Paycode.Name IN ('+@data+')
    ) AS pvt '

EXECUTE Sp_executesql @query 

When I print @data it retrieve the last paycode only ! Can anyone help ?

4
  • Possible duplicate of SQL Server dynamic PIVOT query? Commented Aug 3, 2016 at 8:44
  • What does the pivot have to do with the select @data? Commented Aug 3, 2016 at 8:49
  • @jonny ... it should get all the paycode names ! Commented Aug 3, 2016 at 8:51
  • @ChrisPickford I saw it and it is not clear for me :/ Commented Aug 3, 2016 at 8:53

3 Answers 3

1

To get all the paycodes in @data use

SELECT @data = @data + Paycode.PayCodeName + ', '
FROM Paycode

Then remove the last comma

Sign up to request clarification or add additional context in comments.

1 Comment

(Y) it worked ... but no result retrieved .. do you have any idea why didn't the query retrieve the result sets ??!
0

the answer is :

DECLARE @codeNameCol NVARCHAR(max)

SELECT   @codeNameCol= COALESCE(@codeNameCol + ',','') + QUOTENAME(RTRIM(LTRIM(PayCodeName)))
FROM (SELECT DISTINCT PayCodeName FROM Paycode) AS codeNameCol

DECLARE @querys NVARCHAR(max)

Set @querys='
SELECT  *
FROM    ( SELECT    dbo.EmpAssignment.EmployeeId ,
                PayCodeName ,
                Amount
      FROM      dbo.MonthlyTransaction
                LEFT JOIN dbo.Paycode ON Paycode.code = MonthlyTransaction.Paycode
                LEFT JOIN dbo.EmpAssignment ON EmpAssignment.EmpId = MonthlyTransaction.EmpId
                LEFT JOIN dbo.PayrollGroup ON PayrollGroup.PayrollGroup = EmpAssignment.PayrollGroup
    ) DataTable PIVOT
( SUM(Amount) FOR PayCodeName IN ( '+@codeNameCol+' ) ) PivotTable;'

EXEC (@querys)

Comments

0

This should do the trick

 SELECT 1 Code, 'Basic Salary' Name
 INTO #Paycode
 UNION
 SELECT 2 Code, 'Variable Deduction Amount' Name
 UNION
 SELECT 3 Code, 'Fixed/Var Insurance PayCode' Name

 SELECT 1 Code, 3000 Amount
 INTO #MonthTrans
 UNION
 SELECT 2 Code, 10000 Amount
 UNION
 SELECT 1 Code, 130000 Amount
 UNION
 SELECT 1 Code, 150000 Amount
 UNION
 SELECT 3 Code, 120000 Amount

 DECLARE @data AS NVARCHAR(MAX)

 SELECT @data = ISNULL(@data,'') + '[' +CAST(Code AS varchar) + '], '
 FROM #Paycode

 SELECT @data=SUBSTRING(@data, 0, LEN(@data))


 DECLARE @query  AS NVARCHAR(MAX)

 SELECT @query = 'SELECT * 
 FROM
    #MonthTrans M
 PIVOT ( 
    SUM(Amount) FOR Code IN (' + @data + ')
 ) pvt'


 EXECUTE Sp_executesql @query 


 drop table #paycode
 drop table #MonthTrans

3 Comments

I used temptables to try it out. Substitute the names to your tables
check my answer ... Thanks 4 help :)
Please mark as the solution if it helped. I corrected the typo too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.