Post

๐Ÿ˜ PostgreSQL ๊ธฐ๋ณธ โ…จ - ๊ทธ๋ฃจํ•‘๊ณผ ์š”์•ฝ์œผ๋กœ ์ •๋ณด ์ถ”์ถœ

๐Ÿ˜ PostgreSQL ๊ธฐ๋ณธ โ…จ - ๊ทธ๋ฃจํ•‘๊ณผ ์š”์•ฝ์œผ๋กœ ์ •๋ณด ์ถ”์ถœ

๐Ÿ“™ ใ€Ž์‹ค์šฉ SQLใ€์„ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

๋„์„œ๊ด€ ์กฐ์‚ฌ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
CREATE TABLE pls_fy2018_libraries (
	stabr text NOT NULL,
	fscskey text CONSTRAINT fscskey_2018_pkey PRIMARY KEY,
	libid text NOT NULL,
	libname text NOT NULL,
	address text NOT NULL,
	city text NOT NULL,
	zip text NOT NULL,
	county text NOT NULL,
	phone text NOT NULL,
	c_relatn text NOT NULL,
	c_legbas text NOT NULL,
	c_admin text NOT NULL,
	c_fscs text NOT NULL,
	geocode text NOT NULL,
	lsabound text NOT NULL,
	startdate text NOT NULL,
	enddate text NOT NULL,
	popu_lsa integer NOT NULL,
	popu_und integer NOT NULL,
	centlib integer NOT NULL,
	branlib integer NOT NULL,
	bkmob integer NOT NULL,
	totstaff numeric(8,2) NOT NULL,
	bkvol integer NOT NULL,
	ebook integer NOT NULL,
	audio_ph integer NOT NULL,
	audio_dl integer NOT NULL,
	video_ph integer NOT NULL,
	video_dl integer NOT NULL,
	ec_lo_ot integer NOT NULL,
	subscrip integer NOT NULL,
	hrs_open integer NOT NULL,
	visits integer NOT NULL,
	reference integer NOT NULL,
	regbor integer NOT NULL,
	totcir integer NOT NULL,
	kidcircl integer NOT NULL,
	totpro integer NOT NULL,
	gpterms integer NOT NULL,
	pitusr integer NOT NULL,
	wifisess integer NOT NULL,
	obereg text NOT NULL,
	statstru text NOT NULL,
	statname text NOT NULL,
	stataddr text NOT NULL,
	longitude numeric(10,7) NOT NULL,
	latitude numeric(10,7) NOT NULL
);

COPY pls_fy2018_libraries
FROM 'C:\YourDirectory\pls_fy2018_libraries.csv'
WITH (FORMAT CSV, HEADER);

CREATE INDEX libname_2018_idx ON pls_fy2018_libraries (libname);
  • ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ์ฟผ๋ฆฌ ์†๋„ ํ–ฅ์ƒ์„ ์œ„ํ•ด ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

2016, 2017๋…„๋„ ๋„์„œ๊ด€ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
CREATE TABLE pls_fy2017_libraries (
	stabr text NOT NULL,
	fscskey text CONSTRAINT fscskey_17_pkey PRIMARY KEY,
	libid text NOT NULL,
	libname text NOT NULL,
	address text NOT NULL,
	city text NOT NULL,
	zip text NOT NULL,
	county text NOT NULL,
	phone text NOT NULL,
	c_relatn text NOT NULL,
	c_legbas text NOT NULL,
	c_admin text NOT NULL,
	c_fscs text NOT NULL,
	geocode text NOT NULL,
	lsabound text NOT NULL,
	startdate text NOT NULL,
	enddate text NOT NULL,
	popu_lsa integer NOT NULL,
	popu_und integer NOT NULL,
	centlib integer NOT NULL,
	branlib integer NOT NULL,
	bkmob integer NOT NULL,
	totstaff numeric(8,2) NOT NULL,
	bkvol integer NOT NULL,
	ebook integer NOT NULL,
	audio_ph integer NOT NULL,
	audio_dl integer NOT NULL,
	video_ph integer NOT NULL,
	video_dl integer NOT NULL,
	ec_lo_ot integer NOT NULL,
	subscrip integer NOT NULL,
	hrs_open integer NOT NULL,
	visits integer NOT NULL,
	reference integer NOT NULL,
	regbor integer NOT NULL,
	totcir integer NOT NULL,
	kidcircl integer NOT NULL,
	totpro integer NOT NULL,
	gpterms integer NOT NULL,
	pitusr integer NOT NULL,
	wifisess integer NOT NULL,
	obereg text NOT NULL,
	statstru text NOT NULL,
	statname text NOT NULL,
	stataddr text NOT NULL,
	longitude numeric(10,7) NOT NULL,
	latitude numeric(10,7) NOT NULL
);

