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() to SELECT the weekday value for the StartDate.
  • Use SUM() and DATEDIFF() to calculate TotalTripHours. (beginning with seconds).
  • Group by the DATENAME() result and summarize TotalTripHours.
  • 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;


  • Use SUM() and DATEDIFF() to find the Total Ride Hours per day starting from seconds.
  • Use CONVERT() to SELECT the date portion of StartDate.
  • Use DATENAME() and CONVERT() to select the WEEKDAY.
  • 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 first StartDate from the BikeShare 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 a TABLE variable named @Shifts
  • The @Shifts table variable should have the following columns - StartDateTime and EndDateTime - both of datetime 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 columns RideStart and RideEnd.
  • Both table variable columns should be date data type.
  • SELECT the unique values of StartDate and EndDate from the CapitalBikeShare table. CAST them from datetime to date 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

Popular posts from this blog

Binomial Test in Python

Slicing and Indexing in Python Pandas

Python Syntax and Functions Part2 (Summary Statistics)