there are two tables, region tables and accident tables. I wrote a query to join the two tables based off of state and s

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

there are two tables, region tables and accident tables. I wrote a query to join the two tables based off of state and s

Post by answerhappygod »

there are two tables, region tables and accident tables. I wrotea query to join the two tables based off of state and stateidcolumns
below are the two tables:
There Are Two Tables Region Tables And Accident Tables I Wrote A Query To Join The Two Tables Based Off Of State And S 1
There Are Two Tables Region Tables And Accident Tables I Wrote A Query To Join The Two Tables Based Off Of State And S 1 (48.52 KiB) Viewed 20 times
this is the query I wrote to combine the tables
Select region, rank() over(partition by region order bysum(number_of_accidents) as rank, region_tbl.state,sum(number_of_accident)
From region_tbl
Inner join car_accident_tbl
On region_tbl.state_id =car_accident_tbl.state_id
And region_tbl.state =car_accident_tbl.state
Where state != ‘Alaska’
Group by region, region_tbl.state
this query essentially list all the regions and ranksthem from highest state to least per region. Work off of thisquery!!!
for this questions the goal is
In sql, using rank or partition, write a query thats aggregatesthe top three states that had the most amount of accident per eachregion, after the top three add the rest of the states togetherwhere there were accidents and label as other. so for example forthe west region groupings, add all the entries for each state andenter the top 3 states, after that add all the other states entriestogether and label as other. Remember the goal is to know which top3 states had the most accidents, and besides the top 3 how manyaccidents were there for all states combined(other) per region. Soeach region(west,north,east,south) should have top 3 states andother associated with it
this is what the output(final result) of the query shouldbe:
There Are Two Tables Region Tables And Accident Tables I Wrote A Query To Join The Two Tables Based Off Of State And S 2
There Are Two Tables Region Tables And Accident Tables I Wrote A Query To Join The Two Tables Based Off Of State And S 2 (50.04 KiB) Viewed 20 times
State_ID 1c 2t 3g 4a 3g 6n 1c 8n 8n 90 REGION table State CA TX GA AZ GA NY CA NM NM Alaska Region WEST SOUTH EAST WEST EAST NORTH WEST WEST WEST North State_ID 1c 2t 3g 4a 3g 6n 1c 8n 8n 90 Car accident table State CA TX GA AZ GA NY CA NM NM Alaska Number_of_accidents 10 5 20 5 30 80 10 5 10 20
Region WEST WEST WEST Other SOUTH SOUTH SOUTH Other EAST EAST EAST Other NORTH NORTH NORTH Other Rank 1 UT 2 NW 3 AZ State 'other_rank' other 1 TX 2 CA 3 NJ 'other_rank' other 1 HW 2 GA 3 TN 'other_rank' 'other_rank' other 1 ta 2 tv 3 cd other Sum_amount_accidents 40 30 25 55 56 50 35 80 30 20 10 50 10 5 1 50
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply