SQL Functions Advance
1] Use CONVERT()
to SELECT
and GROUP BY
the date portion of the StartDate
SELECT
-- Select the date portion of StartDate
CONVERT(DATE, StartDate) as StartDate,
-- Measure how many records exist for each StartDate
COUNT(StartDate) as CountOfRows
FROM CapitalBikeShare
-- Group by the date portion of StartDate
GROUP BY CONVERT(DATE, StartDate)
-- Sort the results by the date portion of StartDate
ORDER BY CONVERT(DATE, StartDate)
2]
- Use
DATENAME()
toSELECT
theweekday
value for theStartDate
. - Use
SUM()
andDATEDIFF()
to calculateTotalTripHours
. (beginning with seconds). - Group by the
DATENAME()
result and summarizeTotalTripHours
. - Order
TotalTripHours
in descending order.
Ans -
SELECT
-- Select the day of week value for StartDate
DATENAME(Weekday, StartDate) as DayOfWeek,
-- Calculate TotalTripHours
SUM(DATEDIFF(ss, StartDate, EndDate))/ 3600 as TotalTripHours
FROM CapitalBikeShare
-- Group by the day of week
GROUP BY DATENAME(WEEKDAY,StartDate)
-- Order TotalTripHours in descending order
ORDER BY TotalTripHours DESC;
3
- Use
SUM()
andDATEDIFF()
to find the Total Ride Hours per day starting from seconds. - Use
CONVERT()
toSELECT
the date portion ofStartDate
. - Use
DATENAME()
andCONVERT()
to select theWEEKDAY
. - Use
WHERE
to only include Saturdays
Answer
SELECT
-- Calculate TotalRideHours using SUM() and DATEDIFF()
SUM(DATEDIFF(SECOND, StartDate, EndDate))/ 3600 AS TotalRideHours,
-- Select the DATE portion of StartDate
CONVERT(DATE, StartDate) AS DateOnly,
-- Select the WEEKDAY
DATENAME(WEEKDAY, CONVERT(DATE, StartDate)) AS DayOfWeek
FROM CapitalBikeShare
-- Only include Saturday
WHERE DATENAME(WEEKDAY, StartDate) = 'Saturday'
GROUP BY CONVERT(DATE, StartDate);
DECLARE VARIABLE Function Syntax
- Create a
time
variable named@ShiftStartTime
and set initial value to'08:00 AM'
. - Create a
date
variable named@StartDate
and set it to the firstStartDate
from theBikeShare
table. - Create a
datetime
variable named`@ShiftStartDateTime
. - Change
@StartDate
and@ShiftStartTime
to datetime data types and assign to@ShiftStartDateTime
Answer -
-- Create @ShiftStartTime
DECLARE @ShiftStartTime AS time = '08:00 AM'
-- Create @StartDate
DECLARE @StartDate AS date
-- Set StartDate to the first StartDate from CapitalBikeShare
SET
@StartDate = (
SELECT TOP 1 StartDate
FROM CapitalBikeShare
ORDER BY StartDate ASC
)
-- Create ShiftStartDateTime
DECLARE @ShiftStartDateTime AS datetime
-- Cast StartDate and ShiftStartTime to datetime data types
SET @ShiftStartDateTime = CAST(@StartDate AS datetime) + CAST(@ShiftStartTime AS datetime)
SELECT @ShiftStartDateTime
Declare Syntax
- Use
DECLARE
to create aTABLE
variable named@Shifts
- The
@Shifts
table variable should have the following columns -StartDateTime
andEndDateTime
- both ofdatetime
data type. - Populate the table variable with the values
'3/1/2018 8:00 AM'
and'3/1/2018 4:00 PM'
.
-- Declare @Shifts as a TABLE
DECLARE @Shifts TABLE(
-- Create StartDateTime column
StartDateTime datetime ,
-- Create EndDateTime column
EndDateTime datetime)
-- Populate @Shifts
INSERT INTO @shifts (StartDateTime, EndDateTime)
SELECT '3/1/2018 8:00 AM', '3/1/2018 4:00 PM'
SELECT *
FROM @Shifts
4.
- Declare a
TABLE
variable named@RideDates
with the following columnsRideStart
andRideEnd
. - Both table variable columns should be
date
data type. SELECT
the unique values ofStartDate
andEndDate
from theCapitalBikeShare
table.CAST
them fromdatetime
todate
data types.- Store the query results in
@RideDates
-- Declare @RideDates
DECLARE @RideDates TABLE(
-- Define RideStart column
RideStart date,
-- Define RideEnd column
RideEnd date)
-- Populate @RideDates
INSERT INTO @RideDates(RideStart, RideEnd)
-- Select the unique date values of StartDate and EndDate
SELECT DISTINCT
-- Cast StartDate as date
CAST(StartDate as date),
-- Cast EndDate as date
CAST(EndDate as date)
FROM CapitalBikeShare
SELECT *
FROM @RideDates
Here you will use the
GETDATE()
, DATEDIFF()
, and DATEADD()
functions to find the first day of the current month.SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
Comments
Post a Comment