如何在我的SQL查询中透视两列

时间:2019-02-12 07:58:23

标签: sql sql-server pivot

此查询几乎存在。我现在唯一的问题是结果没有改变。相反,它们保留在名为“值”的列中。

选择*从(     选择         jD.AccountID,         SUM(jD.Amount)AS [总计],         COA.Name作为COAName,         SUM(jD.Qty)AS QTY,         CONVERT(date,GETDATE())AS Date,         AttributeDefinitions.Name,         AttributeValues.Value     从         属性定义INNER JOIN         AttributeCategories ON上AttributeDefinitions.AttributeCategoryID = AttributeCategories.AttributeCategoryID内联接         AttributeValues ON AttributeDefinitions.AttributeDefinitionID = AttributeValues.AttributeDefinitionID右外连接         JnlDetails AS jD带(NOLOCK)内部联接         COD ON jD.AccountID = COA.AccountID ON AttributeValues.AttributeValueGroupID = COA.AttributeValueGroupID     哪里         (jD.CreateDate> = GETDATE()-2)和         (jD.CreateDate

)作为T1 枢 (    MAX([名称])FOR名称输入([ACCT_NO],[DEPT_ID],[GLENTRY_CLASSID],[GLENTRY_PROJECTID]    ,[GLDIMBENEFITING_DEPARTMENT],[GLDIMFUND],[LOCATION_ID]) )PT

2 个答案:

答案 0 :(得分:0)

这是您要寻找的吗?

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

答案 1 :(得分:0)

  

请注意,结果会有所不同,因此我可以有3列或4,5   等等...最多10列

那么您需要动态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);