Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. 2 child tables: Driver & Commuter
2. Parent table: User
3. My code and result and expected output(in link):

SQL
SELECT  Booking_ID
       ,Book_Time
       ,a.Name as Driver
       ,b.Name as Commuter
       ,Distance as Estimated_Travel_Distance
       ,Price_Min
       ,Price_Max
       ,book.Distance_Travelled as Actual_Distance
       ,(Distance_Travelled *Basic_Fare) as Actual_Fare
FROM    Booking_t book, user_t a, user_t b, locations_info_t loc, cartype_t ct
WHERE   book.Commuter = b.Phone_Num
    AND book.Driver = a.Phone_Num
    AND book.Distance_Travelled = loc.Distance
    AND book.Car_Type = ct.Car_Type_Name
    AND extract(month from Book_time) = 2 
    AND extract(year from Book_time) = 2014
    AND extract(day from Book_time) < 16 
GROUP BY Booking_ID;

http://i.stack.imgur.com/RC4ll.jpg

4. Driver & commuter have Phone_Num that is foreign key & referenced to user table Phone_Num
5. My code results show the commuter names in both driver & commuter columns but actual output should display drivers names and commuters names in driver and commuter column respectively.
6. When i add "book.Driver = u.Phone_Num" in the where clause to my code mentioned above, the output shows no data.

7. Table structure of User,Driver,Commuter & booking (Ignore the other tables in link)
http://i.stack.imgur.com/f6vRe.jpg

What I have tried:

I have already tried to add "book.Driver = u.Phone_Num" in the where clause that is in my code which is mentioned above, but the output still shows no data instead.
Posted
Updated 23-Feb-16 21:51pm
v2
Comments
Jörgen Andersson 24-Feb-16 2:47am    
Just look at the first row of your query, you're using the same field for both the Driver and Commuter.
Member 12345181 24-Feb-16 3:09am    
Hi, thanks i have made some changes with my code, Driver and commuters names are shown in each column respectively but the code is not showing records that is null, which is the driver's column containing null values.

my code:
select Booking_ID, Book_Time, a.Name as Driver, b.Name as Commuter, Distance as Estimated_Travel_Distance,Price_Min, Price_Max, book.Distance_Travelled as Actual_Distance, (Distance_Travelled *Basic_Fare) as Actual_Fare
from Booking_t book, user_t a, user_t b, locations_info_t loc, cartype_t ct
where book.Commuter = b.Phone_Num and book.Driver = a.Phone_Num and book.Distance_Travelled = loc.Distance and book.Car_Type = ct.Car_Type_Name and extract(month from Book_time) = 2
and extract(year from Book_time) = 2014 and extract(day from Book_time) < 16
group by Booking_ID;

1 solution

Then you need to use an outer join
And to make it more readable you should avoid implicit joins, spell them out properly using ANSI joins.

Try this:
SQL
SELECT  Booking_ID
       ,Book_Time
       ,a.Name as Driver
       ,b.Name as Commuter
       ,Distance as Estimated_Travel_Distance
       ,Price_Min
       ,Price_Max
       ,book.Distance_Travelled as Actual_Distance
       ,(Distance_Travelled *Basic_Fare) as Actual_Fare
FROM    Booking_t book
JOIN    locations_info_t loc
    ON  book.Distance_Travelled = loc.Distance
JOIN    cartype_t ct
    ON  book.Car_Type = ct.Car_Type_Name
JOIN    user_t b
    ON  book.Commuter = b.Phone_Num
left OUTER JOIN user_t a
    ON  book.Driver = a.Phone_Num
WHERE   extract(month from Book_time) = 2 
    AND extract(year from Book_time) = 2014
    AND extract(day from Book_time) < 16;


By the way, you need to use proper keys. If a driver changes his phone number his records in the booking table will have disappeared
 
Share this answer
 
v4
Comments
Member 12345181 24-Feb-16 3:59am    
Hi, let's assume that in my project the driver will not change his phone number.
Your code's output is still the same as mine, it does not display the null records.
Jörgen Andersson 24-Feb-16 4:33am    
Remove the group by
Member 12345181 24-Feb-16 4:55am    
The output is still the same, its not showing the booking records that have null value for Driver
Jörgen Andersson 24-Feb-16 5:33am    
Can you create an example fiddle with some of your data here: http://www.sqlfiddle.com/
and post the link to the fiddle?
Member 12345181 24-Feb-16 11:22am    
Hi, the group by needs to be there if not there will be duplication of booking ids and after making some changes and following what you mention on outer join to get the null records, i had gotten the correct results! Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900