CREATE TABLE pls_fy2016_libraries (
	stabr text NOT NULL,
	fscskey text CONSTRAINT fscskey_16_pkey PRIMARY KEY,
	libid text NOT NULL,
	libname text NOT NULL,
	address text NOT NULL,
	city text NOT NULL,
	zip text NOT NULL,
	county text NOT NULL,
	phone text NOT NULL,
	c_relatn text NOT NULL,
	c_legbas text NOT NULL,
	c_admin text NOT NULL,
	c_fscs text NOT NULL,
	geocode text NOT NULL,
	lsabound text NOT NULL,
	startdate text NOT NULL,
	enddate text NOT NULL,
	popu_lsa integer NOT NULL,
	popu_und integer NOT NULL,
	centlib integer NOT NULL,
	branlib integer NOT NULL,
	bkmob integer NOT NULL,
	totstaff numeric(8,2) NOT NULL,
	bkvol integer NOT NULL,
	ebook integer NOT NULL,
	audio_ph integer NOT NULL,
	audio_dl integer NOT NULL,
	video_ph integer NOT NULL,
	video_dl integer NOT NULL,
	ec_lo_ot integer NOT NULL,
	subscrip integer NOT NULL,
	hrs_open integer NOT NULL,
	visits integer NOT NULL,
	reference integer NOT NULL,
	regbor integer NOT NULL,
	totcir integer NOT NULL,
	kidcircl integer NOT NULL,
	totpro integer NOT NULL,
	gpterms integer NOT NULL,
	pitusr integer NOT NULL,
	wifisess integer NOT NULL,
	obereg text NOT NULL,
	statstru text NOT NULL,
	statname text NOT NULL,
	stataddr text NOT NULL,
	longitude numeric(10,7) NOT NULL,
	latitude numeric(10,7) NOT NULL
);

COPY pls_fy2017_libraries
FROM 'C:\YourDirectory\pls_fy2017_libraries.csv'
WITH (FORMAT CSV, HEADER);

COPY pls_fy2016_libraries
FROM 'C:\YourDirectory\pls_fy2016_libraries.csv'
WITH (FORMAT CSV, HEADER);

CREATE INDEX libname_2017_idx ON pls_fy2017_libraries (libname);
CREATE INDEX libname_2016_idx ON pls_fy2016_libraries (libname);
  • ์„ธ ํ…Œ์ด๋ธ” ๋ชจ๋‘ ๋™์ผํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„์„œ๊ด€ ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ํ•˜๊ธฐ

count()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ–‰๊ณผ ๊ฐ’ ์„ธ๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-- count()๋กœ ํ…Œ์ด๋ธ” ํ–‰ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
SELECT count(*)
FROM pls_fy2018_libraries;

SELECT count(*)
FROM pls_fy2017_libraries;

SELECT count(*)
FROM pls_fy2016_libraries;

-- count()๋ฅผ ์ด์šฉํ•œ NULL์ด ์•„๋‹Œ ๊ฐ’ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
SELECT count(phone)
FROM pls_fy2018_libraries;

-- count()๋ฅผ ์‚ฌ์šฉํ•ด ์—ด ์•ˆ์˜ ๊ณ ์œ ๊ฐ’ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
SELECT count(libname)
FROM pls_fy2018_libraries;

SELECT count(DISTINCT libname)
FROM pls_fy2018_libraries;
  • count(*)์€ ๋ชจ๋“  ํ–‰์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , count()์— ์ธ์ž๋กœ ์—ด ์ด๋ฆ„์„ ๋„˜๊ธฐ๋ฉด, ํ•ด๋‹น ์—ด์˜ NULL ๊ฐ’์„ ์ œ์™ธํ•œ ํ–‰ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • DISTINCT ํ‚ค์›Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํ•ด๋‹น ์—ด์˜ ๊ณ ์œ  ๊ฐ’ ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜ ์žˆ๋‹ค.

