简体   繁体   中英

how can I use Pivot using a variable in SQL?

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 ?

To get all the paycodes in @data use

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

Then remove the last comma

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM