SQL有sum group by

时间:2014-04-15 16:47:00

标签: sql sql-server sql-server-2008

使用Having,Group By的SQL语法。我需要修复语法。预期输出以图像格式显示。

 select     
            DPC.EnglishProductCategoryName,
            DP.ListPrice,
            DST.SalesTerritoryGroup
from        FactInternetSales FIS
inner join  DimProduct DP
on          FIS.ProductKey = DP.ProductKey
inner join  DimSalesTerritory DST
on          FIS.SalesTerritoryKey = DST.SalesTerritoryKey
inner join  DimProductSubcategory DPSC
on          DPSC.ProductSubcategoryKey = DP.ProductSubcategoryKey
inner join  DimProductCategory DPC
on          DPC.ProductCategoryKey = DPSC.ProductCategoryKey
where       DPC.ProductCategoryKey in (1,2,3)
Having sum(DP.ListPrice)
Group by DPC.EnglishProductCategoryName

enter image description here

2 个答案:

答案 0 :(得分:1)

select      DPC.EnglishProductCategoryName,
            DST.SalesTerritoryGroup,
            CAST(SUM(DP.ListPrice) AS INT) AS ListPrice_Total
from        FactInternetSales FIS
inner join  DimProduct DP
on          FIS.ProductKey = DP.ProductKey
inner join  DimSalesTerritory DST
on          FIS.SalesTerritoryKey = DST.SalesTerritoryKey
inner join  DimProductSubcategory DPSC
on          DPSC.ProductSubcategoryKey = DP.ProductSubcategoryKey
inner join  DimProductCategory DPC
on          DPC.ProductCategoryKey = DPSC.ProductCategoryKey
where       DPC.ProductCategoryKey in (1,2,3)
Group by DPC.EnglishProductCategoryName, DST.SalesTerritoryGroup

我将值转换为整数,只是为了避免小数值。您也可以直接在SSRS中执行此操作。如果你只是想通过使用SSMS来提取这些数据,那么你应该看一下Pivoting,这是我在Pivot上写的一篇文章。 http://sqlsaga.com/sql-server/how-to-use-pivot-to-transform-rows-into-columns-in-sql-server/

会出现这样的事情。

SELECT *
FROM
(
select     
            DPC.EnglishProductCategoryName,
            DST.SalesTerritoryGroup,
            DP.ListPrice
from        FactInternetSales FIS
inner join  DimProduct DP
on          FIS.ProductKey = DP.ProductKey
inner join  DimSalesTerritory DST
on          FIS.SalesTerritoryKey = DST.SalesTerritoryKey
inner join  DimProductSubcategory DPSC
on          DPSC.ProductSubcategoryKey = DP.ProductSubcategoryKey
inner join  DimProductCategory DPC
on          DPC.ProductCategoryKey = DPSC.ProductCategoryKey
where       DPC.ProductCategoryKey in (1,2,3)
) a
PIVOT (SUM(ListPrice) FOR SalesTerritoryGroup IN ([North America], [Europe], [Pacific]))pvt

由于您限制了产品类别,我认为您的预期产量不会被看到。

答案 1 :(得分:-1)

SELECT DPC.EnglishProductCategoryName,
    SUM(CASE WHEN DST.SalesTerritoryGroup = 'North America' THEN DP.ListPrice ELSE 0 END as NorthAmerica,
    SUM(CASE WHEN DST.SalesTerritoryGroup = 'Europe' THEN DP.ListPrice ELSE 0 END as Europe,
    SUM(CASE WHEN DST.SalesTerritoryGroup = 'Pacific' THEN DP.ListPrice ELSE 0 END as Pacific
FROM FactInternetSales FIS
INNER JOIN DimProduct DP ON FIS.ProductKey = DP.ProductKey
INNER JOIN DimSalesTerritory DST on FIS.SalesTerritoryKey = DST.SalesTerritoryKey
INNER JOIN DimProductSubcategory DPSC  on DPSC.ProductSubcategoryKey = DP.ProductSubcategoryKey
INNER JOIN DimProductCategory DPC on DPC.ProductCategoryKey = DPSC.ProductCategoryKey
WHERE DPC.ProductCategoryKey in (1,2,3)
GROUP BY DPC.EnglishProductCategoryName, DST.SalesTerritoryGroup