min()๊ณผ max()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’ ์ฐพ๊ธฐ

  • ์ด ๋‘ ํ•จ์ˆ˜๋Š” ๋ณด๊ณ ๋œ ๊ฐ’์ด ๋ฒ”์œ„๋ฅผ ์ดํ•ดํ•˜์—ฌ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๋ฌธ์ œ๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
1
2
SELECT max(visits), min(visits)
FROM pls_fy2018_libraries;
maxmin
16,686,945-3
  • ์ตœ์†Ÿ๊ฐ’์œผ๋กœ -3์ด ์กฐํšŒ๋˜์—ˆ๋Š”๋ฐ, ๊ฐ€๋Šฅํ•œ ์ˆ˜์น˜์ผ๊นŒ?
  • ๋‹ค์†Œ ๋ฌธ์ œ๊ฐ€ ์žˆ์ง€๋งŒ, ์„ค๋ฌธ์กฐ์‚ฌ๋ฅผ ๋งŒ๋“  ์ž‘์„ฑ์ž๊ฐ€ ์‘๋‹ต ์—†์Œ์„ -1, ํ•ด๋‹น ์—†์Œ์„ -3 ๊ฐ’์œผ๋กœ ๋‘์–ด ๋ฐœ์ƒํ•œ ์ผ์ด์—ˆ๋‹ค.
  • ์—ด์„ ๋ชจ๋‘ ๋”ํ•  ๋•Œ ์Œ์ˆ˜ ๊ฐ’์„ ํฌํ•จํ•˜๋ฉด ํ•ฉ๊ณ„๊ฐ€ ์ž˜๋ชป ๊ณ„์‚ฐ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ์ดํ„ฐ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ๋Š” ์Œ์ˆ˜ ๊ฐ’์„ ๊ณ ๋ คํ•˜๊ณ  ์ œ์™ธํ•ด์•ผ ํ•œ๋‹ค.
  • ์ด๋Š” WHERE ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

๋” ์ข‹์€ ๋ฐฉ๋ฒ•์€, ์‘๋‹ต ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ NULL ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๊ณ , ๋ณ„๋„์˜ visit_flag ์—ด์„ ๋งŒ๋“ค์–ด ๊ทธ ์ด์œ ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

GROUP BY๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ง‘๊ณ„ํ•˜๊ธฐ

  • ์ง‘๊ณ„ ํ•จ์ˆ˜์™€ GROUP BY ์ ˆ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ๊ฐœ ์ด์ƒ์˜ ์—ด์— ์žˆ๋Š” ๊ฐ’์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๋ฅผ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ชจ๋“  ์ฃผ ๋˜๋Š” ๋ชจ๋“  ์œ ํ˜•์˜ ๋„์„œ๊ด€ ๊ธฐ๊ด€์— ๋Œ€ํ•ด sum() ๋˜๋Š” count()์™€ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
1
2
3
4
5
6
7
8
9
SELECT stabr
FROM pls_fy2018_libraries
GROUP BY stabr
ORDER BY stabr;

SELECT stabr
FROM pls_fy2017_libraries
GROUP BY stabr
ORDER BY stabr;
  • GROUB BY๋Š” DISTINCT์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ค‘๋ณต ๊ฐ’์„ ์ œ๊ฑฐํ•œ๋‹ค.
  • ๋ฌผ๋ก  ๋‹จ ํ•˜๋‚˜์˜ ์—ด๋กœ๋งŒ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.
1
2
3
4
SELECT city, stabr
FROM pls_fy2018_libraries
GROUP BY city, stabr
ORDER BY city, stabr;
  • ๊ฒฐ๊ณผ๋Š” ORDER BY ์ ˆ์— ์˜ํ•ด ๋„์‹œ ๋ณ„๋กœ ์ •๋ ฌ๋œ ๋’ค, ์ฃผ ๋ณ„๋กœ ์ •๋ ฌ๋œ๋‹ค.

GROUP BY์™€ count() ๊ฒฐํ•ฉํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
SELECT stabr, count(*)
FROM pls_fy2018_libraries
GROUP BY stabr
ORDER BY count(*) DESC;

