forked from musa-5090-spring-2024/assignment01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query07.sql
33 lines (25 loc) · 923 Bytes
/
query07.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
How many trips started on one day and ended on a different day?
Your result should have one column named trip_year, one column named
trip_quarter, and one column named num_trips.
*/
-- Enter your SQL query here
SELECT
EXTRACT(YEAR FROM start_time) AS trip_year,
EXTRACT(QUARTER FROM start_time) AS trip_quarter,
COUNT(*) AS num_trips
FROM
(SELECT * FROM indego.trips_2021_q3
UNION ALL
SELECT * FROM indego.trips_2022_q3)
AS combined_trips
WHERE
CAST(start_time AS DATE) != CAST(end_time AS DATE)
GROUP BY
trip_year, trip_quarter;
/*
Hint 1: when you cast a TIMESTAMP to a DATE the time component of the value is simply stripped off
Hint 2: Years, quarters, and other parts of DATEs or TIMESTAMPs can be retrieved from a TIMESTAMP using the
[EXTRACT](https://www.postgresql.org/docs/12/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT)
function.
*/