Commit eddaa2d7 by michael lundquist

### Done with week 5 ch 11 hands on stuff.

parent bb923ace
 # problem 1 Show the average number of crimes reported by an officer ```SQL SELECT (SELECT COUNT(*) FROM CRIMES) / (SELECT COUNT(*) FROM OFFICERS) FROM DUAL; ``` ![ch11 pics](./PICS/PROBLEM1.PNG) # problem 2 Show the total number of crimes by status ```sql SELECT COUNT(*), STATUS FROM CRIMES GROUP BY STATUS; ``` ![ch11 pics](./PICS/PROBLEM2.PNG) ## problem 3 List the highest number of crimes committed by a person ```sql SELECT * FROM (SELECT COUNT(*), CRIMINAL_ID FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) GROUP BY CRIMINAL_ID ORDER BY COUNT(*) DESC) WHERE ROWNUM < 2; ``` ![ch11 pics](./PICS/PROBLEM3.PNG) OR IF YOU DON'T CARE ABOUT THE ID: ```sql SELECT MAX(COUNT(*)) FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) GROUP BY CRIMINAL_ID; ``` ![ch11 pics](./PICS/PROBLEM3B.PNG) ## Problem 4 Display the lowest fine amount assigned to a crime charge ```sql SELECT * FROM (SELECT CRIMINAL_ID, FINE_AMOUNT, FIRST, LAST, CHARGE_ID FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) JOIN CRIME_CHARGES USING (CRIME_ID) ORDER BY FINE_AMOUNT ASC) WHERE ROWNUM<2; ``` ![ch11 pics](./PICS/PROBLEM4.PNG) ```SQL SELECT MIN(FINE_AMOUNT) FROM CRIME_CHARGES; ``` ![ch11 pics](./PICS/PROBLEM4B.PNG) ## Problem 5 List criminals (ID and name) who have multiple sentances assigned ```SQL SELECT CRIMINAL_ID, COUNT(*) FROM CRIMINALS JOIN CRIMES USING (CRIMINAL_ID) GROUP BY CRIMINAL_ID HAVING COUNT(*) > 1; ``` ![ch11 pics](./PICS/PROBLEM5.PNG) ## Problem 6 List the total number of crime charges successfully defended (guilty status assigned) by precinct. Include only precincts with at least 7 guilty charges. ```SQL SELECT PRECINCT, COUNT(*) FROM OFFICERS JOIN CRIME_OFFICERS USING (OFFICER_ID) JOIN CRIMES USING (CRIME_ID) JOIN CRIME_CHARGES USING (CRIME_ID) WHERE CHARGE_STATUS = 'GL' GROUP BY PRECINCT HAVING COUNT(*) > 7; ``` ![ch11 pics](./PICS/PROBLEM6.PNG) ## problem 7 List the total amount of collections (fines and fees) and the total amount owed by crime classification ```SQL SELECT CLASSIFICATION, SUM(AMOUNT_PAID) AS COLLECTIONS, SUM(FINE_AMOUNT) - SUM(AMOUNT_PAID) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY CLASSIFICATION; ``` ![ch11 pics](./PICS/PROBLEM7.PNG) ## problem 8, Use single queries for 9-11 When completing items 8-10 from Chapter 11 Case Study use GROUPING SETS expression when needed. ### problem 9 List the total number of charges by crime classification and charge status. Include a grand total in the results. ```SQL SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS((CLASSIFICATION, CHARGE_STATUS), ()); ``` ![ch11 pics](./PICS/PROBLEM9.PNG) ### Problem 10 Perform the same task as problem 9, and add: #### problem 10 a a subtotal by each crime classification ```SQL SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS(classification,(CLASSIFICATION, CHARGE_STATUS), ()); ``` ![ch11 pics](./PICS/PROBLEM10a.PNG) #### problem 10 b a subtotal for each charge status. Provide two different queries to accomplish this task.: ```SQL SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS( charge_status, (CLASSIFICATION, CHARGE_STATUS), ()); ``` ![ch11 pics](./PICS/PROBLEM10b.PNG) ### Problem 11 Perform the same task as in problem 9, and add a subtotal by each crime classification. Provide two different queries to accomplish this task. #### problem 11 a ```SQL SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS(charge_status, classification,(CLASSIFICATION, CHARGE_STATUS), ()); ``` ![ch11 pics](./PICS/PROBLEM11a.PNG) #### problem 11b ```SQL SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS( charge_status, (CLASSIFICATION, CHARGE_STATUS), ()) union SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS(classification,(CLASSIFICATION, CHARGE_STATUS)); ``` ![ch11 pics](./PICS/PROBLEM11b.PNG) \ No newline at end of file
 /** problem 1 Show the average number of crimes reported by an officer*/ SELECT (SELECT COUNT(*) FROM CRIMES) / (SELECT COUNT(*) FROM OFFICERS) FROM DUAL; /** problem 2 Show the total number of crimes by status */ SELECT COUNT(*), STATUS FROM CRIMES GROUP BY STATUS; /** problem 3 List the highest number of crimes committed by a person*/ SELECT * FROM (SELECT COUNT(*), CRIMINAL_ID FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) GROUP BY CRIMINAL_ID ORDER BY COUNT(*) DESC) WHERE ROWNUM < 2; --OR IF YOU DON'T CARE ABOUT THE ID: SELECT MAX(COUNT(*)) FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) GROUP BY CRIMINAL_ID; /** Problem 4 Display the lowest fine amount assigned to a crime charge*/ SELECT CRIMINAL_ID, FINE_AMOUNT, FIRST, LAST FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) JOIN CRIME_CHARGES USING (CRIME_ID) ORDER BY FINE_AMOUNT; SELECT * FROM (SELECT CRIMINAL_ID, FINE_AMOUNT, FIRST, LAST FROM CRIMES JOIN CRIMINALS USING (CRIMINAL_ID) JOIN CRIME_CHARGES USING (CRIME_ID) ORDER BY FINE_AMOUNT ASC) WHERE ROWNUM<2; --OR SELECT MIN(FINE_AMOUNT) FROM CRIME_CHARGES; /** Problem 5 List criminals (ID and name) who have multiple sentances assigned */ SELECT CRIMINAL_ID, COUNT(*) FROM CRIMINALS JOIN CRIMES USING (CRIMINAL_ID) GROUP BY CRIMINAL_ID HAVING COUNT(*) > 1; /** Problem 6 List the total number of crime charges successfully defended (guilty status assigned) by precinct. Include only precincts with at least 7 guilty charges.*/ SELECT PRECINCT, COUNT(*) FROM OFFICERS JOIN CRIME_OFFICERS USING (OFFICER_ID) JOIN CRIMES USING (CRIME_ID) JOIN CRIME_CHARGES USING (CRIME_ID) WHERE CHARGE_STATUS = 'GL' GROUP BY PRECINCT HAVING COUNT(*) > 7; /** problem 7 List the total amount of collections (fines and fees) and the total amount owed by crime clasification*/ SELECT CLASSIFICATION, SUM(AMOUNT_PAID) AS COLLECTIONS, SUM(FINE_AMOUNT) - SUM(AMOUNT_PAID) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY CLASSIFICATION; --problem 8, Use single queries for 9-11 --When completing items 8-10 from Chapter 11 Case Study use GROUPING SETS expression when needed. /** problem 9 List the total number of charges by crime classification and charge status. Include a grand total in the results. */ SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS((CLASSIFICATION, CHARGE_STATUS), ()); /** Problem 10 Perform the same task as problem 9, and add: a) a subtotal by each crime classification b) a subtotal for each charge status. Provide two different queries to accomplish this task.*/ --a SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS(classification,(CLASSIFICATION, CHARGE_STATUS), ()); --b SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS( charge_status, (CLASSIFICATION, CHARGE_STATUS), ()); /** Problem 11 Perform the same task as in problem 9, and add a subtotal by each crime classification. Provide two different queries to accomplish this task.*/ --a SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS(charge_status, classification,(CLASSIFICATION, CHARGE_STATUS), ()); --b SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS( charge_status, (CLASSIFICATION, CHARGE_STATUS), ()) union SELECT CLASSIFICATION, CHARGE_STATUS, COUNT(*) FROM CRIMES JOIN CRIME_CHARGES USING (CRIME_ID) GROUP BY GROUPING SETS(classification,(CLASSIFICATION, CHARGE_STATUS)); \ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!