Chapter5 SQL
Chapter5 SQL
I N TE R M E D I AT E S Q L S E R V E R
Etibar Vazirov
CS instructor at UFAZ
Inspired by Ginger Grant’s slides
E xamining Totals with Counts
SELECT COUNT(*) FROM Incidents
+ +
|(No column name) |
+ +
|6452 |
+ +
+ +
|Countries |
+ +
|3 |
+ +
+ + +
|Countries | Cities |
+ + +
|3 | 3566 |
+ + +
+ + +
|TotalRowsbyCountry | Country |
+ + +
|5452 |us |
|750 |NULL |
|249 |ca |
|1 |gb |
+ + +
+ + +
|TotalRowsbyCountry | Country |
+ + +
|750 |NULL |
|249 |ca |
|1 |gb |
|5452 |us |
+ + +
+ + +
|TotalRowsbyCountry | Country |
+ + +
|5452 |us |
|1 |gb |
|249 |ca |
|750 |NULL |
+ + +
+ + +
|Country |TotalDuration |
+ + +
|us |17024946.750001565 |
|null |18859192.800000012 |
|ca |200975 |
|gb |120 |
+ + +
Etibar
Ginger Vazirov
Grant
CS instructor at UFAZ
Instructor
Inspired by Ginger Grant’s slides
DATEPART
DATEPART is used to determine what part of the date you want to calculate. Some of the
common abbreviations are:
DD for Da y
MM for Month
YY for Year
HH for Hour
+ + +
|Difference1 |Difference2 |
+ + +
|30 |-30 |
+ + +
Etibar
Ginger Vazirov
Grant
CS instructor at UFAZ
Instructor
Inspired by Ginger Grant’s slides
Rounding numbers in T-SQL
ROUND(number, length [,function])
+ + + +
|DurationSeconds |RoundToZero |RoundToOne |
+ + + +
|121.6480 |122.0000 |121.6000 |
|170.3976 |170.0000 |170.4000 |
|336.0652 |336.0000 |336.1000 |
...
+ + + +
+ + + +
|DurationSeconds |RoundToTen |RoundToHundred |
+ + + +
|121.6480 |120.0000 |100.0000 |
|170.3976 |170.0000 |200.0000 |
|336.0652 |340.0000 |300.0000 |
...
+ + + +
+ + + +
|Profit |RoundingtoWhole |Truncating |
+ + + +
|15.6100 |16.0000 |15.0000 |
|13.2444 |13.0000 |13.0000 |
|17.9260 |18.0000 |17.0000 |
...
+ + + +
Etibar
Ginger Vazirov
Grant
CS instructor at UFAZ
Instructor
Inspired by Ginger Grant’s slides
Absolute value
Use ABS() to return non-negative values
ABS(number)
+ + + +
|(No column name) |(No column name) |(No column name) |
+ + + +
|2.77 |3 |2 |
+ + + +
+ + +
|DurationSeconds |AbsSeconds |
+ + +
|-25.36 |25.36 |
|-258482.44 |258482.44 |
|45.66 |45.66 |
+ + +
+ + +
|Sqrt |Square |
+ +
|3 |81 |
+ + +
Optionally, you can set the base, which if not set is 2.718281828
LOG(number [,Base])
+ + +
|DurationSeconds |LogSeconds |
+ + +
|37800 |4.577491799837225 |
|5 |0.6989700043360187 |
|20 |1.301029995663981 |
...
+ + +