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
  • 704,970
  • 168
  • 1,303
  • 1,425
WeShall
  • 409
  • 6
  • 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
  • 71,300
  • 6
  • 42
  • 62
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,198,228
  • 53
  • 572
  • 709
  • 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