IeeTeY
IeeTeY

Reputation: 93

(T-SQL)Rise or Fall In Growth

I am trying to get the Rise or Fall in Growth on the number of orders placed (created) this month compared to the prior month.

It should look like this:

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

Upvotes: 1

Views: 67

Answers (2)

iamdave
iamdave

Reputation: 12243

This should do what you need it to, untested obviously as you have provided no test data.

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

Upvotes: 1

SqlZim
SqlZim

Reputation: 38033

If DateCreated datatype is not date, change the convert(...) datatype to match.

Using with common_table_expression as () and row_number() to get data for the last and current month and compare them:

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

Upvotes: 0

Related Questions