i have the following table generated by SQL TABLE A
timeinterval count(exclusive range)
0-6 2
0-12 5
0-18 10
i want a table like this TABLE B
timeinterval count(exclusive range) count(inclusive range)
1-6 2 2
1-12 5 3
1-18 10 5
i have already generated table A and need table B. can i do something in SQL where i can add a query in the code for table A and do something like this (0-12)-(0-6) for 2nd row in table B.
code used for generating table A is
with ranges as
(
select 6 as val, 1 as count_all
union all
select 12, 1
union all
select 18, 1
union all
select 24, 1
union all
select 30, 1
union all
select 36, 1
union all
select 42, 1
union all
select 48, 1
union all
select 1, 0
)
select case when ranges.count_all = 0
then 'more'
else convert (varchar(10), ranges.val)
end [MetLifeExperienceMonths],
sum (case when (ranges.count_all = 0 and GoldListHistogram.MetLifeExperienceMonths>=1)
or
(GoldListHistogram.MetLifeExperienceMonths<= ranges.val and GoldListHistogram.MetLifeExperienceMonths>=1)
then 1 end) [count],
count(EmployeeID) as 'Total'
into yy
from GoldListHistogram
cross join ranges
where MetLifeExperienceMonths > 0
group by ranges.val, ranges.count_all
i need to modify the query such that i can subtract first two rows value for “count(exclusive range)” for every row staring from the 2nd row..like for 0-12(time interval) row i need to output a value that is difference of the first two rows..like row(i)=count(i)-count(i-1).
first column gives the time interval in 5 years (in months) second column calculates no. of employees in the exclusive range like (0-6,0-12,0-18)..6 ,12,18 being no. of months third column calculates no. of employees in the exclusive range like (0-6,6-12,12-18)
Could you not just add a start value to ranges? Something like: