外键上的SQL Pivot但显示不同的列标题

时间:2014-03-12 14:13:44

标签: sql sql-server

我编写了一个存储过程,该存储过程首先会获得一个列表,这些列是不同的帐户描述,并使用这些列来获取每个帐户的每日货币值。我遇到的问题是最终用户有多个帐户具有相同帐户描述的实例,因此我现在使用AccountID来进行转移。

您将在下面找到我的查询以获取不同的帐户ID,然后创建数据透视表。我的最终目标是用户的当前列标题是无用的,即" 1504" " 1505" " 1683"这些对他们没有意义。如何重命名结果集的列标题,或者在显示描述时以某种方式使用基于不同帐户ID的数据透视表。 GLAccounts.Description是包含我想要显示的描述的列,而不是GLAccounts.AccountID。

SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(AccountID)
                FROM (
                    SELECT DISTINCT GLAccounts.AccountID AS AccountID
                    FROM GeneralLedger
                    INNER JOIN DailyFiles ON GeneralLedger.DailyFileID = DailyFiles.DailyFileID
                    Inner Join GLAccounts ON GeneralLedger.AccountID = GLAccounts.AccountID
                    -- Get All Daily Files for company and date range
                    INNER JOIN
                    (SELECT DailyFileID
                    FROM DailyFiles
                    WHERE (DailyFiles.DailyFileDate >= CONVERT(DATETIME, @MTDStart, 102)) AND (DailyFiles.DailyFileDate <= CONVERT(DATETIME, @MTDEnd, 102))
                    AND CompanyID = @CompanyID) AS DAILYFILEIDS ON GeneralLedger.DailyFileID = DAILYFILEIDS.DailyFileID
                ) x
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

SET @query = N'SELECT A.*
        from(
            SELECT SummedAccounts.Date, SUM(SummedAccounts.DayTotal) AS DayTotal, SummedAccounts.AccountID AS AccountID
            FROM(SELECT DailyFiles.DailyFileDate AS Date, IIF(GLAccounts.FriendlyName IS NOT NULL AND GLAccounts.FriendlyName <> '''', GLAccounts.FriendlyName, GLAccounts.Description) as AccountDescription, IIF(GeneralLedger.SavedToCredit = 1,
            --Saved To Credit Is True
            IIF(AccountTYpes.PositiveToCredit = 1, (SUM(GeneralLedger.Credit + GeneralLedger.Debit)), (SUM(GeneralLedger.Credit + GeneralLedger.Debit) * -1)),
            --Saved To Credit Is False
            IIF(AccountTYpes.PositiveToCredit = 1, ( SUM(GeneralLedger.Credit + GeneralLedger.Debit) * -1), (SUM(GeneralLedger.Credit + GeneralLedger.Debit)))) AS DayTotal, GeneralLedger.SavedToCredit, AccountTypes.PositiveToCredit, GeneralLedger.AccountID AS AccountID
            FROM GeneralLedger
            INNER JOIN DailyFiles ON GeneralLedger.DailyFileID = DailyFiles.DailyFileID
            Inner Join GLAccounts ON GeneralLedger.AccountID = GLAccounts.AccountID
            INNER JOIN AccountTypes ON AccountTypes.AccountTypeID = GLAccounts.AccountTypeID
            -- Get All Daily Files for company and date range
            INNER JOIN
            (SELECT DailyFileID
            FROM DailyFiles
            WHERE (DailyFiles.DailyFileDate >= @MTDStart) AND (DailyFiles.DailyFileDate <= @MTDEnd)
            AND CompanyID = @CompanyID) AS DAILYFILEIDS ON GeneralLedger.DailyFileID = DAILYFILEIDS.DailyFileID 
            GROUP BY GeneralLedger.AccountID, DailyFiles.DailyFileDate, GLAccounts.FriendlyName, GLAccounts.Description, GeneralLedger.SavedToCredit, AccountTypes.PositiveToCredit) SummedAccounts             
            GROUP BY SummedAccounts.Date, SummedAccounts.AccountID
        ) y
        pivot
        (
            min(DayTotal)
            for AccountID in (' + @cols + ')
        ) A'

结果集如下:

Stack Overflow Pivot Question 1

Stack Overflow Pivot Question 2

0 个答案:

没有答案