1

I'm working on website where I need to find rank of user on the basis of score. Earlier I'm calculating the score and rank of user by sql query .

select * from (
    select
        usrid,
        ROW_NUMBER()
        OVER(ORDER BY (count(*)+sum(sup)+sum(opp)+sum(visited)*0.3) DESC) AS rank,
        (count(*)+sum(sup)+sum(opp)+sum(visited)*0.3 ) As score
    from [DB_].[dbo].[dsas]
    group by usrid) as cash
where usrid=@userid

Please don't concentrate more on query because this is only to explain how I select data.

Problem: Now I can't use above query because every time I use rank it need to select rank from dsas table and data of dsas table is increasing day by day and slows down my website.

What I need is select data by above query and insert in another table named as score. Can we do anything like this?

1 Answer 1

2

A better solution is to either include score as a field in your user table or have a separate table for scores. Any time you add new sup, opp, or visited data for a user, also recalculate their score at that time.

Then to get the highest ranking users, you will be able to perform a very simple select statement, ordering by score descending, and only fetching the number of rows you want. It will be very fast.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah i m thinking about that only . My problem is that how to insert data dynamically of 1000 user's selected by above query .
@chetanSaini, you could add an extra query to update score at each location that changes sup,opp, and visited. Or you could add a trigger to the database that will do that automatically.
first we have to create table with fields usrid and score . After that insert all user data then we have to take step suggested by you. So our first priority is to create table with all user data .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.