0

I want to calculate the percentage change of some statistics id using SQLSEVER.

enter image description here

I want to calculate %change using = ((curr-prev)/prev) * 100

My %change expected there was 2.63% since (400008/15189461)* 100 but I am getting result as 0.00%.

select 
  c.poscount as curr, 
  p.poscount as 'prev', 
  (p.PosCount - c.PosCount) 'difference', 
  CONVERT(
    decimal(2, 2), 
    (p.PosCount - c.PosCount)/ NULLIF(p.PosCount, 0)
  )   as   'Poscountchange' 
from 
  (
    select 
      * 
    from 
      Goker.dbo.LocalStatisticsDetail_daily 
    where 
      statisticid = '13527'
  )   c 
  inner join (
    select 
      * 
    from 
      Goker.dbo.LocalStatisticsDetail_daily 
    where 
      statisticid = '13373'
  ) p on c.fieldname = p.fieldname

Why I am getting 0% as percentage change? I didn't wanted to change to decimal at first and my result was 0% before converting it into decimal.

Random guy
  • 883
  • 3
  • 11
  • 32
  • Side note, don't use single quotes (`'`) for aliases. Single quotes are for literal strings, not delimit identifying object names. They can also result in some "gotchas" as their behaviour is not consistent depending on where they are referenced. Also some syntaxes with literal string aliases are deprecated. Stick to object and alias names that don't need delimit identifying, and if you *must* delimit identify them use the T-SQL identifier, brackets (`[]`), or ANSI-SQL's, double quotes (`"`). – Thom A Oct 28 '22 at 09:03

0 Answers0