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 9145807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:36:20+00:00 2026-06-17T10:36:20+00:00

This query DOES NOT give expected result, What i am trying to do :

  • 0

This query DOES NOT give expected result,

What i am trying to do :

I am trying to rank user based on two different column/field and value. And want to show highest ranking user on top.

I am creating dummy column in select query as RANK and using two CASE … AS RANK.

Is this good practice? Or any better way out?

PS : I really dont want to persist RANK so not storing them, as it will be dynamic from various fields (this is just and example with two fields)

SELECT *, 

CASE EMPLOYEEOF
WHEN 'ASDF' THEN @RANK := @RANK+1 
END AS RANK,

CASE VENDOROF 
WHEN 'WXYZ' THEN @RANK := @RANK+1
END AS RANK

from USERMASTER, (SELECT @RANK := 0) r ORDER BY RANK DESC;

EDIT : DOES NOT GIVE! in line 1

  • 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-06-17T10:36:21+00:00Added an answer on June 17, 2026 at 10:36 am

    If you want to select from a table (USERMASTER), generate a calculated rank and then sort by that given rank, here’s how you can do that effectively.

    1) Determine your ranking criteria – Figure out what matters to your rank, whether it’s money spent, student grade, etc, knowing what your metrics are will help.

    2) Write your logic – CASE statements are great when more than one condition is included. For example if you wanted to give a higher rank to a student with both a GPA above 3.0 and a grade in a class equal to an “A”, but you want to give a lower rank to lower GPA, you might say:

    SELECT CASE WHEN student_grade = 'A'
                AND student_gpa > 3.0
                THEN 4
           ELSE 3
    END rank
    FROM students
    ORDER BY rank DESC
    

    This will create a rank column based on student_grade and student_gpa logic, then sort the students based on this calculated rank in order of highest rank first.

    3) Test your cases – Verify that your logic gave you the results your looking for by reviewing some sample data. For my example (since I’m unaware of your criteria:

    student student_grade   student_gpa
    John    A           3.5
    Mike    B           2.9
    

    My results from running the above (with group by on student_name):

    student_name rank
    John     4
    Mike     3
    

    My results lined up with my expected outcome (John’s rank is higher at 4 and also shows up on top of my list due to order by) and we are done!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this query but this shows column does not exist SELECT p.content, (select
This SQL query does not execute. Can someone give the correct syntax? UPDATE PayrollTotals
I am using Zend_Db with the Pdo_Mysql driver. This query does not give any
I do not understand what is wrong with this query? Query tool does not
This code does not throw an error but the query fails, that is, the
I am trying to optimize a query that does something like this: SELECT ...
We have a registration system Database and basically what this query does is check
Why does this query work normal: Query query = session.createQuery(from Table tab); And this
I cannot understand this query? select @:vfOutBalance+@:vfHSSubTotal+@3+orpartpayment,@:vfOutBalance+@:vfHSSubTotal from or_equip_upgrd_id what does @3 mean?
Currently, I am using this query, which does a pretty good job of finding

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.