1

Need a little help converting this:

Source

to...

Destination

where

Percentage is simply N / (D - E)

I did read this, this and few other posts.
Is there a simpler way to add compute to the transposed columns !?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
WeShall
  • 409
  • 7
  • 20

2 Answers2

3

Just another option

Select *
      ,Pct = ( IsNull([N],0)+0.0) / NullIf((IsNull([D],0)-IsNull([E],0) ),0)
 From  YourTable A
 Pivot ( sum(Users) for Marker in ([D],[E],[N]) ) pvt

EDIT - Corrected for NULLS

John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
1

I would use a subquery or CTE:

select d.*,
       n * 1.0 / nullif(d - e, 0) as ratio
from (select date,
             sum(case when market = 'D' then users else 0 end) as d,
             sum(case when market = 'E' then users else 0 end) as e,
             sum(case when market = 'N' then users else 0 end) as n
      from t
      group by date
     ) d;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks Gordon. I referred to some of your previous answers / comments on the topic. That was helpful too. – WeShall Sep 24 '19 at 19:58