Presentation is loading. Please wait.

Presentation is loading. Please wait.

SELECT LA_code, sum(population)/sum(wardarea) AS density, (SELECT count(*) FROM wards_by_LA WLA2, violent_crime WHERE WLA2.ward_code =violent_crime.ward_code.

Similar presentations


Presentation on theme: "SELECT LA_code, sum(population)/sum(wardarea) AS density, (SELECT count(*) FROM wards_by_LA WLA2, violent_crime WHERE WLA2.ward_code =violent_crime.ward_code."— Presentation transcript:

1 SELECT LA_code, sum(population)/sum(wardarea) AS density, (SELECT count(*) FROM wards_by_LA WLA2, violent_crime WHERE WLA2.ward_code =violent_crime.ward_code AND WLA2.LA_code= WLA1.LA_code ) AS n_crimes FROM ward_profile, wards_by_LA WLA1 WHERE ward_profile.ward_code= WLA1.ward_code GROUP BY LA_code ORDER BY sum(population)/sum(wardarea) DESC; Correlated subqueries Tutorial 3, Q2 d) Produce a table showing the total population for each LA and the area it covers. Compute the population density. Order your output from highest to lowest density. e) Extend the previous query to include the total number of crimes in each LA. Part e) is not a simple extension of d). Bringing in violent_crime in a 3 way join will not work, and this will mean the information about population density and ward are is repeated multiple times. The answer is to use a subquery: Note the use of the subquery as an output field. This type of subquery is called a correlated subquery because it makes an external reference (WLA1) to the main query, and has to be re-evaluated for each iteration of the main query, i.e. each value of LA_code

2 SELECT ward_profile.ward_code, count(crime_id) AS n_crime FROM ward_profile LEFT OUTER JOIN violent_crime ON ward_profile.ward_code = violent_crime.ward_code GROUP BY ward_profile.ward_code ORDER BY count(crime_id) DESC SELECT * FROM violent_crime INNER JOIN ward_profile ON violent_crime.ward_code= ward_profile.ward_code All the joins we have been considering so far are called inner joins. SQL provides an alternative syntax: Inner joins only include combinations of rows when there is a match in both tables. This can be problematic – the above join, for instance, would not produce any rows for wards where no crimes were committed. Sometimes we may want to include all the rows from one table even if there is no matching row in the other table. This is called an outer join. For example: More SQL LEFT just means all rows from the first table are included (vice versa for RIGHT) FINALLY – note that once you have stored an Access query, you can query it just like a table, e.g. SELECT * from Query1. This allows very complex queries to be built up. You can think of the query as providing a particular VIEW of the database.

3 MORE ERM Relationships can be a) three way and b) recursive. SUPPLIER SUPPLY PART PROJECT EMPLOYEE SUPERVISOR b) Recursive relationship. Note that the double line means every employee has to have a supervisor. This is called TOTAL PARTICIPATION in the relationship. a) three way N 1 Chen is not the only notation

4 ERM – finale: Wozzie’s Gym USER BOOKS USER ID NAME CASUAL MEMBER FULL MEMBER d BANK DETAILS U U FACILITY Two implementation options: 1) Combine casual and full members in same table, with an extra attribute to indicate status. Alternatively, have a separate table for each and then use the UNION operator to combine them: SELECT user_id FROM full_member UNION SELECT user_id FROM casual_member This notation is used to indicate subclasses of an entity. Note that the subclasses “inherit” all the attributes of the “parent class”.

5 Business Intelligence (BI) A rational approach to continuous improvement based on: –Gathering/analyzing operational (transactional) data –Making decisions & taking actions based on that data –Measuring results according to predetermined metrics (Key performance indicators – KPIs) –Feeding lessons from one decision into the next The production of timely, accurate, high value and actionable information David Wastell

6 Data warehousing A Data Warehouse is a centrally managed and integrated database containing structured data from operational sources in an organization. Data extracts are validated, cleansed and transformed into a common, stable, relatable view of data. ETL = Extract Transform Load

7 Dashboards and KPIs Business dashboards provide a “control panel” for monitoring the vital functions of the business, supplying immediate information, indicating when and where performance is lagging…..

8 BI in Action: Crime Policy Police County Council Fire service Ambulance Service Probation Other ETL MADE Monthly Reports Data- mining (policy research)

9 0 100 200 300 400 500 600 02468 10121416182022 time of day serious violent crime Town 1 Town 2 Rural 1 Rural 2 Town 3 Data mining example: the aetiology of alcohol- related violence

10 Correlation = 0.67, very sig.

11 Evidence-based Policy: crime control Street drinking ban Ambulance Incidents (monthly) Target zone County demand Before ban9.1516.2 After ban8.3541.1 Change-9%+5% % total serious violent crime committed within target zone (reduced 14.8% to 12.3%) All effects stat. sig. BUT … any validity concerns, alternative explanations?? % of total in target zone 0 5 10 15 20 25 200204200205200206200207200208200209200210200211200212200301200302200303200304200305200306200307200308200309200310200311200312200401200402200403 Month Guess what…..

12 Crime data: relational view OLAP: Online analytical processing

13 Crime as a multi-dimensional “cube” Dimensions are typically hierarchies of categories

14 Slicing and dicing – and drilling down


Download ppt "SELECT LA_code, sum(population)/sum(wardarea) AS density, (SELECT count(*) FROM wards_by_LA WLA2, violent_crime WHERE WLA2.ward_code =violent_crime.ward_code."

Similar presentations


Ads by Google