Chapter 11 stats code and data
This commit is contained in:
parent
3ca79909ce
commit
71bedcdf49
@ -19,9 +19,8 @@ CREATE TABLE acs_2014_2018_stats (
|
|||||||
);
|
);
|
||||||
|
|
||||||
COPY acs_2014_2018_stats
|
COPY acs_2014_2018_stats
|
||||||
FROM '/Users/adebarros/Dropbox/DataMonky/Book-Writing/PracticalSQL_2e/Code-Repo/Chapter_11/acs_2014_2018_stats.csv'
|
FROM 'C:\YourDirectory\acs_2014_2018_stats.csv'
|
||||||
--FROM 'C:\YourDirectory\acs_2014_2018_stats.csv'
|
WITH (FORMAT CSV, HEADER);
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
|
||||||
|
|
||||||
SELECT * FROM acs_2014_2018_stats;
|
SELECT * FROM acs_2014_2018_stats;
|
||||||
|
|
||||||
@ -64,8 +63,8 @@ SELECT round(
|
|||||||
) AS r_squared
|
) AS r_squared
|
||||||
FROM acs_2014_2018_stats;
|
FROM acs_2014_2018_stats;
|
||||||
|
|
||||||
-- Bonus: Additional stats functions.
|
-- Bonus: Additional stats functions
|
||||||
-- Variance
|
-- Variance of the entire population
|
||||||
SELECT var_pop(median_hh_income)
|
SELECT var_pop(median_hh_income)
|
||||||
FROM acs_2014_2018_stats;
|
FROM acs_2014_2018_stats;
|
||||||
|
|
||||||
@ -73,28 +72,24 @@ FROM acs_2014_2018_stats;
|
|||||||
SELECT stddev_pop(median_hh_income)
|
SELECT stddev_pop(median_hh_income)
|
||||||
FROM acs_2014_2018_stats;
|
FROM acs_2014_2018_stats;
|
||||||
|
|
||||||
-- Covariance
|
|
||||||
SELECT covar_pop(median_hh_income, pct_bachelors_higher)
|
|
||||||
FROM acs_2014_2018_stats;
|
|
||||||
|
|
||||||
-- Listing 11-6: The rank() and dense_rank() window functions
|
-- Listing 11-6: The rank() and dense_rank() window functions
|
||||||
|
|
||||||
CREATE TABLE widget_companies (
|
CREATE TABLE widget_companies (
|
||||||
id bigserial,
|
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
|
||||||
company varchar(30) NOT NULL,
|
company text NOT NULL,
|
||||||
widget_output integer NOT NULL
|
widget_output integer NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO widget_companies (company, widget_output)
|
INSERT INTO widget_companies (company, widget_output)
|
||||||
VALUES
|
VALUES
|
||||||
('Morse Widgets', 125000),
|
('Dom Widgets', 125000),
|
||||||
('Springfield Widget Masters', 143000),
|
('Ariadne Widget Masters', 143000),
|
||||||
('Best Widgets', 196000),
|
('Saito Widget Co.', 201000),
|
||||||
('Acme Inc.', 133000),
|
('Mal Inc.', 133000),
|
||||||
('District Widget Inc.', 201000),
|
('Dream Widget Inc.', 196000),
|
||||||
('Clarke Amalgamated', 620000),
|
('Miles Amalgamated', 620000),
|
||||||
('Stavesacre Industries', 244000),
|
('Arthur Industries', 244000),
|
||||||
('Bowers Widget Emporium', 201000);
|
('Fischer Worldwide', 201000);
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
company,
|
company,
|
||||||
@ -106,8 +101,8 @@ FROM widget_companies;
|
|||||||
-- Listing 11-7: Applying rank() within groups using PARTITION BY
|
-- Listing 11-7: Applying rank() within groups using PARTITION BY
|
||||||
|
|
||||||
CREATE TABLE store_sales (
|
CREATE TABLE store_sales (
|
||||||
store varchar(30),
|
store text NOT NULL,
|
||||||
category varchar(30) NOT NULL,
|
category text NOT NULL,
|
||||||
unit_sales bigint NOT NULL,
|
unit_sales bigint NOT NULL,
|
||||||
CONSTRAINT store_category_key PRIMARY KEY (store, category)
|
CONSTRAINT store_category_key PRIMARY KEY (store, category)
|
||||||
);
|
);
|
||||||
@ -131,9 +126,35 @@ SELECT
|
|||||||
rank() OVER (PARTITION BY category ORDER BY unit_sales DESC)
|
rank() OVER (PARTITION BY category ORDER BY unit_sales DESC)
|
||||||
FROM store_sales;
|
FROM store_sales;
|
||||||
|
|
||||||
|
-- Listing 11-8: Creating a rolling average for export data
|
||||||
|
|
||||||
|
CREATE TABLE us_exports (
|
||||||
|
year smallint,
|
||||||
|
month smallint,
|
||||||
|
citrus_export_value bigint,
|
||||||
|
soybeans_export_value bigint
|
||||||
|
);
|
||||||
|
|
||||||
--
|
COPY us_exports
|
||||||
|
ROM 'C:\YourDirectory\us_exports.csv'
|
||||||
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
|
-- View the monthly citrus data
|
||||||
|
SELECT year, month, citrus_export_value
|
||||||
|
FROM us_exports
|
||||||
|
ORDER BY year, month;
|
||||||
|
|
||||||
|
-- Calculate rolling average
|
||||||
|
SELECT year, month, citrus_export_value,
|
||||||
|
round(
|
||||||
|
avg(citrus_export_value)
|
||||||
|
OVER(ORDER BY year, month
|
||||||
|
ROWS BETWEEN 11 PRECEDING AND CURRENT ROW), 0)
|
||||||
|
AS twelve_month_avg
|
||||||
|
FROM us_exports
|
||||||
|
ORDER BY year, month;
|
||||||
|
|
||||||
|
-- Listing 11-9: Creating and filling a table for Census county business pattern data
|
||||||
|
|
||||||
CREATE TABLE cbp_naics_72_establishments (
|
CREATE TABLE cbp_naics_72_establishments (
|
||||||
state_fips text,
|
state_fips text,
|
||||||
@ -148,61 +169,22 @@ CREATE TABLE cbp_naics_72_establishments (
|
|||||||
);
|
);
|
||||||
|
|
||||||
COPY cbp_naics_72_establishments
|
COPY cbp_naics_72_establishments
|
||||||
FROM '/Users/adebarros/Dropbox/DataMonky/Book-Writing/PracticalSQL_2e/Code-Repo/Chapter_11/cbp_naics_72_establishments.csv'
|
FROM 'C:\YourDirectory\cbp_naics_72_establishments.csv'
|
||||||
--FROM 'C:\YourDirectory\cbp_naics_72_establishments.csv'
|
WITH (FORMAT CSV, HEADER);
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
|
||||||
|
|
||||||
|
SELECT * FROM cbp_naics_72_establishments LIMIT 5;
|
||||||
|
|
||||||
SELECT cbp.state_fips || cbp.county_fips AS fips,
|
-- Listing 11-10: Finding business rates per thousand population in counties with 50,000 or more people
|
||||||
cbp.county,
|
|
||||||
cbp.st,
|
|
||||||
cbp.establishments,
|
|
||||||
pop.pop_est_2018,
|
|
||||||
round(
|
|
||||||
(cbp.establishments::numeric / pop.pop_est_2018) * 1000, 1
|
|
||||||
) AS estabs_per_1000
|
|
||||||
FROM
|
|
||||||
cbp_naics_72_establishments cbp LEFT JOIN us_counties_pop_est_2019 pop
|
|
||||||
ON cbp.state_fips = pop.state_fips
|
|
||||||
AND cbp.county_fips = pop.county_fips
|
|
||||||
WHERE pop.pop_est_2018 >= 50000
|
|
||||||
ORDER BY cbp.establishments::numeric / pop.pop_est_2018 DESC;
|
|
||||||
|
|
||||||
-- OLD first edition
|
|
||||||
|
|
||||||
|
|
||||||
-- Listing 11-8: Create and fill a 2015 FBI crime data table
|
|
||||||
|
|
||||||
CREATE TABLE fbi_crime_data_2015 (
|
|
||||||
st varchar(20),
|
|
||||||
city varchar(50),
|
|
||||||
population integer,
|
|
||||||
violent_crime integer,
|
|
||||||
property_crime integer,
|
|
||||||
burglary integer,
|
|
||||||
larceny_theft integer,
|
|
||||||
motor_vehicle_theft integer,
|
|
||||||
CONSTRAINT st_city_key PRIMARY KEY (st, city)
|
|
||||||
);
|
|
||||||
|
|
||||||
COPY fbi_crime_data_2015
|
|
||||||
FROM 'C:\YourDirectory\fbi_crime_data_2015.csv'
|
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
|
||||||
|
|
||||||
SELECT * FROM fbi_crime_data_2015
|
|
||||||
ORDER BY population DESC;
|
|
||||||
|
|
||||||
-- Listing 11-9: Find property crime rates per thousand in cities with 500,000
|
|
||||||
-- or more people
|
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
city,
|
cbp.county,
|
||||||
st,
|
cbp.st,
|
||||||
population,
|
cbp.establishments,
|
||||||
property_crime,
|
pop.pop_est_2018,
|
||||||
round(
|
round( (cbp.establishments::numeric / pop.pop_est_2018) * 1000, 1 )
|
||||||
(property_crime::numeric / population) * 1000, 1
|
AS estabs_per_1000
|
||||||
) AS pc_per_1000
|
FROM cbp_naics_72_establishments cbp LEFT JOIN us_counties_pop_est_2019 pop
|
||||||
FROM fbi_crime_data_2015
|
ON cbp.state_fips = pop.state_fips
|
||||||
WHERE population >= 500000
|
AND cbp.county_fips = pop.county_fips
|
||||||
ORDER BY (property_crime::numeric / population) DESC;
|
WHERE pop.pop_est_2018 >= 50000
|
||||||
|
ORDER BY cbp.establishments::numeric / pop.pop_est_2018 DESC;
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
3143
Chapter_11/acs_2014_2018_stats.csv
Normal file
3143
Chapter_11/acs_2014_2018_stats.csv
Normal file
File diff suppressed because it is too large
Load Diff
3075
Chapter_11/cbp_naics_72_establishments.csv
Normal file
3075
Chapter_11/cbp_naics_72_establishments.csv
Normal file
File diff suppressed because it is too large
Load Diff
225
Chapter_11/us_exports.csv
Normal file
225
Chapter_11/us_exports.csv
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
year,month,citrus_export_value,soybeans_export_value
|
||||||
|
2002,1,81939159,794068114
|
||||||
|
2002,2,91145699,659050185
|
||||||
|
2002,3,115051310,345893170
|
||||||
|
2002,4,86779722,246909123
|
||||||
|
2002,5,45814736,247943895
|
||||||
|
2002,6,36015544,249802757
|
||||||
|
2002,7,18333834,339539503
|
||||||
|
2002,8,15058915,243321058
|
||||||
|
2002,9,13271588,191216281
|
||||||
|
2002,10,26590694,812436788
|
||||||
|
2002,11,53639481,919308934
|
||||||
|
2002,12,61170830,701085611
|
||||||
|
2003,1,79069804,991310046
|
||||||
|
2003,2,91905948,944352353
|
||||||
|
2003,3,112045016,588018642
|
||||||
|
2003,4,82714650,424072090
|
||||||
|
2003,5,65807931,235398657
|
||||||
|
2003,6,37152480,221053757
|
||||||
|
2003,7,22025720,250246921
|
||||||
|
2003,8,15347844,212711390
|
||||||
|
2003,9,12997666,230478511
|
||||||
|
2003,10,37309973,1246026488
|
||||||
|
2003,11,54333917,1457358978
|
||||||
|
2003,12,69999550,1164256986
|
||||||
|
2004,1,94487872,968427385
|
||||||
|
2004,2,116664557,714701271
|
||||||
|
2004,3,148210057,674486035
|
||||||
|
2004,4,89528321,304579006
|
||||||
|
2004,5,45931753,189713599
|
||||||
|
2004,6,32178545,210817937
|
||||||
|
2004,7,21438553,141448559
|
||||||
|
2004,8,10944527,100759947
|
||||||
|
2004,9,9966250,303876760
|
||||||
|
2004,10,17371687,1053528352
|
||||||
|
2004,11,46297575,1070602202
|
||||||
|
2004,12,59646056,953920362
|
||||||
|
2005,1,79290510,785578490
|
||||||
|
2005,2,92361489,843596786
|
||||||
|
2005,3,113556560,698225482
|
||||||
|
2005,4,103259234,412721537
|
||||||
|
2005,5,57853049,315114212
|
||||||
|
2005,6,33253877,260911572
|
||||||
|
2005,7,20652393,160938521
|
||||||
|
2005,8,13516221,210525670
|
||||||
|
2005,9,12209375,226293704
|
||||||
|
2005,10,16136054,878190446
|
||||||
|
2005,11,39164454,909878711
|
||||||
|
2005,12,69586703,580244470
|
||||||
|
2006,1,90647111,732623108
|
||||||
|
2006,2,105474601,719732967
|
||||||
|
2006,3,119582761,673898856
|
||||||
|
2006,4,85317096,282056498
|
||||||
|
2006,5,79193517,270076101
|
||||||
|
2006,6,44478040,262376265
|
||||||
|
2006,7,17752553,309444780
|
||||||
|
2006,8,12519827,326320838
|
||||||
|
2006,9,9924518,402749394
|
||||||
|
2006,10,27667251,1182460249
|
||||||
|
2006,11,60010909,889549580
|
||||||
|
2006,12,68907983,897594351
|
||||||
|
2007,1,105326246,1092606423
|
||||||
|
2007,2,90297551,990018125
|
||||||
|
2007,3,120445250,784247691
|
||||||
|
2007,4,96608536,575051574
|
||||||
|
2007,5,53944346,336040732
|
||||||
|
2007,6,27553378,410972130
|
||||||
|
2007,7,16198822,329393569
|
||||||
|
2007,8,9461235,435126692
|
||||||
|
2007,9,9000139,571788118
|
||||||
|
2007,10,29664916,1380861708
|
||||||
|
2007,11,78976704,1378173182
|
||||||
|
2007,12,87396882,1719763106
|
||||||
|
2008,1,100336804,1836020708
|
||||||
|
2008,2,125734229,1888696804
|
||||||
|
2008,3,150929370,1668647250
|
||||||
|
2008,4,119241412,1041463163
|
||||||
|
2008,5,78603045,780652877
|
||||||
|
2008,6,58718347,872860052
|
||||||
|
2008,7,25284986,804165010
|
||||||
|
2008,8,15073035,674660164
|
||||||
|
2008,9,13444938,494556141
|
||||||
|
2008,10,30204485,1956533639
|
||||||
|
2008,11,54859362,1770092128
|
||||||
|
2008,12,66971586,1668263869
|
||||||
|
2009,1,99934953,1571905719
|
||||||
|
2009,2,99601348,1702454398
|
||||||
|
2009,3,143879751,1043785149
|
||||||
|
2009,4,104359130,903399997
|
||||||
|
2009,5,70024775,700816631
|
||||||
|
2009,6,35827565,746873282
|
||||||
|
2009,7,14294631,618525330
|
||||||
|
2009,8,13196062,687034546
|
||||||
|
2009,9,13615442,462744846
|
||||||
|
2009,10,35163233,2137269786
|
||||||
|
2009,11,65961282,3299584065
|
||||||
|
2009,12,75494747,2569699237
|
||||||
|
2010,1,114566021,2540223651
|
||||||
|
2010,2,124233060,1837252524
|
||||||
|
2010,3,183995841,1406572242
|
||||||
|
2010,4,135588001,608475829
|
||||||
|
2010,5,96689107,352767464
|
||||||
|
2010,6,56467068,301759668
|
||||||
|
2010,7,25918299,405149528
|
||||||
|
2010,8,15948706,643054105
|
||||||
|
2010,9,18271562,816948644
|
||||||
|
2010,10,32784054,3594727245
|
||||||
|
2010,11,65843403,3416531504
|
||||||
|
2010,12,86249358,2714785350
|
||||||
|
2011,1,119045881,2709902699
|
||||||
|
2011,2,164306721,2488744968
|
||||||
|
2011,3,208216509,1826752627
|
||||||
|
2011,4,155145073,960921884
|
||||||
|
2011,5,101145805,497448258
|
||||||
|
2011,6,66211941,458111693
|
||||||
|
2011,7,30691394,402428555
|
||||||
|
2011,8,15361251,634382227
|
||||||
|
2011,9,17414070,694642976
|
||||||
|
2011,10,28734985,2606946002
|
||||||
|
2011,11,54031921,2426024903
|
||||||
|
2011,12,98512159,1905712354
|
||||||
|
2012,1,116314568,2242366369
|
||||||
|
2012,2,163481404,2043786309
|
||||||
|
2012,3,210923029,1630138170
|
||||||
|
2012,4,145157754,1117738831
|
||||||
|
2012,5,101536782,1024085540
|
||||||
|
2012,6,61418454,801469156
|
||||||
|
2012,7,22542688,1166010698
|
||||||
|
2012,8,14284760,1267198087
|
||||||
|
2012,9,12790880,1659675173
|
||||||
|
2012,10,36123896,4671385966
|
||||||
|
2012,11,68285554,4209675188
|
||||||
|
2012,12,86025609,2973846322
|
||||||
|
2013,1,121281012,3083515201
|
||||||
|
2013,2,164868221,2283856119
|
||||||
|
2013,3,228269907,1158214685
|
||||||
|
2013,4,144062517,546244146
|
||||||
|
2013,5,86170789,347483402
|
||||||
|
2013,6,48141351,316242933
|
||||||
|
2013,7,20877974,230334756
|
||||||
|
2013,8,14694105,272308939
|
||||||
|
2013,9,11503786,832007245
|
||||||
|
2013,10,37957305,4149994287
|
||||||
|
2013,11,86098688,4689124896
|
||||||
|
2013,12,112137714,3696508434
|
||||||
|
2014,1,132960381,3808069205
|
||||||
|
2014,2,145823447,2958781283
|
||||||
|
2014,3,190485143,1799107982
|
||||||
|
2014,4,118021632,685288021
|
||||||
|
2014,5,93742216,503950768
|
||||||
|
2014,6,50524537,364025770
|
||||||
|
2014,7,18101963,288516999
|
||||||
|
2014,8,13680724,252164257
|
||||||
|
2014,9,10999733,960793480
|
||||||
|
2014,10,39742015,3868429034
|
||||||
|
2014,11,81665964,4796051712
|
||||||
|
2014,12,104271776,3637625551
|
||||||
|
2015,1,126682504,3066056057
|
||||||
|
2015,2,156254508,1961449198
|
||||||
|
2015,3,193699330,1081619892
|
||||||
|
2015,4,131278820,569611797
|
||||||
|
2015,5,85121730,489803825
|
||||||
|
2015,6,45647144,399181413
|
||||||
|
2015,7,24846635,439471655
|
||||||
|
2015,8,17088292,477996416
|
||||||
|
2015,9,11002503,880577152
|
||||||
|
2015,10,31221705,3693398810
|
||||||
|
2015,11,71632407,3393311754
|
||||||
|
2015,12,97667823,2460650580
|
||||||
|
2016,1,112721187,2178001053
|
||||||
|
2016,2,168607847,2062374601
|
||||||
|
2016,3,183363169,956751612
|
||||||
|
2016,4,152415213,512643030
|
||||||
|
2016,5,100725071,357599772
|
||||||
|
2016,6,51703351,453526359
|
||||||
|
2016,7,23547099,1159018241
|
||||||
|
2016,8,16261344,1723671655
|
||||||
|
2016,9,13215668,1516753088
|
||||||
|
2016,10,28130358,4482728117
|
||||||
|
2016,11,86551070,4195995512
|
||||||
|
2016,12,102436973,3285557650
|
||||||
|
2017,1,102783104,2837746400
|
||||||
|
2017,2,165393310,1835060288
|
||||||
|
2017,3,251153742,1291651704
|
||||||
|
2017,4,146816401,964459631
|
||||||
|
2017,5,89066739,580819978
|
||||||
|
2017,6,35075913,687560641
|
||||||
|
2017,7,18123991,871422998
|
||||||
|
2017,8,14202432,1133261413
|
||||||
|
2017,9,11412061,1714391653
|
||||||
|
2017,10,27024223,3708945739
|
||||||
|
2017,11,62961726,3529020687
|
||||||
|
2017,12,96860554,2377661750
|
||||||
|
2018,1,133108140,2196527236
|
||||||
|
2018,2,195453600,1638130524
|
||||||
|
2018,3,205113022,1284671733
|
||||||
|
2018,4,141333137,896365203
|
||||||
|
2018,5,79372432,1265857407
|
||||||
|
2018,6,29608161,1221069038
|
||||||
|
2018,7,15711114,1258513068
|
||||||
|
2018,8,12997986,1237523273
|
||||||
|
2018,9,11052844,1160235948
|
||||||
|
2018,10,22812344,1904783049
|
||||||
|
2018,11,59727020,1694889673
|
||||||
|
2018,12,84653964,1399186554
|
||||||
|
2019,1,108193268,1698008495
|
||||||
|
2019,2,126464885,1604303259
|
||||||
|
2019,3,192601690,1388740203
|
||||||
|
2019,4,138485448,900265075
|
||||||
|
2019,5,79001506,873353636
|
||||||
|
2019,6,35539946,1160314171
|
||||||
|
2019,7,16984047,1320519403
|
||||||
|
2019,8,15108854,1757349487
|
||||||
|
2019,9,14012305,1373995500
|
||||||
|
2019,10,26308151,2115640933
|
||||||
|
2019,11,60885676,2476088336
|
||||||
|
2019,12,84873954,2059107670
|
||||||
|
2020,1,110924836,1954840496
|
||||||
|
2020,2,171767821,1005152257
|
||||||
|
2020,3,201231998,944016991
|
||||||
|
2020,4,122708243,783880798
|
||||||
|
2020,5,75644260,704320137
|
||||||
|
2020,6,36090558,645792528
|
||||||
|
2020,7,20561815,793474898
|
||||||
|
2020,8,15510692,1646514743
|
||||||
|
@ -662,4 +662,70 @@ FROM meat_poultry_egg_establishments
|
|||||||
WHERE meat_processing = TRUE AND
|
WHERE meat_processing = TRUE AND
|
||||||
poultry_processing = TRUE;
|
poultry_processing = TRUE;
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
-- Chapter 11: Creating Your First Database and Table
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
-- 1. In Listing 11-2, the correlation coefficient, or r value, of the
|
||||||
|
-- variables pct_bachelors_higher and median_hh_income was about .70.
|
||||||
|
-- Write a query to show the correlation between pct_masters_higher and
|
||||||
|
-- median_hh_income. Is the r value higher or lower? What might explain
|
||||||
|
-- the difference?
|
||||||
|
|
||||||
|
-- Answer:
|
||||||
|
-- The r value of pct_bachelors_higher and median_hh_income is about .60, which
|
||||||
|
-- shows a lower connection between percent master's degree or higher and
|
||||||
|
-- income than percent bachelor's degree or higher and income. One possible
|
||||||
|
-- explanation is that attaining a master's degree or higher may have a more
|
||||||
|
-- incremental impact on earnings than attaining a bachelor's degree.
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
round(
|
||||||
|
corr(median_hh_income, pct_bachelors_higher)::numeric, 2
|
||||||
|
) AS bachelors_income_r,
|
||||||
|
round(
|
||||||
|
corr(median_hh_income, pct_masters_higher)::numeric, 2
|
||||||
|
) AS masters_income_r
|
||||||
|
FROM acs_2014_2018_stats;
|
||||||
|
|
||||||
|
|
||||||
|
-- 2. Using the exports data, create a 12-month rolling sum using the values
|
||||||
|
-- in the column soybeans_export_value and the query pattern from
|
||||||
|
-- Listing 11-8. Copy and paste the results from the pgAdmin output
|
||||||
|
-- pane and graph the values using Excel. What trend do you see?
|
||||||
|
|
||||||
|
-- Answer: Soybean exports rose considerably during the late 2000s
|
||||||
|
-- and dropped off considerably starting in 2018 following the start of the
|
||||||
|
-- U.S. trade war with China.
|
||||||
|
|
||||||
|
SELECT year, month, soybeans_export_value,
|
||||||
|
round(
|
||||||
|
sum(soybeans_export_value)
|
||||||
|
OVER(ORDER BY year, month
|
||||||
|
ROWS BETWEEN 11 PRECEDING AND CURRENT ROW), 0)
|
||||||
|
AS twelve_month_avg
|
||||||
|
FROM us_exports
|
||||||
|
ORDER BY year, month;
|
||||||
|
|
||||||
|
-- 3. As a bonus challenge, revisit the libraries data in the table
|
||||||
|
-- pls_fy2018_libraries in Chapter 9. Rank library agencies based on the rate
|
||||||
|
-- of visits per 1,000 population (variable popu_lsa), and limit the query to
|
||||||
|
-- agencies serving 250,000 people or more.
|
||||||
|
|
||||||
|
-- Answer:
|
||||||
|
-- Pinellas Public Library Coop tops the rankings with 9,705 visits per
|
||||||
|
-- thousand people (or roughly 10 visits per person).
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
libname,
|
||||||
|
stabr,
|
||||||
|
visits,
|
||||||
|
popu_lsa,
|
||||||
|
round(
|
||||||
|
(visits::numeric / popu_lsa) * 1000, 1
|
||||||
|
) AS visits_per_1000,
|
||||||
|
rank() OVER (ORDER BY (visits::numeric / popu_lsa) * 1000 DESC)
|
||||||
|
FROM pls_fy2018_libraries
|
||||||
|
WHERE popu_lsa >= 250000;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user