Introducere în curs

Scrierea funcțiilor și a procedurilor stocate în SQL Server

Meghan Kwartler

IT Consultant

Obiectivele cursului

  • Analiza exploratorie temporală a datelor (EDA) cu funcții SQL de dată
  • Crearea, actualizarea și executarea funcțiilor definite de utilizator
  • Crearea, actualizarea și executarea procedurilor stocate
  • Cunoștințe prealabile
    • Introducere în SQL Server
    • Îmbinarea datelor în SQL
    • SQL Server intermediar
Scrierea funcțiilor și a procedurilor stocate în SQL Server

EDA temporală

  • Seturi de date tranzacționale
    • CapitalBikeShare
    • YellowTripTaxi
  • Procesul de analiză exploratorie a datelor (EDA)
    • Iterativ
    • Fără o listă fixă de întrebări EDA
    • Fiți curioși!
    • Reduce efortul de reluare a lucrului

Vizualizare EDA

Scrierea funcțiilor și a procedurilor stocate în SQL Server

Funcții SQL pentru EDA

-- CONVERT Syntax:  
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )  
-- Returns expression based on data_type
-- DATEPART Syntax
DATEPART ( datepart , date ) 
-- Returns int
-- DATENAME Syntax
DATENAME ( datepart , date ) 
-- Returns nvarchar
-- DATEDIFF Syntax
DATEDIFF ( datepart , startdate , enddate ) 
-- Returns int; can't use datepart weekday value
-- datepart values = year, quarter, month, dayofyear, day, week, weekday, hour, 
-- minute, second, microsecond, nanosecond
Scrierea funcțiilor și a procedurilor stocate în SQL Server
-- CONVERT
SELECT 
  TOP 1 PickupDate, 
  CONVERT (DATE, PickupDate) AS DateOnly 
FROM YellowTripData
+-----------------------------+------------+
| PickupDate                  | DateOnly   |
|-----------------------------+------------|
| 2017-01-09 11:52:13.0000000 | 2017-01-09 |
+-----------------------------+------------+
Scrierea funcțiilor și a procedurilor stocate în SQL Server
-- DATEPART
SELECT 
  TOP 3 COUNT(ID) AS NumberofRides, 
  DATEPART(HOUR, PickupDate) AS Hour 
FROM YellowTripData 
GROUP BY DATEPART(HOUR, PickupDate) 
ORDER BY COUNT(ID) DESC
+----------------------+
| NumberOfRides | Hour |
|----------------------+
| 616281        | 18   |
| 616281        | 19   |
| 540629        | 17   |
+---------------+------+
Scrierea funcțiilor și a procedurilor stocate în SQL Server
--DATENAME
SELECT 
  TOP 3 ROUND(
    SUM(FareAmount), 
    0
  ) as TotalFareAmt, 
  DATENAME(WEEKDAY, PickupDate) AS DayofWeek 
FROM YellowTripData 
GROUP BY DATENAME (WEEKDAY, PickupDate) 
ORDER BY SUM(FareAmount) DESC;
+--------------------------+
| TotalFareAmt | DayofWeek |
|--------------------------+
| 19026645     | Sunday    |
| 18749482     | Tuesday   |
| 17213351     | Thursday  |
+--------------+-----------+
Scrierea funcțiilor și a procedurilor stocate în SQL Server
--DATEDIFF
SELECT 
  AVG(
    DATEDIFF(SECOND, PickupDate, DropOffDate)/ 60
  ) AS AvgRideLengthInMin 
FROM YellowTripData 
WHERE DATENAME(WEEKDAY, PickupDate) = 'Sunday';
+--------------------+
| AvgRideLengthInMin |
|--------------------+
| 13                 |
+--------------------+
Scrierea funcțiilor și a procedurilor stocate în SQL Server

Să exersăm!

Scrierea funcțiilor și a procedurilor stocate în SQL Server

Preparing Video For Download...