SELECT libname, count(libname)
FROM pls_fy2018_libraries
GROUP BY libname
ORDER BY count(libname) DESC;
  • ์ฒซ ๋ฒˆ์งธ ์ฟผ๋ฆฌ๋Š” ์ฃผ ๋ณ„๋กœ ๋„์„œ๊ด€ ์ˆ˜๋ฅผ ์กฐํšŒํ•˜์—ฌ ์–ด๋А ์ฃผ์— ๋„์„œ๊ด€์ด ๊ฐ€์žฅ ๋งŽ์€์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์ฟผ๋ฆฌ์ฒ˜๋Ÿผ ๊ฐœ๋ณ„ ์—ด์„ ์ง‘๊ณ„ ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์กฐํšŒํ•  ๋•Œ๋Š” GROUP BY ์ ˆ์— ๊ทธ ์—ด์„ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค.
  • ์—ด์„ ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰์—์„œ GROUP BY๋ฅผ count()์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
SELECT stabr, stataddr, count(*)
FROM pls_fy2018_libraries
GROUP BY stabr, stataddr
ORDER BY stabr, stataddr;

SELECT city, stabr, count(*)
FROM pls_fy2018_libraries
GROUP BY city, stabr
ORDER BY count(*) DESC;
  • ์œ„ ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‘ ์—ด์˜ ๊ณ ์œ ํ•œ ์กฐํ•ฉ ์ˆ˜๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‘ ์ฟผ๋ฆฌ์˜ ์ฐจ์ด์ ์€ ๋‹จ์ˆœํžˆ ์ •๋ ฌ ๋ฐฉ์‹์— ์žˆ๋‹ค.

sum()์„ ์‚ฌ์šฉํ•ด ๋„์„œ๊ด€ ๋ฐฉ๋ฌธ ์ˆ˜ ์‚ดํŽด๋ณด๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 2018
SELECT sum(visits) AS visits_2018
FROM pls_fy2018_libraries
WHERE visits >= 0;

-- 2017
SELECT sum(visits) AS visits_2017
FROM pls_fy2017_libraries
WHERE visits >= 0;

-- 2016
SELECT sum(visits) AS visits_2016
FROM pls_fy2016_libraries
WHERE visits >= 0;
  • ์‘๋‹ต ์—†์Œ๊ณผ ํ•ด๋‹น ์—†์Œ ๊ฐ’์€ ์Œ์ˆ˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š”๋ฐ, ๋ถ„์„์— ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š๋„๋ก ์Œ์ˆ˜ ๊ฐ’์„ ์ œ์™ธํ•˜์˜€๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- ๋ฐฉ๋ฌธ์ž ์ˆ˜
SELECT sum(pls18.visits) AS visits_2018,
	sum(pls17.visits) AS visits_2017,
	sum(pls16.visits) AS visits_2016
FROM pls_fy2018_libraries pls18
	JOIN pls_fy2017_libraries pls17 ON pls18.fscskey = pls17.fscskey
	JOIN pls_fy2016_libraries pls16 ON pls18.fscskey = pls16.fscskey
WHERE pls18.visits >= 0
	AND pls17.visits >= 0
	AND pls16.visits >= 0;

-- ์™€์ดํŒŒ์ด ์„ธ์…˜ ์ˆ˜
SELECT sum(pls18.wifisess) AS wifi_2018,
	sum(pls17.wifisess) AS wifi_2017,
	sum(pls16.wifisess) AS wifi_2016
FROM pls_fy2018_libraries pls18
	JOIN pls_fy2017_libraries pls17 ON pls18.fscskey = pls17.fscskey
	JOIN pls_fy2016_libraries pls16 ON pls18.fscskey = pls16.fscskey
WHERE pls18.wifisess >= 0
	AND pls17.wifisess >= 0
	AND pls16.wifisess >= 0;
  • INNER JOIN์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ ํ…Œ์ด๋ธ”์˜ ์ง‘๊ณ„ ๊ฐ’์„ ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ•œ๋‹ค.
  • fscskey ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ–ˆ์ง€๋งŒ ๋‘ ํ…Œ์ด๋ธ” ๋ชจ๋‘์— ํ‘œ์‹œ๋˜๋Š” ๋„์„œ๊ด€ ์ค‘ ์ผ๋ถ€๊ฐ€ 3๋…„ ์‚ฌ์ด์— ๋ณ‘ํ•ฉ๋˜๊ฑฐ๋‚˜ ๋ถ„ํ• ๋˜์—ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.
  • ์ž‘์—… ์ „์—๋Š” ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ฃผ์˜ ์‚ฌํ•ญ์„ ์กฐ์‚ฌํ•ด์•ผ ํ•œ๋‹ค.

