Aan de slagGa gratis aan de slag

Inline TVF

Create an inline table value function that returns the number of rides and total ride duration for each StartStation where the StartDate of the ride is equal to the input parameter.

Deze oefening maakt deel uit van de cursus

Writing Functions and Stored Procedures in SQL Server

Cursus bekijken

Oefeninstructies

  • Create a function named SumStationStats that has one input parameter of type datetime - @StartDate - and returns a TABLE data type.
  • Calculate the total RideCount using COUNT() and ID.
  • Calculate the TotalDuration using SUM() and DURATION.
  • Group by StartStation.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

-- Create the function
___ ___ SumStationStats(___ AS ___)
-- Specify return data type
___ ___
AS
RETURN
SELECT
	StartStation,
    -- Use COUNT() to select RideCount
	___(___) AS RideCount,
    -- Use SUM() to calculate TotalDuration
    ___(___) AS TotalDuration
FROM CapitalBikeShare
WHERE CAST(StartDate as Date) = @StartDate
-- Group by StartStation
___ ___ ___;
Code bewerken en uitvoeren