开发者

How to write the following SQL case statement?

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= ca开发者_高级运维se  
            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 NULL and 1 will be NOT NULL.

select 
         @min_points =max(case
            when var_name='min_points' then var_value
        end),
        @max_points=max(case
            when var_name='max_points' then var_value
        end),
        @avg_points=max(case
            when var_name='avg_points' then var_value
        end),
        @total_points= max(case  
            when var_name='total_points' then var_value
        end)
    from 
        **joined multiple tables**

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.

   @min_points = CASE
                   WHEN var_name = 'min_points' THEN var_value
                   ELSE @min_points
                 END


create table #activity(
ID  int,
var_name  varchar(20),
var_value  int,
)
INSERT INTO #activity VALUES(1,'min_points',20);
INSERT INTO #activity VALUES(2,'max_points',120);
INSERT INTO #activity VALUES(3,'avg_points',50);
INSERT INTO #activity VALUES(4,'total_points',320);

select MAX(CASE WHEN var_name='min_points' THEN  var_value end)as min_points, 
    MAX(CASE WHEN var_name='max_points' THEN  var_value end)as max_points,
    MAX(CASE WHEN var_name='avg_points' THEN  var_value end)as avg_points,
    MAX(CASE WHEN var_name='total_points' THEN  var_value end)as total_points
    from  #activity

Drop Table #activity;


You'd probably be better off doing this in multiple queries

select @min_points = var_value from activity where varmane = 'min_points'
select @max_points = var_value from activity where varmane = 'max_points'
select @avg_points = var_value from activity where varmane = 'avg_points'
select @total_points = var_value from activity where varmane = 'total_points'


You have to use it like this:

  case (var_name)
    case 'min_points'
      var_value
    case 'max_points'
      var_value
    default
      var_value
  end

The thing is though, it looks like all of your cases give the same result, so I don't understand what you are trying to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