(T-SQL)增长上升或下降

时间:2017-01-25 17:33:35

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

我试图使本月下达(创建)的订单数量与上个月相比有所上升或下降。

应该如下所示:

SELECT [o].ClientID
    ,[ru].UnitName
    ,YEAR(GETDATE()) AS [Current Year]
    ,[Current Month] = 
        CASE 
            WHEN MONTH(GETDATE()) = 1 THEN 'JANUARY'
            WHEN MONTH(GETDATE()) = 2 THEN 'FEBRUARY'
            WHEN MONTH(GETDATE()) = 3 THEN 'MARCH'
            WHEN MONTH(GETDATE()) = 4 THEN 'APRIL'
            WHEN MONTH(GETDATE()) = 5 THEN 'MAY'
            WHEN MONTH(GETDATE()) = 6 THEN 'JUNE'
            WHEN MONTH(GETDATE()) = 7 THEN 'JULY'
            WHEN MONTH(GETDATE()) = 8 THEN 'AUGUST'
            WHEN MONTH(GETDATE()) = 9 THEN 'SEPTEMBER'
            WHEN MONTH(GETDATE()) = 10 THEN 'OCTOBER'
            WHEN MONTH(GETDATE()) = 11 THEN 'NOVEMBER'
            WHEN MONTH(GETDATE()) = 12 THEN 'DECEMBER'
            ELSE NULL
        END
    ,COUNT([o].OrderID) AS [Current Month Orders Placed]
    ,[Prior Month] = 
        CASE 
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 1 THEN 'JANUARY'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 2 THEN 'FEBRUARY'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 3 THEN 'MARCH'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 4 THEN 'APRIL'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 5 THEN 'MAY'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 6 THEN 'JUNE'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 7 THEN 'JULY'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 8 THEN 'AUGUST'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 9 THEN 'SEPTEMBER'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 10 THEN 'OCTOBER'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 11 THEN 'NOVEMBER'
            WHEN DATEPART(m, DATEADD(m, - 1, getdate())) = 12 THEN 'DECEMBER'
            ELSE NULL
        END
    ,COUNT([o].OrderID) AS [Prior Months Orders Placed]
    ,[Current Month Orders Placed] * [ Prior Months Orders Placed] AS [Comparison Percentage]
FROM Valuations.dbo.[Order] [o](NOLOCK)
    INNER JOIN Valuations.dbo.resnet_unit ru(NOLOCK) ON [o].ClientID = ru.UnitId
WHERE YEAR([o].DateCreated) = YEAR(GETDATE())
    AND MONTH([o].DateCreated) = MONTH(GETDATE())
GROUP BY MONTH([o].DateCreated)
    ,[o].ClientID
    ,[ru].UnitName

2 个答案:

答案 0 :(得分:1)

这应该做你需要的,显然未经测试,因为你没有提供测试数据。

SELECT [o].ClientID
    ,[ru].UnitName
    ,YEAR(GETDATE()) AS [Current Year]

    ,[Current Month] = dateName(MM,GetDate())
    ,sum(case when [o].DateCreated >= dateadd(m,datediff(m,0,getdate()),0)  -- First day of this month
                    and [o].DateCreated < dateadd(m,datediff(m,0,getdate())+1,0) -- First day of next month
                then 1
                else 0
                end) AS [Current Month Orders Placed]

    ,[Prior Month] = dateName(MM,dateadd(MM,-1,GetDate()))
    ,sum(case when [o].DateCreated >= dateadd(m,datediff(m,0,getdate())-1,0)    -- First day of last month
                    and [o].DateCreated < dateadd(m,datediff(m,0,getdate()),0) -- First day of this month
                then 1
                else 0
                end) AS [Prior Months Orders Placed]

    --Current Month Orders Placed
    ,isnull(sum(case when [o].DateCreated >= dateadd(m,datediff(m,0,getdate()),0)   -- First day of this month
                    and [o].DateCreated < dateadd(m,datediff(m,0,getdate())+1,0) -- First day of next month
                then 1.  -- Decimal points ensure the percentage division works by not returning integer values.
                else 0.
                end)
         --Prior Months Orders Placed
        / nullif(sum(case when [o].DateCreated >= dateadd(m,datediff(m,0,getdate())-1,0)    -- First day of last month
                        and [o].DateCreated < dateadd(m,datediff(m,0,getdate()),0) -- First day of this month
                    then 1.
                    else 0.
                    end),0),0) AS [Comparison Percentage]

FROM Valuations.dbo.[Order] [o](NOLOCK)
    INNER JOIN Valuations.dbo.resnet_unit ru(NOLOCK) ON [o].ClientID = ru.UnitId

WHERE [o].DateCreated >= dateadd(m,datediff(m,0,getdate())-1,0) -- First day of last month
    and [o].DateCreated < dateadd(m,datediff(m,0,getdate())+1,0) -- First day of next month

GROUP BY [o].ClientID
        ,[ru].UnitName

答案 1 :(得分:0)

如果DateCreated数据类型不是date,请更改convert(...)数据类型以匹配。

使用with common_table_expression as () row_number() 获取上个月和当月的数据并进行比较:

with cte as (
  select 
        o.ClientId
      , ClientName  = ru.UnitName
      , [Year]      = datepart(year,o.CreatedDate)
      , [Month]     = datename(month,o.CreatedDate)
      , [OrderCount]= count(o.OrderId)
      , rn          = row_number() over (
          partition o.ClientId, ru.UnitName
          order by datepart(year,o.CreatedDate),datename(month,o.CreatedDate)
        )
    from Valuations.dbo.Order o(nolock)
        inner join Valuations.dbo.resnet_unit ru(nolock) on o.ClientId = ru.UnitId
    where o.DateCreated >= /* First day of last month */
      convert(date,dateadd(month, datediff(month, 0, getdate())-1, 0))
    group by 
        o.ClientId
      , o.ClientName
      , ru.UnitName
      , datepart(year,o.CreatedDate)
      , datename(month,o.CreatedDate)
)
select 
    c.ClientId
  , c.ClientName
  , c.[Year]
  , c.[Month]
  , [Orders Placed] = c.OrderCount
  , [Prior Month] = p.[Month]
  , [Prior Month Count]
  , [Comparison Percentage] = c.OrderCount / (nullif(p.OrderCount ,0)+0.0)
from (select * from cte where rn = 1) as c
  left join (select * from cte where rn = 2) as p
    on c.ClientId = p.ClientId