SQL Server:计算每月总销售额,包括空月

时间:2022-05-02 07:27:58

I'm trying to calculate the total sales of a product in a month, but I would like it to include any "empty" months (with no sales) and only select the latest 12 months.

我试图计算一个月内产品的总销售额,但我希望它包括任何“空”月(没有销售),只选择最近12个月。

This is my code so far.

到目前为止这是我的代码。

declare
@ProductNo int

set @ProductNo = 1234

SELECT 
YEAR(o.OrderDate) as 'Year', MONTH(o.OrderDate) as 'Month', sum(Amount) as 'Units sold',[ProductNo]

  FROM [OrderLine] ol
  inner join [Order] o on ol.OrderNo = o.OrderNo
  where ProductNo = @ProductNo

  Group by ProductNo, YEAR(o.OrderDate), Month(o.OrderDate)
  Order by ProductNo, YEAR(o.OrderDate), Month(o.OrderDate)

This returns

Year    Month   Units sold
2011    6       2
2011    10      1
2011    11      1
2012    2       1

But I would like it to return.

但我希望它能够回归。

Year    Month   Units sold
2011    4       0
2011    5       0
2011    6       2
2011    7       0
2011    8       0    
2011    9       0
2011    10      1
2011    11      1
2011    12      0
2012    1       0
2012    2       2
2012    3       0

I'm using SQL Server 2008 R2 Sp1

我正在使用SQL Server 2008 R2 Sp1

2 个解决方案

#1


2  

I've done before I know that you have calendar table. I've used master.dbo.spt_values to generate last twelve consecutive months (including current).

在我知道你有日历表之前我已经完成了。我已经使用master.dbo.spt_values连续生成了12个月(包括当前)。

    declare @ProductNo int

    set @ProductNo = 1234

select MONTH(d.date), YEAR(d.date), isnull(t.amnt, 0) as [Units sold] from (
    SELECT
        YEAR(o.OrderDate) as 'Year', 
        MONTH(o.OrderDate) as 'Month', 
        sum(Amount) as amnt,
        [ProductNo]
    FROM [OrderLine] ol
    inner join [Order] o on ol.OrderNo = o.OrderNo
    where ProductNo = @ProductNo
    group by ProductNo, YEAR(o.OrderDate), Month(o.OrderDate)
) t
right join (
    select dateadd(mm, -number, getdate()) as date
    from master.dbo.spt_values 
    where type = 'p' and number < 12
) d  on year(d.date) = t.[year] and month(d.date) = t.[month]
order by YEAR(d.date), MONTH(d.date)

#2


1  

Try:

;with CTE as 
(select 0 months_ago union all 
 select months_ago - 1 months_ago from CTE where months_ago > -11),
month_list as 
(select dateadd(MONTH, 
                months_ago, 
                dateadd(DAY, 
                        1-datepart(DAY,getdate()),
                        cast(GETDATE() as DATE))) month_start 
 from cte)
SELECT YEAR(ml.start_date) as 'Year', 
       MONTH(ml.start_date) as 'Month', 
       sum(Amount) as 'Units sold',[ProductNo]
FROM month_list ml
left join [Order] o 
       on o.OrderDate >= ml.start_date and 
          o.OrderDate < dateadd(MONTH, 1, ml.start_date)
left join [OrderLine] ol 
       on ol.OrderNo = o.OrderNo and ProductNo = @ProductNo
Group by ProductNo, YEAR(ml.start_date), Month(ml.start_date)
Order by ProductNo, YEAR(ml.start_date), Month(ml.start_date)

#1


2  

I've done before I know that you have calendar table. I've used master.dbo.spt_values to generate last twelve consecutive months (including current).

在我知道你有日历表之前我已经完成了。我已经使用master.dbo.spt_values连续生成了12个月(包括当前)。

    declare @ProductNo int

    set @ProductNo = 1234

select MONTH(d.date), YEAR(d.date), isnull(t.amnt, 0) as [Units sold] from (
    SELECT
        YEAR(o.OrderDate) as 'Year', 
        MONTH(o.OrderDate) as 'Month', 
        sum(Amount) as amnt,
        [ProductNo]
    FROM [OrderLine] ol
    inner join [Order] o on ol.OrderNo = o.OrderNo
    where ProductNo = @ProductNo
    group by ProductNo, YEAR(o.OrderDate), Month(o.OrderDate)
) t
right join (
    select dateadd(mm, -number, getdate()) as date
    from master.dbo.spt_values 
    where type = 'p' and number < 12
) d  on year(d.date) = t.[year] and month(d.date) = t.[month]
order by YEAR(d.date), MONTH(d.date)

#2


1  

Try:

;with CTE as 
(select 0 months_ago union all 
 select months_ago - 1 months_ago from CTE where months_ago > -11),
month_list as 
(select dateadd(MONTH, 
                months_ago, 
                dateadd(DAY, 
                        1-datepart(DAY,getdate()),
                        cast(GETDATE() as DATE))) month_start 
 from cte)
SELECT YEAR(ml.start_date) as 'Year', 
       MONTH(ml.start_date) as 'Month', 
       sum(Amount) as 'Units sold',[ProductNo]
FROM month_list ml
left join [Order] o 
       on o.OrderDate >= ml.start_date and 
          o.OrderDate < dateadd(MONTH, 1, ml.start_date)
left join [OrderLine] ol 
       on ol.OrderNo = o.OrderNo and ProductNo = @ProductNo
Group by ProductNo, YEAR(ml.start_date), Month(ml.start_date)
Order by ProductNo, YEAR(ml.start_date), Month(ml.start_date)