Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4001526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:54:57+00:00 2026-05-20T07:54:57+00:00

I have resultset like – Id var_name var_value 1 min_points 20 2 max_points 120

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:54:57+00:00Added an answer on May 20, 2026 at 7:54 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq to Entities query like this one: var results = from
I have a simple SQL 'Select' query, and I'd like to dump the results
I have a query like this: SELECT title,id FROM table1 WHERE id IN ('2','7','4','10')
I have the following schema, and would like to do a query that returns
i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
Using DBIx::Class and I have a resultset which needs to be filtered by data
I have SQL 2008 (not R2). I would like to have a matrix report
I have one query that returns string value, my query is select case when
I have a class MyClass that has a static variable. Class implements smth like
I have a result set in MS-SQL within a stored procedure, and lets say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.