将参数传递给Sql Pivot

时间:2015-06-16 17:16:57

标签: sql sql-server tsql pivot

我在将多个参数传递到我希望转向的视图时遇到了一些麻烦。我正在尝试将此实现为我正在创建的报告,该报告不断添加新帐户。我确信这必须作为一个存储过程完成,但我的知识有限,而且我没有找到解释如何做到这一点的方法。以下只是我需要做的一小部分,但想法是一样的。

declare @AccountRef_Fullname nvarchar(max)

select  @AccountRef_Fullname = (select distinct Accountref_fullname 
from (select accountref_fullname from journalcreditlinedetail)JournalCreditLine
union
(select accountref_fullname from journaldebitlinedetail)
union
(select accountref_fullname from txnexpenselinedetail)
union
(select accountref_fullname from depositlinedetail)
union
(select discountaccountref_fullname from [appliedtotxndetail]))



select * from

(SELECT     DATEPART(ww, JournalEntry_2.TxnDate) AS Week, DATEPART(YYYY, JournalEntry_2.TxnDate) AS Year, SUM([Credit-Debit].Amount) 
                                                                           AS Amount, [Credit-Debit].AccountRef_FullName
                                                    FROM          (SELECT     IDKEY, sum(isnull(Amount,0)) * - 1 AS Amount, AccountRef_FullName
                                                                            FROM          dbo.journalcreditlinedetail
                                                                            group by idkey, AccountRef_FullName
                                                                            UNION
                                                                            SELECT     IDKEY, sum(isnull(Amount,0))Amount, AccountRef_FullName
                                                                            FROM         dbo.journaldebitlinedetail
                                                                            group by idkey, AccountRef_FullName
                                                                            ) AS [Credit-Debit] INNER JOIN
                                                                               (SELECT     TxnID, TxnDate
                                                                                 FROM          dbo.journalentry AS journalentry_1) AS JournalEntry_2 ON [Credit-Debit].IDKEY = JournalEntry_2.TxnID
                                                    GROUP BY [Credit-Debit].AccountRef_FullName, DATEPART(ww, JournalEntry_2.TxnDate), DATEPART(yyyy, JournalEntry_2.TxnDate)
                                                    ) Journal_Data


                                                    PIVOT
(
  sum(amount)
  for
  AccountRef_FullName in (' + @AccountRef_Fullname + ')
  )
 AS PivotTable

1 个答案:

答案 0 :(得分:1)

需要使用EXEX(@sql)形成和执行动态数据透视表语句。列名必须在查询的SelectIn部分中指定,并且可以是变量。

要构建列名,您需要将每个Accountref_fullname包装在[]

DECLARE @AccountRef_Fullname NVARCHAR(MAX)
SELECT @AccountRef_Fullname = COALESCE(@AccountRef_Fullname + ',', '') + '[' + AccountRef_Fullname + ']'
FROM (
    SELECT accountref_fullname FROM journalcreditlinedetail
    UNION select accountref_fullname FROM journaldebitlinedetail
    etc..
)

然后构建你的select语句并像

一样执行它
DECLARE @Sql NVARCHAR(MAX)
SET @Sql = N' SELECT Week, Year, ' + @AccountRef_Fullname 
    + 'FROM ( your subquery ) JournalData'
    + 'PIVOT ('
    + ' SUM(amount) FOR AccountRef_FullName IN (' + @AccountRef_Fullname + ')'
    + ') AS PivotTable'

EXEC (@Sql)