SQL GROUP BY季度

时间:2015-03-27 09:19:45

标签: sql sql-server group-by

使用Microsot的SQL服务器我想显示每个客户的列表,他们按年和季度花费。所以,我有下一个代码:

    SELECT CompanyName AS 'Customer', YEAR(OrderDate) AS 'Year' ,
    Quarter1 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 1 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END,
    Quarter2 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 2 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END,
    Quarter3 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 3 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END,
    Quarter4 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 4 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END
 FROM Customers LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
 LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID
 GROUP BY CompanyName, YEAR(OrderDate), DATEPART(q, OrderDate)

但它每季度显示一个单行,例如:

Customer    Year    Quarter1    Quarter2    Quarter3   Quarter4
--------    ----    --------    --------    --------   --------
John Smith  1997         127           0           0          0
John Smith  1997           0         254           0          0
John Smith  1997           0           0         547          0
John Smith  1997           0           0           0        155

我想要的是每个cutomer一行,在这种情况下:

Customer    Year    Quarter1    Quarter2    Quarter3   Quarter4
--------    ----    --------    --------    --------   -------- 
John Smith  1997         127         254         547        155

有什么建议吗?

THX。

3 个答案:

答案 0 :(得分:0)

打包您的查询并按客户和年份对其进行分组。像这样......

SELECT Customer,Year,SUM(Quarter1) AS Quarter1,SUM(Quarter2) AS Quarter2,SUM(Quarter3) AS Quarter3,SUM(Quarter4) AS Quarter4
FROM
(
    SELECT CompanyName AS 'Customer', YEAR(OrderDate) AS 'Year' ,
    Quarter1 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 1 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END,
    Quarter2 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 2 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END,
    Quarter3 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 3 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END,
    Quarter4 = CASE(DATEPART(q, Orders.OrderDate))
    WHEN 4 THEN SUM((UnitPrice*Quantity)*(1-Discount))
    ELSE 0
    END
 FROM Customers LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
 LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID
 GROUP BY CompanyName, YEAR(OrderDate), DATEPART(q, OrderDate)
 )C
 GROUP BY CompanyName,Year

答案 1 :(得分:0)

使用公用表格式CTE

  ;with cte1 as
    (
     SELECT CompanyName AS 'Customer', YEAR(OrderDate) AS 'Year' ,
        Quarter1 = CASE(DATEPART(q, Orders.OrderDate))
        WHEN 1 THEN SUM((UnitPrice*Quantity)*(1-Discount))
        ELSE 0
        END,
        Quarter2 = CASE(DATEPART(q, Orders.OrderDate))
        WHEN 2 THEN SUM((UnitPrice*Quantity)*(1-Discount))
        ELSE 0
        END,
        Quarter3 = CASE(DATEPART(q, Orders.OrderDate))
        WHEN 3 THEN SUM((UnitPrice*Quantity)*(1-Discount))
        ELSE 0
        END,
        Quarter4 = CASE(DATEPART(q, Orders.OrderDate))
        WHEN 4 THEN SUM((UnitPrice*Quantity)*(1-Discount))
        ELSE 0
        END
     FROM Customers LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
     LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID
     GROUP BY CompanyName, YEAR(OrderDate), DATEPART(q, OrderDate)
    )
select Customer,max(Year), max(Quarter1),max(Quarter2),max(Quarter3),max(Quarter4) from cte1 group by Customer

答案 2 :(得分:0)

简单地按四分之一部分删除该组,并将总和包裹在您的案例中:

SELECT CompanyName AS 'Customer', YEAR(OrderDate) AS 'Year' 
 , Quarter1 = SUM(CASE(DATEPART(q, Orders.OrderDate))
                    WHEN 1 THEN (UnitPrice*Quantity)*(1-Discount)
                    ELSE 0
                  END),
 , Quarter2 = SUM(CASE(DATEPART(q, Orders.OrderDate))
                    WHEN 2 THEN (UnitPrice*Quantity)*(1-Discount)
                    ELSE 0
                  END),
 , Quarter3 = SUM(CASE(DATEPART(q, Orders.OrderDate))
                    WHEN 3 THEN (UnitPrice*Quantity)*(1-Discount)
                    ELSE 0
                  END),
 , Quarter4 = SUM(CASE(DATEPART(q, Orders.OrderDate))
                    WHEN 4 THEN (UnitPrice*Quantity)*(1-Discount)
                    ELSE 0
                  END),
 FROM Customers LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
 LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID
 GROUP BY CompanyName, YEAR(OrderDate)