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

Databases

Stanislava Stoeva
Stanislava Stoeva
131 Points

Could somebody help me with some SQL query, please?

I have created a database in SQL server using Query. I have done my tables as well as I provide some data in them. Anyway I have to do some query now but I have issues with those. What am I missing?

Query: Display all the customers who have booked today that have not yet received the service and their type of services as well as their assigned drivers. "SELECT count (*) as number_of_bookings FROM dbo.Customer on CustomerID = JourneyID * jService_type WHERE JourneyID.DriverID = Journey.jService_type GROUP BY Journey; "

2 Answers

Steven Parker
Steven Parker
229,921 Points

It sounds like you need to return the records themselves.

Based on those instructions, it sounds like what is expected is the actual customer records rather than the aggregate count. It also looks like you're attempting to join tables but the syntax is not correct.

So a generic example using assumed table and column names might be:

SELECT * FROM Customer c
JOIN Service s on c.ID = s.Customer_ID
WHERE s.Booked_On = date("now") AND s.Completed IS NULL

To facilitate a more complete and accurate analysis, provide a link to the course page you are working with.

Steven Parker
Steven Parker
229,921 Points

So it sounds like you need two joins:

SELECT * FROM tblCustomers c
JOIN tblJourney j on c.ID = j.Customer_ID
JOIN tblDrivers d on d.ID = j.Driver_ID
WHERE j.Booked_On = date("now") AND j.Completed IS NULL;

I can't be more specific without seeing your complete DDL (table definitions). But hopefully this is enough to get you where you can complete it yourself.

Stanislava Stoeva
Stanislava Stoeva
131 Points

This query is from some of my exercises that I am working so i don't have a link. I can give you my scenario: the database is for a car company that provide different services (pickup, drop-off, minicab, van, vip transfer), which i am identifying in the database as Service_type. I got tblCustomers , tblDrivers, tblJourney and tblPriceList. So each customer when is booking services have to choose Driver and Service_type. The drivers for each journey is different so, I suppose that I have to inccorpoarate the driver (DriverID) in my query.