I'm having some trouble passing multiple parameters into a view I wish to Pivot. I am trying to implement this into a report I am creating that is constantly having new accounts added to it. I am sure this has to be done as a stored procedure, but I have limited knowledge and I am not finding much in the ways of explaining how to do this. Below is just a small fraction of what I need to do, but the idea is the same.
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
Subquery returned more than 1 valueerror if you have more than one account