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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:09:45+00:00 2026-06-01T18:09:45+00:00

I have not ever worked on triggers in SQL Server 2008. So I don’t

  • 0

I have not ever worked on triggers in SQL Server 2008. So I don’t have any idea how to do it.

I have a game application, I’m keeping track of the number of questions played & total number of questions answered correctly. I’m also saving the average time taken for each question played.

Now I want to record the rank depending upon percentage of answers correct and If there is a tie we will check the time taken. I have to do all this using triggers.

  id   | playerID | CompetitionID  | NoOfCOrrectANswers | NoOfPlayedQuestions | TimeTaken 
-----------------------------------------------------------------------------------------
  1      3             203              4                       8                  8.4
  2      56            203              9                      18                 13
  3      67            203              16                     45                 15

Any help is appreciated. Thanks in advance.

  • 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-01T18:09:47+00:00Added an answer on June 1, 2026 at 6:09 pm

    You probably should not take this road as number of updates will be high. You might add row_number() or rank() function to a query that presents results of a competition.

    That being said, if you feel that you absolutely must persist ranking info in a table, add a column

    alter table ATable add PlayerRank int
    

    and create a trigger on ATable

    create trigger RankingTrigger on ATable
    after insert, update, delete
    as
    -- We don't want queries in a trigger to mess up with "Rows affected"
       set nocount on
    -- If any of the following columns are mentioned in a query
    -- Always true for insert and delete, but we will save updates
    -- if we skip processing when columns participating in ranking 
    -- are not changed
       If update (NoOfCOrrectANswers)
          OR update(NoOfPlayedQuestions)
          OR update(TimeTaken)
       begin
          -- CTE that returns primary key and rank by competition.
          -- I've changed your condition (percent of correct answers)
          -- As it would rank players with one correct answer over those
          -- who answered 100 questions and got only one wrong. If you
          -- want to change it, replace NoOfCOrrectANswers with 
          -- NoOfCOrrectANswers / NoOfPlayedQuestions
          ; with theRank as (
            select ID, row_number() over (partition by CompetitionID
                                          order by NoOfCOrrectANswers DESC,
                                                   TimeTaken DESC) rn
              from ATable
          -- Only changed competitions
             where CompetitionID in
          -- Inserted table is available in trigger and OUTPUT clause
          -- of insert, update, delete statements.
          -- It contains newly added/changed rows
          -- We are using it here to filter only changed competitions
          -- Similary, Deleted table hold a copy of removed rows for delete 
          -- and old values for update
             (
                select CompetitionID
                  from Inserted
                union
                select CompetitionID
                  from Deleted
             )
          )
          update ATable
          -- Update to rank from theRank CTE
             set PlayerRank = theRank.rn
            from ATable
            -- All records participation in affected competitions
              inner join theRank
                 on ATable.ID = theRank.ID
            -- Only change if really changed
            -- This part is very likely not needed. I have never tested
            -- to see if it affects performance
                and isnull(ATable.PlayerRank, 0) <> theRank.rn
       end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have anybody ever tried to create thumbnails/previews of MS Office files? I do not
I have not worked with parameters in ADO.Net very much. I'm writing a custom
I have not changed any of the configuration options for sessions in my php.ini.
I have not been able to find any definitive answers to this question: Can
I have not done any animation stuff with iphone development so far. Can anybody
have not tested on windows. but in ubuntu when u disconnect from the network,
I have not used jquery much yet and not very familiar with it. Trying
We have not purchased an SSL certificate so the user of our website has
I have not used many lambda expressions before and I ran into a case
I have not used Twitter enough to become familiar with its terminology or the

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.