sql - Interesting Query in MySQL with conditions -


here question:

management wants know specific aircraft have been assigned same hangar lot (each aircraft assigned lot year , renewed if requested. lot may or may not same lot upon renewal; pk lots table registration number , date assigned. list aircraft registration number, hangar location, hangar capacity. each aircraft should appear once in report.

here lots table:

lot_nbr, hangar_nbr, reg_nbr, date_assigned

'7706', '1', '505', '2011-07-17'
'7707', '1', '505', '2012-07-18'
'7708', '1', '505', '2013-12-22'
'8801', '2', '101', '2011-01-22'
'8802', '2', '101', '2012-01-22'
'8803', '2', '303', '2011-01-10'
'8803', '2', '202', '2011-04-12'
'8803', '2', '202', '2012-04-12'
'9902', '3', '303', '2012-01-10'
'9904', '3', '404', '2011-01-10'
'9905', '3', '404', '2012-01-10'

here hangar table:

hangar_nbr, location, capacity, manager_nbr

'1', 'south rw', '24', '902'
'2', 'north rw', '12', '902'
'3', 'west rw', '25', '902'

here aircraft table: (might needed)

reg_nbr, plane_type, manufacturer, resident, date_service
'101', 'se', 'prop', 'lear', '1', '1998-12-01'
'202', 'de prop', 'cessna', '1','1999-11-22'
'303', 'se jet', 'bombardier', '1', '2000-07-06'
'404','me jet', 'lear', '1', '2001-02-24'
'505', 'se prop', 'cessna', '0','2003-12-01'
'606', 'de prop', 'bombardier', '1', '2004-11-22'
'707','se jet', 'lear', '1', '2005-07-06'
'808', 'me jet', 'cessna', '0','2005-02-24'
'909', 'se prop', 'bombardier', '1', '2002-02-12'

the query want (exactly):

reg_nbr, location, capacity
'202', 'north rw', '12'

here tried:

select lots.reg_nbr, location, capacity  hangar, lots  lots.hangar_nbr = hangar.hangar_nbr , reg_nbr='202' group lots.reg_nbr     

my problem:

as shown above wanted show aircraft reg_nbr '202' in result because aircraft has been allocated same lot_nbr every time unfortunately way did i.e writing reg_nbr ='202' in clause not way query should done. want show how results real , legal query. lot effort.

select reg_nbr lots group reg_nbr having count(distinct lot_nbr) = 1 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -