1

我已经编写了我的数据透视 SQL,它正在工作。当前的 SQL 语句如下所示,我将展示如何返回数据的示例。我需要添加一列,这就是它爆炸的地方:

Select 
    ProductGroup,
    Origin,
    Destination,
    [YEAR], 
    Isnull([Jan],0) as "Jan",
    isnull([Feb],0) as "Feb",
    isnull([Mar],0) as "Mar",
    isnull([Apr],0) as "Apr",
    isnull([May],0) as "May",
    isnull([Jun],0) as "Jun",
    isnull([Jul],0) as "Jul",
    isnull([Aug],0) as "Aug",
    isnull([Sep],0) as "Sep",
    isnull([Oct],0) as "Oct",
    isnull([Nov],0) as "Nov",
    isnull([Dec],0) as "Dec"
FROM
(
SELECT 
    p.ProductGroup, 
    S.Origin, 
    S.FinalDest AS Destination,
    SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3) as MonthAbreviated, 
    Year(BolDate) AS [Year], 
    Count(*) AS [Total]
--  ,COUNT(S.Purchase#) AS [TheTotal]
    FROM 
        dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
        INNER JOIN dbo.Products as p ON pu.Product = p.Product 
        INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
WHERE 
    Year(BolDate)<>1994 AND
    pu.Cancelled=0 AND S.[Status]='L'
GROUP BY p.ProductGroup, S.Origin, S.FinalDest,Year(BolDate), SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3)

) AS SourceTable
PIVOT
(
sum( Total )
FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) AS PivotTable

结果示例:

ProductGroup   Origin  Destination  YEAR        Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
Nail           Bath    Toronto      2012        0   0   0   10  1   0   0   0   0   0   0   0 
Nail           Cedars  Toronto      2011        0   0   0   0   0   0   0   0   0   0   25  53

我需要添加一个显示行总数的列。例如,第 1 行是 11,第 2 行是 78。在我的选择中,我认为只需将“Total”添加到查询中就可以了,但每次我都会得到一个无效的列。

换句话说,我收到此错误:

消息 207,级别 16,状态 1,第 6 行无效的列名称“总计”。

Select 
    ProductGroup,
    Origin,
    Destination,
    [YEAR], 
    [Total],
    Isnull([Jan],0) as "Jan",
    isnull([Feb],0) as "Feb",
    isnull([Mar],0) as "Mar",
    isnull([Apr],0) as "Apr",
    isnull([May],0) as "May",
    isnull([Jun],0) as "Jun",
    isnull([Jul],0) as "Jul",
    isnull([Aug],0) as "Aug",
    isnull([Sep],0) as "Sep",
    isnull([Oct],0) as "Oct",
    isnull([Nov],0) as "Nov",
    isnull([Dec],0) as "Dec"
FROM
(
SELECT 
    p.ProductGroup, 
    S.Origin, 
    S.FinalDest AS Destination,
    SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3) as MonthAbreviated, 
    Year(BolDate) AS [Year], 
    Count(*) AS [Total]
--  ,COUNT(S.Purchase#) AS [TheTotal]
    FROM 
        dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
        INNER JOIN dbo.Products as p ON pu.Product = p.Product 
        INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
WHERE 
    Year(BolDate)<>1994 AND
    pu.Cancelled=0 AND S.[Status]='L'
GROUP BY p.ProductGroup, S.Origin, S.FinalDest,Year(BolDate), SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3)

) AS SourceTable
PIVOT
(
sum( Total )
FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) AS PivotTable
4

2 回答 2

1

如果您想要该行的总计,您只需将各列相加即可获得总计。因此,您将需要添加以下行:

([Jan] + [Feb] + [Mar] + [Apr] + [May] + [Jun] + [Jul] + [Aug] + [Sep] + [Oct] + [Nov] + [Dec]) as Total

进行查询:

