Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Create a function ufn_CashInUsersGames that sums the cash of odd rows. Rows must be ordered by cash in descending order. The function --should take a game name as a parameter and return the result as table. Submit only your function in.
- --Execute the function over the following game names, ordered exactly like: "Love in a mist".
- CREATE OR ALTER FUNCTION ufn_CashInUsersGames (@GameName nvarchar(100))
- RETURNS TABLE
- AS
- RETURN
- (
- WITH CTE (Name, GameId, CASH, Row#)
- AS
- (
- SELECT Games.Name, UsersGames.GameId, UsersGames.CASH,
- ROW_NUMBER() OVER(ORDER BY UsersGames.CASH DESC) AS Row#
- FROM UsersGames
- JOIN Games ON Games.ID = UsersGames.GameId
- WHERE Games.Name = @GameName
- )
- SELECT SUM(CTE.CASH) AS SumCash
- FROM CTE
- WHERE Row# % 2 = 1
- )
- GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement