From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Calculate global score with custom function

Calculate global score with custom function - SQL Server Tutorial

From the course: Complete Guide to Advanced SQL Server

Calculate global score with custom function

For the next part of our Game Score database project, I'm tasked with writing a custom function that will calculate the global scores that have been earned for all of our players. And we can calculate this by taking each player's score, multiplying it by their bonus multiplier, and then adding up those scores for all of our players. So let's take a look at the state of the table that I have right now, our Players table. I'm just going to execute that now. So we can see that we have currently two players in my database, Alfa and Bravo. And Alfa has a score of 200 right now and Bravo has a score of 100. So the custom function that I'm about to write, hopefully, the number that comes out of it is going to be 300, which would be the global score for all of our players at this point in time. So that's what I'm looking for at the end of this process. So let's go ahead and get started by CREATE OR ALTER FUNCTION and put everything in the game schema. And I'll call my function…

Contents