ββββ±*.q:qβ±*.:qβ§*.qβ°*.:qβ§*.q:q*.qⱠβββ
Views
CrewRosterView
- Displays current ship assignments of personnel
- Joins
PERSONNEL,ASSIGNMENT, andSTARSHIP - Includes name, rank, position, home planet, starship name
- Filtering for active assignments was intended, but there wasnβt a
STATUScolumn, so it was left commented out (since I didnβt see a way to filter active assignments)
FleetStatusView
- Monitors ship deployment and crew levels
- Joins
STARSHIPandASSIGNMENT - Includes registry, class, mission, location, crew count, commission date, ship age
- Order by was skipped since SQL Server has limitations on ordering with views.
- You have to use either a TOP, OFFSET, or FOR XML clause
MissionHistoryView
- Tracks visits made by ships to planets
- Joins
VISIT,STARSHIP, andPLANET - Includes arrival/depart dates, duration (or βCurrently Dockedβ), and planet attributes
QualificationMatrixView
- Shows personnel qualifications and experience
- Joins
PERSONNELandQUAL - Calculates the years of experience based on the qualification
GRANTEDcolumn
Stored Procedures
TransferCrewMember
- Reassigns a crew member to a new ship and position
- Verifies existence β deletes old assignment β inserts a new one
- Handles errors with
TRY...CATCH,ROLLBACK, andTHROW
RecordPlanetaryVisit
- Records or updates planetary visits for ships
- Checks ship and planet exist as well as the arrival being before the departure
- Updates existing visits or inserts a new row if there isnβt a current visit
MissionResourceAllocation
- Calculates needed resources for missions
- Uses a temporary table
#ResourcePlanto hold projections - Inputs are derived from
STARSHIP,MISSION, andTYPE - Outputs energy, food, and medical supplies by crew size and priority
Functions
CalculateServiceDuration
- Returns time between first assignment and a reference date
- Returns a formatted string such as
'3 years, 4 months' - If
ASSIGNEDis null, it defaults to the current date.- Since it doesnβt allow for
GETDATE()as a default value, thereβs a simple check
- Since it doesnβt allow for
GetQualifiedPersonnel
- Lists personnel with a given skill and experience threshold
- Returns ID, name, rank, planet, years of experience, and current assignment (if any)
Design Decisions/Challenges
- I wasnβt able to use
ORDER BYin views since SQL Server requiresTOP,OFFSET, orXML FOR. - There was no
STATUScolumn inASSIGNMENT, so I wasnβt able to filter active assignments - Since I wasnβt able to use
GETDATE()as a default value, I had to use anIFcheck and redirect the logic slightly
Performance Considerations
- Indexed views werenβt used due to joins and aggregates, but they could be optimized if needed
MissionHistoryViewis lightweight;QualificationMatrixViewuses joins for efficiencyDELETE+INSERTinTransferCrewMemberavoids complex updating of data#ResourcePlanis temporary to minimize permanent clutter within the database
Examples
The example output isn't based on actual data within the database
CrewRosterView
SELECT * FROM CrewRosterView WHERE STARSHIP_NAME = 'USS Voyager';
-- ββββββββββββββββββ¬βββββββ¬βββββββββββββββββ¬ββββββββββββββ¬ββββββββββββββββ
-- β PERSONNEL_NAME β RANK β POSITION β HOME_PLANET β STARSHIP_NAME β
-- ββββββββββββββββββΌβββββββΌβββββββββββββββββΌββββββββββββββΌββββββββββββββββ€
-- β Tuvok β Ltnt β Security Chief β Vulcan β USS Voyager β
-- β Riker β LtCm β Chief Engineer β Kessik IV β USS Voyager β
-- ββββββββββββββββββ΄βββββββ΄βββββββββββββββββ΄ββββββββββββββ΄ββββββββββββββββCalculateServiceDuration
SELECT dbo.CalculateServiceDuration('P0001'); -- '5 years, 3 months'GetQualifiedPersonnel
SELECT * FROM dbo.GetQualifiedPersonnel('Astrophysics', 3);
-- βββββββββ¬βββββββββββββββ¬βββββββ¬βββββββββ¬ββββββββββββββββββββ¬ββββββββββββ¬βββββββββββββββββ¬βββββββββββββ
-- β ID β NAME β RANK β PLANET β YearsOfExperience β SHIP β POSITION β ASSIGNED β
-- βββββββββΌβββββββββββββββΌβββββββΌβββββββββΌββββββββββββββββββββΌββββββββββββΌβββββββββββββββββΌβββββββββββββ€
-- β P0001 β Spock β Cmdr β Vulcan β 5 β NCC-1701 β Science Officerβ 2370-01-01 β
-- β P0004 β Saavik β Ltnt β Romulusβ 4 β NCC-1702 β Astrophysicist β 2371-05-01 β
-- βββββββββ΄βββββββββββββββ΄βββββββ΄βββββββββ΄ββββββββββββββββββββ΄ββββββββββββ΄βββββββββββββββββ΄βββββββββββββTransferCrewMember
EXEC TransferCrewMember 'P0002', 'NCC-1701', 'Tactical Officer', '2373-07-15';MissionResourceAllocation
EXEC MissionResourceAllocation
-- ββββββββββββββ¬βββββββββββββββ¬ββββββββββββ¬ββββββββββββββββ¬βββββββββββ¬βββββββββββββββ¬βββββββββββββββββ¬βββββββββββ¬βββββββββββββββββ
-- β MISSION_ID β SHIP_REGISTRYβ CREW_SIZE β DURATION_DAYS β PRIORITY β ENERGY_UNITS β FOOD_SUPPLY_KG β MED_KITS β PRIORITY_LEVEL β
-- ββββββββββββββΌβββββββββββββββΌββββββββββββΌββββββββββββββββΌβββββββββββΌβββββββββββββββΌβββββββββββββββββΌβββββββββββΌβββββββββββββββββ€
-- β M001 β NCC-1701 β 150 β 90 β 9 β 13500 β 40500 β 30 β High β
-- β M002 β NX-74205 β 100 β 90 β 4 β 4000 β 27000 β 20 β Medium β
-- ββββββββββββββ΄βββββββββββββββ΄ββββββββββββ΄ββββββββββββββββ΄βββββββββββ΄βββββββββββββββ΄βββββββββββββββββ΄βββββββββββ΄βββββββββββββββββ
ββββ±*.q:qβ±*.:qβ§*.qβ°*.:qβ§*.q:q*.qⱠβββ