sql server - Combine two subqueries to main query in SQL -
i new sql , need assistance combine (join) 2 subqueries main query in sql. here have. *each query works independent of another. end result would retrieve # of accommodations each resort, retrieve lowest cost of accommodations each resort, , join results list of resort types , resorts.
db schema
table 1 - resort - resort_id (pk) resort_type_id (fk) name table 2 - resort_type - resort_type_id (pk) resort_type table 3 - accommodations - accommodations_id (pk) resort_id (fk) description cost_per_night
query
select resort.name, resort_type, acc.accommodations, low.min_price (select resort.name resort_name, resort_type.resort_type resort inner join resort_type on resort.resort_type_id = resort_type.resort_type_id (select resort_id, count(resort_id) accommodations accommodations group resort_id) acc (select resort_id, min(cost_per_night) min_price accommodations group resort_id) low
any guidance appreciated. having difficult time visualizing how should come together.
the query below lists each resort , type along number of accommodations , lowest cost per night.
select r.name, t.resort_type type, count(a.accommodations_id) accommodations, min(cost_per_night) lowestcost resort r inner join resort_type t on t.resort_type_id = r.resort_type_id left join accommodations on a.resort_id = r.resort_id group r.name, t.resort_type
example: http://sqlfiddle.com/#!9/fc089/6
Comments
Post a Comment