I have resultset like –
Id var_name var_value
1 min_points 20
2 max_points 120
3 avg_points 50
4 total_points 320
Here is a query I have tried to write –
select
@min_points =case
when var_name='min_points' then var_value
end,
@max_points=case
when var_name='max_points' then var_value
end,
@avg_points=case
when var_name='avg_points' then var_value
end,
@total_points= case
when var_name='total_points' then var_value
end
from
**joined multiple tables**
But the above query does not work and I can understand why..but can anyone help me write a query that will basically help me store all the four var_values in the four variables I have by checking the var_names ?
You need to get the result set into a single row to avoid assigning to the same variables 4 different times. As it stands for each row in the result set the variables are being assigned which means that after the assignment 3 of them will not meet the condition and be
NULLand 1 will beNOT NULL.Or alternatively you could keep the multiple assignments but just reassign the same value back to the variable if the row being processed is not the row of interest.