Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Data Analysis

SQL - Extract month and date from DATETIME

I have three tables in my SQL code:

CREATE TABLE cities (id INTEGER PRIMARY KEY, name TEXT, timezone TEXT); INSERT INTO cities VALUES (1, "san_francisco", "America/Los_Angeles"); INSERT INTO cities VALUES (5, "new_york", "America/New_York");

CREATE TABLE trips (request_at DATETIME, driver_id INTEGER, city_id INTEGER, fare REAL, status TEXT, request_vehicle_view_id INTEGER); INSERT INTO trips VALUES("2011-04-05 18:04:36", 8134971, 5, 10.31, "completed", 8); INSERT INTO trips VALUES("2011-03-05 18:04:36", 8134971, 5, 10.31, "completed", 8); INSERT INTO trips VALUES("2011-03-06 18:04:36", 8134971, 5, 10.31, "completed", 8); INSERT INTO trips VALUES("2011-03-25 18:04:36", 8134971, 5, 10.31, "completed", 8); INSERT INTO trips VALUES("2015-01-13 10:45:06", 3425215, 1, 13.37, "canceled", 2);

CREATE TABLE vehicle_views (id INTEGER PRIMARY KEY, city_id INTEGER,name TEXT); INSERT INTO vehicle_views VALUES(2, 1, "uberX"); INSERT INTO vehicle_views VALUES(3, 1, "uberBLACK"); INSERT INTO vehicle_views VALUES(8, 5, "uberX");

I need to calculate "For each trips.driver_id, report the number of trips completed by week for the weeks of March, 2014."

1 Answer

Steven Parker
Steven Parker
230,995 Points

You didn't mention what kind of database, or link to a course page, but perhaps the most generic way would be to use STRFTIME to get the month and day of the trip, and arrange counts by week, then GROUP BY the driver_id. You might have other options depending on what database is involved.

It doesn't look like you'd need any table other than "trips" for this query.