question

juz410 avatar image
juz410 asked

How do I combine the result below together with the result above with another column?

The ideal queryI want

SELECT Airline_ID,Airline_Name, (SELECT COUNT(Airline_ID) As Total_Number_of_Flight

FROM Flight

WHERE Airline_ID IN (SELECT Airline_ID From Airline)

GROUP BY Airline_ID) FROM Airline;
But it shows me that sub query cannot have more than 1 value


mssql
1623139999078.png (42.2 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered

Join the two tables and count the rows

select 
    Airline.Airline_ID, 
    Airline.Airline_Name,
    isnull(count(Flight.Airline_ID),0) As Total_Number_of_Flight
from Airline
left join Flight on Airline.Airline_ID= Flight.Airline_ID
group by  Airline.Airline_ID, Airline.Airline_Name
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

juz410 avatar image juz410 commented ·

Wow thanks lot it's work but how is that logic work? I'm so confused on "JOIN"

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.