0

我有 2 张表付款和客户。

我们有 5 种类型的客户 1,2,3,4,5。

我想在给定年份的每个月按客户类型(1、2、3、4、5)获得总付款。

如果任何客户类型没有任何付款,则应为 0。

以下是我当前的查询:-

 SELECT 
                    "Month" = month(o.PaymentDate)
                     , "Year" = year(o.PaymentDate)
                     , Amount = sum(o.Amount)
                     , c.CustomerTypeID                  
                FROM
                    Payments o
                    INNER JOIN
                      Customers c ON o.CustomerID = c.CustomerID
                WHERE
                    o.PaymentDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                    AND o.PaymentDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
                GROUP BY
                    month(o.PaymentDate)
                  , year(o.PaymentDate)
                  ,c.CustomerTypeID     
                ORDER BY
                    year(o.PaymentDate)
                  , month(o.PaymentDate)
                      ,c.CustomerTypeID

结果是:-

月 年 金额 CustomerTypeID 1 2013 456 1 1 2013 678 2 1 2013 346 3 1 2013 3245 5

由于目前它没有提供 CustomerType 4 的数据,所以我想在金额列中显示 0,月份和年份将相同。

有谁能够帮我。

提前致谢。

4

3 回答 3

1
SELECT 
    2013 as year,
    months.monthno,
    Amount = isnull(sum(o.Amount),0),
    c.CustomerTypeID  
FROM
    customers c 
         cross join     
    (select number monthNo from master..spt_values where type='p' and number between 1 and 12) months
         left join payments o
     ON o.CustomerID = c.CustomerID
     AND year(o.PaymentDate)=2013
     AND month(o.PaymentDate) = months.monthNo
GROUP BY
    months.monthno, c.CustomerTypeID
ORDER BY
    months.monthno, c.CustomerTypeID
于 2013-10-19T09:55:11.657 回答
0

当您使用WHEREwith 聚合时,您应该认真考虑使用GROUP BY ALL而不是GROUP BY

来自 -MS Technet

如果使用 ALL,则查询结果包括由 GROUP BY 子句生成的所有组,即使某些组没有满足搜索条件的行。如果没有 ALL,则包含 GROUP BY 的 SELECT 语句不会显示没有行符合条件的组。

SELECT 
                    "Month" = month(o.PaymentDate)
                     , "Year" = year(o.PaymentDate)
                     , Amount = sum(o.Amount)
                     , c.CustomerTypeID                  
                FROM
                    Payments o
                    INNER JOIN
                      Customers c ON o.CustomerID = c.CustomerID
                WHERE
                    o.PaymentDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                    AND o.PaymentDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
                GROUP BY ALL
                    month(o.PaymentDate)
                  , year(o.PaymentDate)
                  ,c.CustomerTypeID     
                ORDER BY
                    year(o.PaymentDate)
                  , month(o.PaymentDate)
                      ,c.CustomerTypeID
于 2013-10-19T09:58:51.230 回答
0

使用右外连接让所有客户和 Coalesce(amount,0) 返回 0 而不是 null。

SELECT 
                      c.CustomerTypeID                  
                FROM
                    (SELECT "Month" = month(o.PaymentDate)
                             , "Year" = year(o.PaymentDate)
                             , Amount = sum(o.Amount) 
                             , o.CustomerID
                    FROM Payments o
                    WHERE
                    o.PaymentDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                    AND o.PaymentDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
                    ) D
                    RIGHT OUTER JOIN
                      Customers c ON o.CustomerID = c.CustomerID
                GROUP BY
                    D.MONTH
                  , D.YEAR
                  ,c.CustomerTypeID     
                 ORDER BY
                       D.MONTH
                      , D.YEAR
                      ,c.CustomerTypeID
于 2013-10-19T09:25:33.183 回答