์ฃผ ๋ณ„๋กœ ๋ฐฉ๋ฌธ ํ•ฉ๊ณ„ ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT pls18.stabr,
	sum(pls18.visits) AS visits_2018,
	sum(pls17.visits) AS visits_2017,
	sum(pls16.visits) AS visits_2016,
	round( (sum(pls18.visits::numeric) - sum(pls17.visits)) /
		sum(pls17.visits) * 100, 1 ) AS chg_2018_17,
	round( (sum(pls17.visits::numeric) - sum(pls16.visits)) /
		sum(pls16.visits) * 100, 1 ) AS chg_2017_16
FROM pls_fy2018_libraries pls18
	JOIN pls_fy2017_libraries pls17 ON pls18.fscskey = pls17.fscskey
	JOIN pls_fy2016_libraries pls16 ON pls18.fscskey = pls16.fscskey
WHERE pls18.visits >= 0
	AND pls17.visits >= 0
	AND pls16.visits >= 0
GROUP BY pls18.stabr
ORDER BY chg_2018_17 DESC;
  • ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ณ€ํ™”์œจ ๊ณต์‹์„ ํฌํ•จํ•˜๊ณ , chg_2018_17 ๋ณ„์นญ์„ ORDER BY ์ ˆ์— ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
stabrvisits_2018visits_2017visits_2016chg_2018_17chg_2017_16
SD3,824,8043,699,2123,722,3763.4-0.6
MT4,332,9004,215,4844,298,2682.8-1.9
FL68,423,68966,697,12270,991,0292.6-6
ND2,216,3772,162,1892,201,7302.5-1.8
ID8,179,0778,029,5038,597,9551.9-6.6
DC3,632,5393,593,2013,930,7631.1-8.6
ME6,746,3806,731,7686,811,4410.2-1.2
NH7,045,0107,028,8007,236,5670.2-2.9
UT15,326,96315,295,49416,096,9110.2-5
DE4,122,1814,117,9044,125,8990.1-0.2
OK13,399,26513,491,19413,112,511-0.72.9
WY3,338,7723,367,4133,536,788-0.9-4.8
MA39,926,58340,453,00340,427,356-1.30.1
WA37,338,63537,916,03438,634,499-1.5-1.9
MN22,952,38823,326,30324,033,731-1.6-2.9
NM6,908,6867,036,5827,178,428-1.8-2
VA33,913,16234,563,07935,649,602-1.9-3
KS13,483,33313,737,90013,699,223-1.90.3
NY97,921,323100,012,193103,081,304-2.1-3
WI30,097,18330,865,47031,442,577-2.5-1.8
AL14,188,64714,583,05515,637,164-2.7-6.7
CO31,085,35631,975,61532,011,432-2.8-0.1
MI44,758,91846,052,56146,734,166-2.8-1.5
CA146,656,984151,056,672155,613,529-2.9-2.9
NJ40,947,97842,181,06142,429,576-2.9-0.6
CT20,423,51521,051,59721,603,777-3-2.6
RI5,490,0765,669,3095,778,025-3.2-1.9
IN30,836,05131,849,19533,363,879-3.2-4.5
PA40,885,87642,243,04944,105,513-3.2-4.2
OR19,592,29520,244,49920,391,927-3.2-0.7
IA16,674,97617,245,76417,753,953-3.3-2.9
NE7,449,8687,726,1277,873,829-3.6-1.9
NV9,334,0709,684,9359,733,359-3.6-0.5
AK3,268,0733,402,4863,467,234-4-1.9
VT3,526,3573,673,5013,721,332-4-1.3
SC13,989,51114,567,58515,802,934-4-7.8
IL63,466,88766,166,08267,336,230-4.1-1.7
NC31,263,89432,621,29333,605,264-4.2-2.9
MD24,976,42926,089,96327,481,583-4.3-5.1
AZ23,439,70724,584,20125,315,276-4.7-2.9
OH68,176,96771,895,85474,119,719-5.2-3
WV4,944,2425,231,2515,231,443-5.50
KY16,910,82817,909,49518,028,488-5.6-0.7
MO24,663,46726,117,63327,065,546-5.6-3.5
LA16,227,59417,211,00720,262,385-5.7-15.1
TX66,168,38770,514,13870,975,901-6.2-0.7
TN18,102,46019,396,55418,701,973-6.73.7
GA26,835,70128,816,23327,987,249-6.93
AR9,551,68610,358,18110,596,035-7.8-2.2
GU75,11981,57271,813-7.913.6
MS7,602,7108,581,9948,915,406-11.4-3.7
HI3,456,1314,135,2294,490,320-16.4-7.9
AS48,82867,84863,166-287.4
  • ์ด ์œ ์šฉํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€๊ฐ€ ํŠนํžˆ ๊ฐ€์žฅ ํฐ ๋ณ€ํ™”์˜ ์›์ธ์„ ์กฐ์‚ฌํ•˜๋„๋ก ์œ ๋„ํ•œ๋‹ค.

