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()toSELECTtheweekdayvalue for theStartDate. - Use
SUM()andDATEDIFF()to calculateTotalTripHours. (beginning with seconds). - Group by the
DATENAME()result and summarizeTotalTripHours. - Order
TotalTripHoursin 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()toSELECTthe date portion ofStartDate. - Use
DATENAME()andCONVERT()to select theWEEKDAY. - Use
WHEREto 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
timevariable named@ShiftStartTimeand set initial value to'08:00 AM'. - Create a
datevariable named@StartDateand set it to the firstStartDatefrom theBikeSharetable. - Create a
datetimevariable named`@ShiftStartDateTime. - Change
@StartDateand@ShiftStartTimeto 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
DECLAREto create aTABLEvariable named@Shifts - The
@Shiftstable variable should have the following columns -StartDateTimeandEndDateTime- both ofdatetimedata 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
TABLEvariable named@RideDateswith the following columnsRideStartandRideEnd. - Both table variable columns should be
datedata type. SELECTthe unique values ofStartDateandEndDatefrom theCapitalBikeSharetable.CASTthem fromdatetimetodatedata 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