Select 
    ProductGroup,
    Origin,
    Destination,
    [YEAR], 
    Isnull([Jan],0) as "Jan",
    isnull([Feb],0) as "Feb",
    isnull([Mar],0) as "Mar",
    isnull([Apr],0) as "Apr",
    isnull([May],0) as "May",
    isnull([Jun],0) as "Jun",
    isnull([Jul],0) as "Jul",
    isnull([Aug],0) as "Aug",
    isnull([Sep],0) as "Sep",
    isnull([Oct],0) as "Oct",
    isnull([Nov],0) as "Nov",
    isnull([Dec],0) as "Dec",
    ([Jan] + [Feb] + [Mar] + [Apr] + [May] + [Jun] + [Jul] + [Aug] + [Sep] + [Oct] + [Nov] + [Dec]) as Total
FROM
(
SELECT 
    p.ProductGroup, 
    S.Origin, 
    S.FinalDest AS Destination,
    SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3) as MonthAbreviated, 
    Year(BolDate) AS [Year], 
    Count(*) AS [Total]
--  ,COUNT(S.Purchase#) AS [TheTotal]
    FROM 
        dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
        INNER JOIN dbo.Products as p ON pu.Product = p.Product 
        INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
WHERE 
    Year(BolDate)<>1994 AND
    pu.Cancelled=0 AND S.[Status]='L'
GROUP BY p.ProductGroup, S.Origin, S.FinalDest,Year(BolDate), SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3)

) AS SourceTable
PIVOT
(
sum( Total )
FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) AS PivotTable
于 2012-06-21T14:22:46.950 回答
1

作为替代方案,您也可以使用ROLLUP

你需要

  • 添加一个 case 语句为汇总提供一个“名称”

     CASE WHEN (GROUPING(Substring(DateName(m,boldate),1,3) = 1) Then 'Total'
          ELSE Substring(DateName(m,boldate),1,3) 
     END as MonthAbreviated, 
    
  • 将 RollUp 添加到组中

     GROUP BY
      ....
      Substring(DateName(m,boldate),1,3) 
      WITH ROLLUP
    
  • 将其包含在您的 FOR 子句中

     ... [Nov],[Dec], [Total])
    
  • 排除 Have 中的所有其他汇总分组

          HAVING
             GROUPING (Year(BolDate) = 0)
    

注意我已经修改了您的 MonthAbreviated 语法以缩短它,我不得不更改 Count(*) 的别名,因为它与枢轴值具有相同的名称

Select 
    ProductGroup,
    Origin,
    Destination,
    [YEAR], 
    Isnull([Jan],0) as "Jan",
    isnull([Feb],0) as "Feb",
    isnull([Mar],0) as "Mar",
    isnull([Apr],0) as "Apr",
    isnull([May],0) as "May",
    isnull([Jun],0) as "Jun",
    isnull([Jul],0) as "Jul",
    isnull([Aug],0) as "Aug",
    isnull([Sep],0) as "Sep",
    isnull([Oct],0) as "Oct",
    isnull([Nov],0) as "Nov",
    isnull([Dec],0) as "Dec",
    isnull([Total],0) as "Total"
FROM
(
SELECT 
    p.ProductGroup, 
    S.Origin, 
    S.FinalDest AS Destination,
    CASE WHEN (GROUPING(Substring(DateName(m,boldate),1,3) = 1) Then 'Total'
         ELSE Substring(DateName(m,boldate),1,3) 
    END as MonthAbreviated, 
    Year(BolDate) AS [Year], 
    Count(*) AS [Kount]
    FROM 
        dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
        INNER JOIN dbo.Products as p ON pu.Product = p.Product 
        INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
WHERE 
    Year(BolDate)<>1994 AND
    pu.Cancelled=0 AND S.[Status]='L'
GROUP BY 
     p.ProductGroup, 
     S.Origin, 
     S.FinalDest,Year(BolDate), 
     Substring(DateName(m,boldate),1,3) 
     WITH ROLLUP
HAVING
    GROUPING (Year(BolDate) = 0)
    

) AS SourceTable
PIVOT
(
sum( Kount )
FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec], [Total])
) AS PivotTable

这是 data.se 上该技术的演示

于 2012-06-21T15:19:05.867 回答