Commit eddaa2d7 authored by michael lundquist's avatar michael lundquist
Browse files

Done with week 5 ch 11 hands on stuff.

parent bb923ace
CREATE TABLE aliases
(alias_id NUMBER(6),
criminal_id NUMBER(6),
alias VARCHAR2(10));
CREATE TABLE criminals
(criminal_id NUMBER(6),
last VARCHAR2(15),
first VARCHAR2(10),
street VARCHAR2(30),
city VARCHAR2(20),
state CHAR(2),
zip CHAR(5),
phone CHAR(10),
v_status CHAR(1) DEFAULT 'N',
p_status CHAR(1) DEFAULT 'N' );
CREATE TABLE crimes
(crime_id NUMBER(9),
criminal_id NUMBER(6),
classification CHAR(1),
date_charged DATE,
status CHAR(2),
hearing_date DATE,
appeal_cut_date DATE);
CREATE TABLE sentences
(sentence_id NUMBER(6),
criminal_id NUMBER(6),
type CHAR(1),
prob_id NUMBER(5),
start_date DATE,
end_date DATE,
violations NUMBER(3));
CREATE TABLE prob_officers
(prob_id NUMBER(5),
last VARCHAR2(15),
first VARCHAR2(10),
street VARCHAR2(30),
city VARCHAR2(20),
state CHAR(2),
zip CHAR(5),
phone CHAR(10),
email VARCHAR2(30),
status CHAR(1) DEFAULT 'A' );
CREATE TABLE crime_charges
(charge_id NUMBER(10),
crime_id NUMBER(9),
crime_code NUMBER(3),
charge_status CHAR(2),
fine_amount NUMBER(7,2),
court_fee NUMBER(7,2),
amount_paid NUMBER(7,2),
pay_due_date DATE);
CREATE TABLE crime_officers
(crime_id NUMBER(9),
officer_id NUMBER(8));
CREATE TABLE officers
(officer_id NUMBER(8),
last VARCHAR2(15),
first VARCHAR2(10),
precinct CHAR(4),
badge VARCHAR2(14),
phone CHAR(10),
status CHAR(1) DEFAULT 'A' );
CREATE TABLE appeals
(appeal_id NUMBER(5),
crime_id NUMBER(9),
filing_date DATE,
hearing_date DATE,
status CHAR(1) DEFAULT 'P' );
CREATE TABLE crime_codes
(crime_code NUMBER(3),
code_description VARCHAR2(30));
ALTER TABLE crimes
MODIFY (classification DEFAULT 'U');
ALTER TABLE crimes
ADD (date_recorded DATE DEFAULT SYSDATE);
ALTER TABLE prob_officers
ADD (pager# CHAR(10));
ALTER TABLE aliases
MODIFY (alias VARCHAR2(20));
DROP TABLE appeals;
DROP TABLE crime_officers;
DROP TABLE crime_charges;
ALTER TABLE criminals
ADD CONSTRAINT criminals_id_pk PRIMARY KEY (criminal_id);
ALTER TABLE criminals
ADD CONSTRAINT criminals_vstatus_ck CHECK (v_status IN('Y','N'));
ALTER TABLE criminals
ADD CONSTRAINT criminals_pstatus_ck CHECK (p_status IN('Y','N'));
ALTER TABLE aliases
ADD CONSTRAINT aliases_id_pk PRIMARY KEY (alias_id);
ALTER TABLE aliases
ADD CONSTRAINT appeals_criminalid_fk FOREIGN KEY (criminal_id)
REFERENCES criminals(criminal_id);
ALTER TABLE aliases
MODIFY (criminal_id NOT NULL);
ALTER TABLE crimes
ADD CONSTRAINT crimes_id_pk PRIMARY KEY (crime_id);
ALTER TABLE crimes
ADD CONSTRAINT crimes_class_ck CHECK (classification IN('F','M','O','U'));
ALTER TABLE crimes
ADD CONSTRAINT crimes_status_ck CHECK (status IN('CL','CA','IA'));
ALTER TABLE crimes
ADD CONSTRAINT crimes_criminalid_fk FOREIGN KEY (criminal_id)
REFERENCES criminals(criminal_id);
ALTER TABLE crimes
MODIFY (criminal_id NOT NULL);
ALTER TABLE prob_officers
ADD CONSTRAINT probofficers_id_pk PRIMARY KEY (prob_id);
ALTER TABLE prob_officers
ADD CONSTRAINT probofficers_status_ck CHECK (status IN('A','I'));
ALTER TABLE sentences
ADD CONSTRAINT sentences_id_pk PRIMARY KEY (sentence_id);
ALTER TABLE sentences
ADD CONSTRAINT sentences_criminalid_fk FOREIGN KEY (criminal_id)
REFERENCES criminals(criminal_id);
ALTER TABLE sentences
MODIFY (criminal_id NOT NULL);
ALTER TABLE sentences
ADD CONSTRAINT sentences_probid_fk FOREIGN KEY (prob_id)
REFERENCES prob_officers(prob_id);
ALTER TABLE sentences
ADD CONSTRAINT sentences_type_ck CHECK (type IN('J','H','P'));
ALTER TABLE officers
ADD CONSTRAINT officers_id_pk PRIMARY KEY (officer_id);
ALTER TABLE officers
ADD CONSTRAINT officers_status_ck CHECK (status IN('A','I'));
ALTER TABLE crime_codes
ADD CONSTRAINT crimecodes_code_pk PRIMARY KEY (crime_code);
CREATE TABLE appeals
(appeal_id NUMBER(5),
crime_id NUMBER(9) NOT NULL,
filing_date DATE,
hearing_date DATE,
status CHAR(1) DEFAULT 'P',
CONSTRAINT appeals_id_pk PRIMARY KEY (appeal_id),
CONSTRAINT appeals_crimeid_fk FOREIGN KEY (crime_id)
REFERENCES crimes(crime_id),
CONSTRAINT appeals_status_ck CHECK (status IN('P','A','D')) );
CREATE TABLE crime_officers
(crime_id NUMBER(9),
officer_id NUMBER(8),
CONSTRAINT crimeofficers_cid_oid_pk PRIMARY KEY (crime_id,officer_id),
CONSTRAINT crimeofficers_crimeid_fk FOREIGN KEY (crime_id)
REFERENCES crimes(crime_id),
CONSTRAINT crimeofficers_officerid_fk FOREIGN KEY (officer_id)
REFERENCES officers(officer_id) );
CREATE TABLE crime_charges
(charge_id NUMBER(10),
crime_id NUMBER(9) NOT NULL,
crime_code NUMBER(3) NOT NULL,
charge_status CHAR(2),
fine_amount NUMBER(7,2),
court_fee NUMBER(7,2),
amount_paid NUMBER(7,2),
pay_due_date DATE,
CONSTRAINT crimecharges_id_pk PRIMARY KEY (charge_id),
CONSTRAINT crimecharges_crimeid_fk FOREIGN KEY (crime_id)
REFERENCES crimes(crime_id),
CONSTRAINT crimecharges_code_fk FOREIGN KEY (crime_code)
REFERENCES crime_codes(crime_code),
CONSTRAINT crimecharges_status_ck CHECK (charge_status IN('PD','GL','NG')) );
DROP TABLE criminals CASCADE CONSTRAINTS;
DROP TABLE crimes CASCADE CONSTRAINTS;
CREATE TABLE criminals
(criminal_id NUMBER(6),
last VARCHAR2(15),
first VARCHAR2(10),
street VARCHAR2(30),
city VARCHAR2(20),
state CHAR(2),
zip CHAR(5),
phone CHAR(10),
v_status CHAR(1) DEFAULT 'N',
p_status CHAR(1) DEFAULT 'N' );
CREATE TABLE crimes
(crime_id NUMBER(9),
criminal_id NUMBER(6),
classification CHAR(1),
date_charged DATE,
status CHAR(2),
hearing_date DATE,
appeal_cut_date DATE);
ALTER TABLE crimes
MODIFY (classification DEFAULT 'U');
ALTER TABLE crimes
ADD (date_recorded DATE DEFAULT SYSDATE);
ALTER TABLE criminals
ADD CONSTRAINT criminals_id_pk PRIMARY KEY (criminal_id);
ALTER TABLE criminals
ADD CONSTRAINT criminals_vstatus_ck CHECK (v_status IN('Y','N'));
ALTER TABLE criminals
ADD CONSTRAINT criminals_pstatus_ck CHECK (p_status IN('Y','N'));
ALTER TABLE crimes
ADD CONSTRAINT crimes_id_pk PRIMARY KEY (crime_id);
ALTER TABLE crimes
ADD CONSTRAINT crimes_class_ck CHECK (classification IN('F','M','O','U'));
ALTER TABLE crimes
ADD CONSTRAINT crimes_status_ck CHECK (status IN('CL','CA','IA'));
ALTER TABLE crimes
ADD CONSTRAINT crimes_criminalid_fk FOREIGN KEY (criminal_id)
REFERENCES criminals(criminal_id);
ALTER TABLE crimes
MODIFY (criminal_id NOT NULL);
This diff is collapsed.
drop table aliases cascade constraints;
drop table appeals cascade constraints;
drop table crime_officers cascade constraints;
drop table officers cascade constraints;
drop table crime_charges cascade constraints;
drop table crimes cascade constraints;
drop table sentences cascade constraints;
drop table criminals cascade constraints;
drop table prob_officers cascade constraints;
drop table crime_codes cascade constraints;
\ No newline at end of file
# problem 1
List the following information for all crimes that have a period greater than 14 days between the date charged and the hearing date: crime ID, classification, date charged, hearing date, and number of days between the date charged and the hearing date
```SQL
SELECT CRIME_ID, CLASSIFICATION, DATE_CHARGED, HEARING_DATE, TRUNC(HEARING_DATE - DATE_CHARGED,0) AS DAYS_BETWEEN
FROM CRIMES WHERE HEARING_DATE - DATE_CHARGED > 14;
```
![PROBLEM1](./PICS/PROBLEM1.PNG)
## problem 2
Produce a list showing each active police officer and his or her community assignment, indicated by the second letter of the precinct code. Display the community description listed in the following chart, based on the second letter of the precinct code.
```SQL
SELECT FIRST, LAST, OFFICER_ID, PRECINCT, SUBSTR(PRECINCT, 2, 1) PRE, DECODE(SUBSTR(PRECINCT, 2, 1), 'A', 'SHADY GROVE', 'B', 'CENTER CITY', 'C', 'BAY LANDING')
FROM OFFICERS;
```
![PROBLEM2](./PICS/problem2.PNG)
## problem 3
Produce a list of sentencing information to include criminal ID, name (displayed in all uppercase letters), sentence ID, sentence start date, and length in months of the sentence. The number of months should be shown as a whole number. The start date should be displayed in the format "December 17, 2009"
```SQL
SELECT CRIMINAL_ID, UPPER(FIRST ||' '||LAST) AS NAME, SENTENCE_ID, TO_CHAR(START_DATE, 'MONTH DD, YYYY') AS SENTANCE_START, TRUNC(MONTHS_BETWEEN(END_DATE, START_DATE),0) AS MONTHS
FROM CRIMINALS JOIN SENTENCES USING (CRIMINAL_ID);
```
![PROBLEM3](./PICS/PROBLEM3.PNG)
## Problem 4
A list of all amounts owed is needed. Create a list showing each criminal name, charge ID, total amount owed (fine amount plus court fee), amount paid, amount owed, and payment due date. If nothing has been paid to date, the amount paid is NULL. Include only criminals who owe some amount of money. Display the dollar amount with a sign and two decimals.*/
```SQL
SELECT UPPER(FIRST ||' '||LAST) AS NAME, CHARGE_ID, TO_CHAR(NVL(FINE_AMOUNT,0) + NVL(COURT_FEE,0), '$999.99') AS TOTAL_OWED, TO_CHAR(NVL(AMOUNT_PAID,0), '$999.99') AS AMOUNT_PAID, TO_CHAR((NVL(FINE_AMOUNT,0) + NVL(COURT_FEE,0)) - NVL(AMOUNT_PAID,0), '$999.99') AS BALANCE
FROM CRIMINALS JOIN CRIMES USING (CRIMINAL_ID) JOIN CRIME_CHARGES USING (CRIME_ID)
WHERE (NVL(FINE_AMOUNT,0) + NVL(COURT_FEE,0)) - NVL(AMOUNT_PAID,0) > 0;
```
![PROBLEM3](./PICS/PROBLEM4.PNG)
## Problem 5
Display the criminal name an probation start date for all criminals who have a probation period greater than 2 months. Also, display the date that's two months from the beginning of the probation period, which will serve as a review date.*/
```SQL
SELECT UPPER(FIRST ||' '||LAST) AS NAME, START_DATE, ADD_MONTHS(START_DATE, 2) AS REVIEW_DATE
FROM CRIMINALS JOIN SENTENCES USING (CRIMINAL_ID)
WHERE MONTHS_BETWEEN(END_DATE, START_DATE) >= 2 AND TYPE = 'P';
```
![PROBLEM 5](./PICS/PROBLEM5.PNG)
\ No newline at end of file
-- THIS WEEK WE DO CASE STUDY
/*
problem 1
List the following information for all crimes that have a period greater than 14 days between the date charged and the hearing date: crime ID, classification, date charged, hearing date, and number of days between the date charged and the hearing date
*/
SELECT CRIME_ID, CLASSIFICATION, DATE_CHARGED, HEARING_DATE, TRUNC(HEARING_DATE - DATE_CHARGED,0) AS DAYS_BETWEEN FROM CRIMES WHERE HEARING_DATE - DATE_CHARGED > 14;
/**
problem 2
Produce a list showing each active police officer and his or her community assignment, indicated by the second letter of the precinct code. Display the community description listed in the following chart, based on the second letter of the precinct code.
*/
SELECT FIRST, LAST, OFFICER_ID, PRECINCT, SUBSTR(PRECINCT, 2, 1) PRE, DECODE(SUBSTR(PRECINCT, 2, 1), 'A', 'SHADY GROVE', 'B', 'CENTER CITY', 'C', 'BAY LANDING') FROM OFFICERS;
/**
problem 3
Produce a list of sentencing information to include criminal ID, name (displayed in all uppercase letters), sentence ID, sentence start date, and length in months of the sentence. The number of months should be shown as a whole number. The start date should be displayed in the format "December 17, 2009"
*/
SELECT CRIMINAL_ID, UPPER(FIRST ||' '||LAST) AS NAME, SENTENCE_ID, TO_CHAR(START_DATE, 'MONTH DD, YYYY') AS SENTANCE_START, TRUNC(MONTHS_BETWEEN(END_DATE, START_DATE),0) AS MONTHS FROM CRIMINALS JOIN SENTENCES USING (CRIMINAL_ID);
/**
Problem 4
A list of all amounts owed is needed. Create a list showing each criminal name, charge ID, total amount owed (fine amount plus court fee), amount paid, amount owed, and payment due date. If nothing has been paid to date, the amount paid is NULL. Include only criminals who owe some amount of money. Display the dollar amount with a sign and two decimals.*/
SELECT UPPER(FIRST ||' '||LAST) AS NAME, CHARGE_ID, TO_CHAR(NVL(FINE_AMOUNT,0) + NVL(COURT_FEE,0), '$999.99') AS TOTAL_OWED, TO_CHAR(NVL(AMOUNT_PAID,0), '$999.99') AS AMOUNT_PAID, TO_CHAR((NVL(FINE_AMOUNT,0) + NVL(COURT_FEE,0)) - NVL(AMOUNT_PAID,0), '$999.99') AS BALANCE FROM CRIMINALS JOIN CRIMES USING (CRIMINAL_ID) JOIN CRIME_CHARGES USING (CRIME_ID) WHERE (NVL(FINE_AMOUNT,0) + NVL(COURT_FEE,0)) - NVL(AMOUNT_PAID,0) > 0;
/**
Problem 5
Display the criminal name an probation start date for all criminals who have a probation period greater than 2 months. Also, display the date that's two months from the beginning of the probation period, which will serve as a review date.*/
SELECT UPPER(FIRST ||' '||LAST) AS NAME, START_DATE, ADD_MONTHS(START_DATE, 2) AS REVIEW_DATE FROM CRIMINALS JOIN SENTENCES USING (CRIMINAL_ID) WHERE MONTHS_BETWEEN(END_DATE, START_DATE) >= 2 AND TYPE = 'P';
/**
DON'T DO PROBLEM 6
*/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment