简体   繁体   中英

How to pivot two of the columns in my SQL Query

This query is almost there. The only problem I have now is that the results are not pivoting. Instead they stay in the column named Value.

SELECT * FROM (
    SELECT
        jD.AccountID, 
        SUM(jD.Amount) AS [Total Dollars], 
        COA.Name as COAName, 
        SUM(jD.Qty) AS QTY, 
        CONVERT(date, GETDATE()) AS Date, 
        AttributeDefinitions.Name, 
        AttributeValues.Value
    FROM
        AttributeDefinitions INNER JOIN
        AttributeCategories ON AttributeDefinitions.AttributeCategoryID = AttributeCategories.AttributeCategoryID INNER JOIN
        AttributeValues ON AttributeDefinitions.AttributeDefinitionID = AttributeValues.AttributeDefinitionID RIGHT OUTER JOIN
        JnlDetails AS jD WITH (NOLOCK) INNER JOIN
        COA ON jD.AccountID = COA.AccountID ON AttributeValues.AttributeValueGroupID = COA.AttributeValueGroupID
    WHERE
        (jD.CreateDate >= GETDATE() - 2) AND 
        (jD.CreateDate < GETDATE() + 1)
    GROUP BY 
        jD.AccountID, 
        COA.Name, 
        jD.Qty, 
        jD.CreateDate, 
        AttributeDefinitions.Name, 
        AttributeValues.Value
   
) as T1
PIVOT
(
   MAX([Name]) FOR Name IN ([ACCT_NO], [DEPT_ID], [GLENTRY_CLASSID],[GLENTRY_PROJECTID]
   ,[GLDIMBENEFITING_DEPARTMENT],[GLDIMFUND],[LOCATION_ID])
) PT

Is this what you are looking for ?

SELECT * FROM (
    SELECT
        jD.AccountID, 
        SUM(jD.Amount) AS [Total Dollars], 
        COA.Name, 
        SUM(jD.Qty) AS QTY, 
        CONVERT(date, GETDATE()) AS Date, 
        AttributeDefinitions.Name, 
        AttributeValues.Value
    FROM
        AttributeDefinitions INNER JOIN
        AttributeCategories ON AttributeDefinitions.AttributeCategoryID = AttributeCategories.AttributeCategoryID INNER JOIN
        AttributeValues ON AttributeDefinitions.AttributeDefinitionID = AttributeValues.AttributeDefinitionID RIGHT OUTER JOIN
        JnlDetails AS jD WITH (NOLOCK) INNER JOIN
        COA ON jD.AccountID = COA.AccountID ON AttributeValues.AttributeValueGroupID = COA.AttributeValueGroupID
    WHERE
        (jD.CreateDate >= GETDATE() - 2) AND 
        (jD.CreateDate < GETDATE() + 1)
    GROUP BY 
        jD.AccountID, 
        COA.Name, 
        jD.Qty, 
        jD.CreateDate, 
        AttributeDefinitions.Name, 
        AttributeDefinitions.Description, 
        AttributeValues.Value
    ORDER BY 
        jD.AccountID
) as T1
PIVOT
(
   MAX([Name]) FOR Name IN ([Account Code], [Project Code], [Fund])
) PT

Please note that the results vary , so i can have 3 columns , or 4 , 5 , etc... up to 10 columns

Then you need dynamic SQL

DECLARE @Attributes VARCHAR(MAX), @SQL VARCHAR(MAX)

SELECT @Attributes = STUFF((SELECT ',' + QUOTENAME(Name) 
                    FROM AttributeDefinitions
            FOR XML PATH(''), TYPE
            ).value('.', 'VARCHAR(MAX)') 
        ,1,1,'')

SET @SQL = '
SELECT * FROM (
    SELECT
        jD.AccountID, 
        SUM(jD.Amount) AS [Total Dollars], 
        COA.Name, 
        SUM(jD.Qty) AS QTY, 
        CONVERT(date, GETDATE()) AS Date, 
        AttributeDefinitions.Name, 
        AttributeValues.Value
    FROM
        AttributeDefinitions INNER JOIN
        AttributeCategories ON AttributeDefinitions.AttributeCategoryID = AttributeCategories.AttributeCategoryID INNER JOIN
        AttributeValues ON AttributeDefinitions.AttributeDefinitionID = AttributeValues.AttributeDefinitionID RIGHT OUTER JOIN
        JnlDetails AS jD WITH (NOLOCK) INNER JOIN
        COA ON jD.AccountID = COA.AccountID ON AttributeValues.AttributeValueGroupID = COA.AttributeValueGroupID
    WHERE
        (jD.CreateDate >= GETDATE() - 2) AND 
        (jD.CreateDate < GETDATE() + 1)
    GROUP BY 
        jD.AccountID, 
        COA.Name, 
        jD.Qty, 
        jD.CreateDate, 
        AttributeDefinitions.Name, 
        AttributeDefinitions.Description, 
        AttributeValues.Value
    ORDER BY 
        jD.AccountID
) T
PIVOT
(
   MAX([Name]) FOR Name IN (' + @Attributes + ')
) PT
'

EXEC(@SQL);

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