HAVING์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง‘๊ณ„ ์ฟผ๋ฆฌ ํ•„ํ„ฐ๋งํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT pls18.stabr,
	sum(pls18.visits) AS visits_2018,
	sum(pls17.visits) AS visits_2017,
	sum(pls16.visits) AS visits_2016,
	round( (sum(pls18.visits::numeric) - sum(pls17.visits)) /
		sum(pls17.visits) * 100, 1 ) AS chg_2018_17,
	round( (sum(pls17.visits::numeric) - sum(pls16.visits)) /
		sum(pls16.visits) * 100, 1 ) AS chg_2017_16
FROM pls_fy2018_libraries pls18
	JOIN pls_fy2017_libraries pls17 ON pls18.fscskey = pls17.fscskey
	JOIN pls_fy2016_libraries pls16 ON pls18.fscskey = pls16.fscskey
WHERE pls18.visits >= 0
	AND pls17.visits >= 0
	AND pls16.visits >= 0
GROUP BY pls18.stabr
HAVING sum(pls18.visits) > 50000000
ORDER BY chg_2018_17 DESC;
  • ์ง‘๊ณ„ ํ•จ์ˆ˜์— ์กฐ๊ฑด์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” WHERE ํ‚ค์›Œ๋“œ๊ฐ€ ์•„๋‹Œ HAVING ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • HAVING์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์— ์ด ๋ฐฉ๋ฌธ ํšŸ์ˆ˜๊ฐ€ 5์ฒœ๋งŒ ์ด์ƒ์ธ ํ–‰๋งŒ ํฌํ•จ๋˜๋„๋ก ํ•˜์˜€๋‹ค.
  • ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
stabrvisits_2018visits_2017visits_2016chg_2018_17chg_2017_16
FL68,423,68966,697,12270,991,0292.6-6
NY97,921,323100,012,193103,081,304-2.1-3
CA146,656,984151,056,672155,613,529-2.9-2.9
IL63,466,88766,166,08267,336,230-4.1-1.7
OH68,176,96771,895,85474,119,719-5.2-3
TX66,168,38770,514,13870,975,901-6.2-0.7
  • ์œ„ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด โ€œ๋„์„œ๊ด€ ๋ฐฉ๋ฌธ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์ฃผ ๊ฐ€์šด๋ฐ, 2017 ~ 2018๋…„ ์‚ฌ์ด ๋ฐฉ๋ฌธ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•œ ๊ณณ์€ ํ”Œ๋กœ๋ฆฌ๋‹ค์ฃผ๊ฐ€ ์œ ์ผํ–ˆ๊ณ , ๋‚˜๋จธ์ง€๋Š” ๋ฐฉ๋ฌธ ์ˆ˜๊ฐ€ 2 ~ 6% ์ •๋„ ๊ฐ์†Œํ–ˆ์Šต๋‹ˆ๋‹ค.โ€๋ผ๋Š” ์‹์œผ๋กœ ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
This post is licensed under CC BY 4.0 by the author.