0% found this document useful (0 votes)
20 views40 pages

Cleaning Data in PostgreSQL Databases

The document contains a list of restaurant inspections, detailing names, grades, inspection types, and census tracts. It includes specific entries for various restaurants, such as 'Empanadas Monumental' and 'Alphonso's Pizzeria & Trattoria', along with their respective inspection results. Additionally, it features SQL queries related to the inspection data, including functions for string manipulation and data retrieval.

Uploaded by

danndanium
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views40 pages

Cleaning Data in PostgreSQL Databases

The document contains a list of restaurant inspections, detailing names, grades, inspection types, and census tracts. It includes specific entries for various restaurants, such as 'Empanadas Monumental' and 'Alphonso's Pizzeria & Trattoria', along with their respective inspection results. Additionally, it features SQL queries related to the inspection data, including functions for string manipulation and data retrieval.

Uploaded by

danndanium
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

name | grade | inspection_type | census_tract | ...

--------------------------------+-------+-----------------------------------------+--------------+-----
name | grade | inspection_type | census_tract | ...
... | ... | ... | ... | ...
--------------------------------+-------+-----------------------------------------+--------------+-----
EMPANADAS MONUMENTAL | B | Cycle Inspection / Re-inspection | 26900 | ...
... | ... | ... | ... | ...
ALPHONSO'S PIZZERIA & TRATTORIA | A | Cycle Inspection / Initial Inspection | 202 | ...
EMPANADAS MONUMENTAL | B | Cycle Inspection / Re-inspection | 26900 | ...
THE SPARROW TAVERN | A | Cycle Inspection / Initial Inspection | 12500 | ...
ALPHONSO'S PIZZERIA & TRATTORIA | A | Cycle Inspection / Initial Inspection | 202 | ...
BURGER KING | A | Cycle Inspection / Re-inspection | 86400 | ...
THE SPARROW TAVERN | A | Cycle Inspection / Initial Inspection | 12500 | ...
ASTORIA PIZZA | B | Cycle Inspection / Re-inspection | 6300 | ...
BURGER KING | A | Cycle Inspection / Re-inspection | 86400 | ...
... | ... | ... | ... | ...
ASTORIA PIZZA | B | Cycle Inspection / Re-inspection | 6300 | ...
... | ... | ... | ... | ...
name

inspection_type

census_tract
name | grade | inspection_type | census_tract | ... INITCAP(input_string)
--------------------------------+-------+-----------------------------------------+--------------+-----
... | ... | ... | ... | ...
EMPANADAS MONUMENTAL | B | Cycle Inspection / Re-inspection | 26900 | ... SELECT INITCAP('HELLO FRIEND!');
ALPHONSO'S PIZZERIA & TRATTORIA | A | Cycle Inspection / Initial Inspection | 202 | ...
THE SPARROW TAVERN | A | Cycle Inspection / Initial Inspection | 12500 | ...
BURGER KING | A | Cycle Inspection / Re-inspection | 86400 | ...
ASTORIA PIZZA | B | Cycle Inspection / Re-inspection | 6300 | ... Hello Friend!
... | ... | ... | ... | ...

name | grade | inspection_type | census_tract | ...


--------------------------------+-------+---------------------------------------+--------------+-----
... | ... | ... | ... | ...
Empanadas Monumental | B | Cycle Inspection / Re-inspection | 026900 | ...
Alphonso'S Pizzeria & Trattoria | A | Cycle Inspection / Initial Inspection | 000202 | ...
The Sparrow Tavern | A | Cycle Inspection / Initial Inspection | 012500 | ...
Burger King | A | Cycle Inspection / Re-inspection | 086400 | ...
Astoria Pizza | B | Cycle Inspection / Re-inspection | 006300 | ...
... | ... | ... | ... | ...

REPLACE(input_string, to_replace, replacement) LPAD(input_string, length [, fill_value])

SELECT REPLACE('180 Main Street', 'Street', 'St'); SELECT LPAD('123', 7, 'X');

180 Main St XXXX123


SELECT
INITCAP(name) as name,
grade,
REPLACE(inspection_type, ' / ', ' / ') as inspection_type,
LPAD(census_tract, 6, '0') as census_tract
FROM
restaurant_inspection;

name | grade | inspection_type | census_tract | ...


--------------------------------+-------+---------------------------------------+--------------+-----
... | ... | ... | ... | ...
Empanadas Monumental | B | Cycle Inspection / Re-inspection | 026900 | ...
Alphonso'S Pizzeria & Trattoria | A | Cycle Inspection / Initial Inspection | 000202 | ...
The Sparrow Tavern | A | Cycle Inspection / Initial Inspection | 012500 | ...
Burger King | A | Cycle Inspection / Re-inspection | 086400 | ...
Astoria Pizza | B | Cycle Inspection / Re-inspection | 006300 | ...
... | ... | ... | ... | ...

camis | name | inspection_date | score | nta | ...


---------+---------------------------------+-----------------+-------+------+-----
... | ... | ... | ... | ... | ...
41659848 | LA BRISA DEL CIBAO | 01/30/2018 | 20 | QN26 | ...
40961447 | MESON SEVILLA RESTAURANT | 03/19/2019 | 50 | MN15 | ...
50063071 | WA BAR | 05/23/2018 | 15 | MN17 | ...
50034992 | EMPANADAS MONUMENTAL | 06/21/2019 | 17 | MN35 | ...
50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/2020 | 10 | MN28 | ...
41104041 | THE SPARROW TAVERN | 09/17/2019 | 13 | QN72 | ...
50016937 | BURGER KING | 09/14/2018 | 12 | QN55 | ...
50066469 | DARBAR'S CHICKEN & RIBS | 08/07/2017 | 11 | QN55 | ...
41195691 | F & J PINE RESTAURANT | 05/02/2019 | 26 | BX49 | ...
50015706 | EL RINCONCITO DE LOS SABORES | 12/18/2019 | 20 | QN35 | ...
... | ... | ... | .. | ... | ...
SELECT * FROM restaurant_inspection WHERE nta LIKE 'QN544';
%

SELECT * FROM restaurant_inspection WHERE nta = 'QN544';


_

SELECT
*
SELECT
FROM
*
restaurant_inspection
FROM
WHERE nta LIKE 'QN%'
restaurant_inspection
AND nta NOT LIKE 'QN__';
WHERE nta LIKE 'QN%';
LIKE

SELECT
camis, name, inspection_date, score, nta
FROM
restaurant_inspection
WHERE nta SIMILAR TO 'QN%' AND nta NOT SIMILAR TO 'QN__';

SELECT
camis, name, inspection_date, score, nta
FROM
restaurant_inspection
\d \d\d\d
WHERE nta SIMILAR TO 'QN%' AND nta NOT SIMILAR TO 'QN__';

? x\d?
SELECT
camis, name, inspection_date, score, nta
+ \d+
FROM
restaurant_inspection
* \d* WHERE
nta NOT SIMILAR TO '[A-Z][A-Z]\d\d';

[] [a-z]
camis | name | inspection_date | score | nta
---------+--------------------+-----------------+-------+------
41659848 | LA BRISA DEL CIBAO | 01/30/2018 | 20 | Q26
41104041 | THE SPARROW TAVERN | 09/17/2019 | 13 | QN723
121 Fontainebleau Drive

Fountainbleau Fontainbleu Fontainblue

Fountainbleau Fontainbleu Fontainblue F535


fuzzystrmatch DIFFERENCE(string1, string2)

CREATE EXTENSION fuzzystrmatch;


SELECT SOUNDEX('pair') AS sd_pair, SOUNDEX('pear') AS sd_pear;

SOUNDEX(input_string)
sd_pair | sd_pear
--------+---------
SELECT P600 | P600
SOUNDEX('Fountainbleau') as sd1,
SOUNDEX('Fontainebleau') as sd2,
SELECT DIFFERENCE('pair', 'pear') AS diff;
SOUNDEX('Fontaineblue') as sd3;

diff
sd1 | sd2 | sd3
----
-----+------+------
4
F535 | F535 | F535

name | boro | building | street


SELECT SOUNDEX('bow') AS sd_bow, SOUNDEX('bough') AS sd_bough; -----------------------+-----------+----------+------------------------------
ATOMIC WINGS | Manhattan | 2090 | FREDERICK DOUGLASS BOULEVARD
BARAKA BUFFET | Manhattan | 2546 | FREDERICK DOUGLASS BOULEVARD
sd_bout | sd_bought CHOCOLAT | Manhattan | 2217 | FREDERICK DOUGLASS BOULEVARD
ESO | Manhattan | 2906 | FREDERICK DOUGLASS BOULEVARD
--------+----------- HOP HOUSE HARLEM | Manhattan | 2224 | FREDERICK DOUGLASS BOULEVARD

B300 | B230 HOT POT UNDER DE' TREE | Manhattan | 2839 | FREDERICK DOUGLAS BOULEVARD
LIDO | Manhattan | 2168 | FREDERICK DOUGLAS BOULEVARD
MESS HALL | Manhattan | 2194 | FRDRCK DGLS BLVD
VINATERIA | Manhattan | 2211 | FREDERICK DOUGLAS BOULEVARD
SELECT DIFFERENCE('bout', 'bought') AS diff

diff
----
2
name | boro | building | street | sd_street UPDATE
-----------------------+-----------+----------+------------------------------+----------- table_name
ATOMIC WINGS | Manhattan | 2090 | FREDERICK DOUGLASS BOULEVARD | F636 SET
BARAKA BUFFET | Manhattan | 2546 | FREDERICK DOUGLASS BOULEVARD | F636 column_name = value
CHOCOLAT | Manhattan | 2217 | FREDERICK DOUGLASS BOULEVARD | F636
WHERE
ESO | Manhattan | 2906 | FREDERICK DOUGLASS BOULEVARD | F636
condition
HOP HOUSE HARLEM | Manhattan | 2224 | FREDERICK DOUGLASS BOULEVARD | F636
HOT POT UNDER DE' TREE | Manhattan | 2839 | FREDERICK DOUGLAS BOULEVARD | F636
LIDO | Manhattan | 2168 | FREDERICK DOUGLAS BOULEVARD | F636 UPDATE
MESS HALL | Manhattan | 2194 | FRDRCK DGLS BLVD | F636 restaurant_inspection
VINATERIA | Manhattan | 2211 | FREDERICK DOUGLAS BOULEVARD | F636 SET
street = 'Frederick Douglass Boulevard'

SELECT WHERE
name, boro, building, street DIFFERENCE(street, 'Frederick Douglass Boulevard') = 4;
FROM
restaurant_inspections
UPDATE 10
WHERE
DIFFERENCE(street, 'Frederick Douglass Boulevard') = 4;
... | name | score | inspection_type | ...
----+------------------+-------+----------------------------------------------+-----
... | ... | ... | ... | ...
... | SCHNIPPERS | 27 | Cycle Inspection / Initial Inspection | ...
... | ATOMIC WINGS | | Administrative Miscellaneous / Re-inspection | ...
... | WING LING | 44 | Cycle Inspection / Initial Inspection | ...
... | JUAN VALDEZ CAFE | 24 | Cycle Inspection / Initial Inspection | ...
... | FULTON GRAND | 22 | Cycle Inspection / Initial Inspection | ...
... | ... | ... | ... | ...

NULL

''
... | name | score | inspection_type | ...
----+------------------+-------+----------------------------------------------+-----
... | ... | ... | ... | ...
... | SCHNIPPERS | 27 | Cycle Inspection / Initial Inspection | ...
... | ATOMIC WINGS | | Administrative Miscellaneous / Re-inspection | ...
... | WING LING | 44 | Cycle Inspection / Initial Inspection | ...
... | JUAN VALDEZ CAFE | 24 | Cycle Inspection / Initial Inspection | ...
... | FULTON GRAND | 22 | Cycle Inspection / Initial Inspection | ...
... | ... | ... | ... | ...

SELECT
*
FROM
restaurant_inspection
WHERE
score IS NULL;

SELECT
COUNT(*)
FROM
restaurant_inspection
WHERE
score IS NULL;
SELECT inspection_type | count
inspection_type, --------------------------------------------------+-------
COUNT(*) as count Administrative Miscellaneous / Initial Inspection | 104
FROM Smoke-Free Air Act / Initial Inspection | 29
restaurant_inspection Calorie Posting / Initial Inspection | 22
WHERE Administrative Miscellaneous / Re-inspection | 22
score IS NULL Trans Fat / Initial Inspection | 18
GROUP BY Smoke-Free Air Act / Re-inspection | 7
inspection_type Trans Fat / Re-inspection | 3
ORDER BY
count DESC;

COALESCE(arg1, [arg2, ...])


... | name | score | inspection_type | ...
----+------------------+-------+----------------------------------------------+-----
SELECT ... | ... | ... | ... | ...
name, ... | SCHNIPPERS | 27 | Cycle Inspection / Initial Inspection | ...
COALESCE(score, -1), ... | ATOMIC WINGS | -1 | Administrative Miscellaneous / Re-inspection | ...
inspection_type ... | WING LING | 44 | Cycle Inspection / Initial Inspection | ...
FROM ... | JUAN VALDEZ CAFE | 24 | Cycle Inspection / Initial Inspection | ...
restaurant_inspection; ... | FULTON GRAND | 22 | Cycle Inspection / Initial Inspection | ...
... | ... | ... | ... | ...
camis | name | boro | inspection_date | ...
---------+---------------------------------+-----------+-----------------+-----
... | ... | ... | ... | ...
41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | ...
40961447 | MESON SEVILLA RESTAURANT | Manhattan | 03/19/2019 | ...
50063071 | WA BAR | Manhattan | 05/23/2018 | ...
50034992 | EMPANADAS MONUMENTAL | Manhattan | 06/21/2019 | ...
50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | Manhattan | 01/16/2020 | ...
... | ... | ... | ... | ...
SELECT SELECT SELECT

camis camis, camis,


name, name,
FROM
boro boro,
restaurant_inspection
FROM inspection_date
GROUP BY restaurant_inspection FROM
camis GROUP BY restaurant_inspection
HAVING camis, name, boro GROUP BY
COUNT(*) > 1; HAVING camis, name, boro, inspection_date
COUNT(*) > 1; HAVING
COUNT(*) > 1;
579
579
83

SELECT camis | name | boro | inspection_date | violation_code | ...


camis,
---------+--------------------------+-----------+-----------------+----------------+-----
name,
boro,
... | ... | ... | ... | ... | ...
inspection_date, 41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | 04L | ...
violation_code 40961447 | MESON SEVILLA RESTAURANT | Manhattan | 03/19/2019 | 10F | ...
FROM
41630358 | FAY DA BAKERY | Queens | 03/07/2019 | 06E | ...
restaurant_inspection
41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | 04L | ...
GROUP BY
camis, name, boro, inspection_date, violation_code ... | ... | ... | ... | ... | ...
HAVING
COUNT(*) > 1;

0
ROW_NUMBER() OVER() SELECT
camis,
name,
ROW_NUMBER() OVER(
boro,
PARTITION BY
inspection_date,
col1, col2, ...
ORDER BY violation_code,
colA, colB, ... ROW_NUMBER() OVER(
) PARTITION BY
camis,
name,
camis | name | boro | inspection_date | violation_code | row_number | ...
boro,
---------+--------------------------+-----------+-----------------+----------------+------------+----
inspection_date,
... | ... | ... | ... | ... | ... | ...
violation_code
41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | 04L | 1 | ...
41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | 04L | 2 | ... ) - 1 AS duplicate
40961447 | MESON SEVILLA RESTAURANT | Manhattan | 03/19/2019 | 10F | 1 | ... FROM
41630358 | FAY DA BAKERY | Queens | 03/07/2019 | 06E | 1 | ... restaurant_inspection;
... | ... | ... | ... | ... | ... |...

SELECT
camis, name, boro, inspection_date, violation_code,
ROW_NUMBER() OVER(
PARTITION BY camis, name, boro, inspection_date, violation_code camis | name | inspection_date | violation_code | score | ...
) - 1 AS duplicate --------+-----------------+-----------------+----------------+-------+-----
FROM
... | ... | ... | ... | ... | ...
restaurant_inspection;
50038736 | DON NICO'S | 03/29/2018 | 09B | 26 | ...
50038736 | DON NICO'S | 03/29/2018 | 09B | 18 | ...
camis | name | boro | inspection_date | violation_code | duplicate | ...
---------+--------------------------+-----------+-----------------+----------------+------------+----
50033304 | ASTORIA PIZZA | 12/18/2019 | 02B | 16 | ...
... | ... | ... | ... | ... | ... | ... 50081658 | IRVING FARMS | 12/13/2018 | 06F | 9 | ...
40961447 | MESON SEVILLA RESTAURANT | Manhattan | 03/19/2019 | 10F | 0 | ... 50033733 | ICHIBANTEI | 02/12/2019 | 10B | 12 | ...
41630358 | FAY DA BAKERY | Queens | 03/07/2019 | 06E | 0 | ...
... | ... | ... | ... | ... | ...
41630358 | FAY DA BAKERY | Queens | 03/07/2019 | 06E | 1 | ...
41630358 | FAY DA BAKERY | Queens | 03/07/2019 | 06E | 2 | ...
41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | 04L | 0 | ...
41659848 | LA BRISA DEL CIBAO | Queens | 01/30/2018 | 04L | 1 | ...
... | ... | ... | ... | ... | ... | ...
AVERAGE() MIN() MAX()
camis | name | inspection_date | violation_code | score | ...
---------+-----------------+-----------------+----------------+-------+-----
SELECT
camis, ... | ... | ... | ... | ... | ...
name,
50038736 | DON NICO'S | 03/29/2018 | 09B | 22.0 | ...
inspection_date,
violation_code, ... | ... | ... | ... | ... | ...
AVG(score) AS score
FROM
restaurant_inspection
GROUP BY
camis,
name,
inspection_date,
violation_code
HAVING
COUNT(*) > 1;
camis | name | inspection_date | score | inspection_type | ...
---------+---------------------------------+-----------------+-------+---------------------------------------+----- SELECT
... | ... | ... | ... | ... | ...
camis,
41659848 | LA BRISA DEL CIBAO | 01/30/2018 | 20 | Cycle Inspection / Initial Inspection | ...
40961447 | MESON SEVILLA RESTAURANT | 03/19/2019 | 50 | Cycle Inspection / Initial Inspection | ... name,
50063071 | WA BAR | 05/23/2018 | 15 | Cycle Inspection / Initial Inspection | ...
50034992 | EMPANADAS MONUMENTAL | 06/21/2019 | 17 | Cycle Inspection / Re-inspection | ... inspection_date,
50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/2020 | 10 | Cycle Inspection / Initial Inspection | ...
... | ... | ... | ... | ... | ...
score
FROM
camis | name | inspection_date | score | inspection_type | ...
restaurant_inspection
---------+------------------------------+-----------------+-------+-------------------------------------------------+-----
... | ... | ... | ... | ... | ... WHERE
41104041 | THE SPARROW TAVERN | 09/17/2019 | 13 | Cycle Inspection / Initial Inspection | ...
50016937 | BURGER KING | 09/14/2018 | 12 | Cycle Inspection / Re-inspection | ... score NOT SIMILAR TO '\d+';
50066469 | DARBAR'S CHICKEN & RIBS | 08/07/2017 | 11 | Pre-permit (Operational) / Reopening Inspection | ...
41195691 | F & J PINE RESTAURANT | 05/02/2019 | 26 | Cycle Inspection / Initial Inspection | ...
50015706 | EL RINCONCITO DE LOS SABORES | 12/18/2019 | A | Cycle Inspection / Initial Inspection | ...
... | ... | ... | ... | ... | ...

SELECT ALTER TABLE restaurant_inspection


camis,
ALTER COLUMN score TYPE SMALLINT USING score::smallint;
name,
inspection_date,
SMALLINT
score
FROM USING
restaurant_inspection
WHERE
score NOT SIMILAR TO '\d{1}' AND
score NOT SIMILAR TO '\d{2}' AND
score NOT SIMILAR TO '\d{3}';
ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;
\d \d\d\d

? x\d? SELECT
camis,
+ \d+
name,
* \d* inspection_date,
score
[] [a-z]
FROM
restaurant_inspection
WHERE
score < 0;

ALTER TABLE restaurant_inspection ALTER TABLE restaurant_inspection


ALTER COLUMN score TYPE SMALLINT USING score::smallint; ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT SELECT
camis, camis,
name, name,
inspection_date, inspection_date,
score score
FROM FROM
restaurant_inspection restaurant_inspection
WHERE WHERE
score <= -1; score < 0 OR
score > 100;
SELECT
ALTER TABLE restaurant_inspection
camis, name, inspection_date, score
ALTER COLUMN score TYPE SMALLINT USING score::smallint; FROM
restaurant_inspection
WHERE
SELECT
score NOT BETWEEN 0 AND 100;
camis,
name,
camis | name | inspection_date | score
inspection_date, ---------+-------------------------+-----------------+-------
score ... | ... | ... | ...
41702543 | TROPICAL GRILL | 05/14/2018 | 109
FROM
50074058 | PAD THAI | 08/01/2018 | 101
restaurant_inspection 50085349 | DON CHILE MEXICAN GRILL | 12/04/2018 | 124
WHERE 50092932 | ENERGY JUICE BAR | 06/24/2019 | 102
41702543 | TROPICAL GRILL | 05/14/2018 | 109
score < 0 OR
50034653 | KAI FAN ASIAN CUISINE | 12/06/2019 | -1
score >= 101; ... | ... | ... | ...
SELECT
camis,
score
grade,
camis | name | score | ... A grade_date,
---------+---------------------------------+--------+-----
... | ... | ... | ... score,
41659848 | LA BRISA DEL CIBAO | 20 | ...
40961447 | MESON SEVILLA RESTAURANT | 50 | ... inspection_type
50063071 | WA BAR | 15 | ...
... | ... | ... | ...
FROM
restaurant_inspection
A B C WHERE
grade = 'A' AND
score NOT BETWEEN 0 AND 13;
A

A B C 0

B SELECT
camis, grade, grade_date, score, inspection_type FROM
SELECT restaurant_inspection
camis, WHERE
grade, (grade = 'A' OR grade = 'B' OR grade = 'C') AND
grade_date, inspection_type LIKE '%Reopening%';
score,
inspection_type
camis | grade | grade_date | score | inspection_type | ...
FROM
---------+-------+------------+-------+-------------------------------------------------+-----
restaurant_inspection
... | ... | ... | ... | ... | ...
WHERE
50005784 | C | 05/29/2019 | 14 | Cycle Inspection / Reopening Inspection | ...
grade = 'B' AND
50091190 | C | 07/12/2019 | 7 | Pre-permit (Operational) / Reopening Inspection | ...
score NOT BETWEEN 14 AND 27;
40395023 | C | 09/13/2019 | 8 | Cycle Inspection / Reopening Inspection | ...
50037770 | C | 10/26/2018 | 11 | Cycle Inspection / Reopening Inspection | ...
camis | grade | grade_date | score | inspection_type 50036406 | C | 07/10/2018 | 20 | Cycle Inspection / Reopening Inspection | ...
---------+-------+------------+-------+---------------------------------- ... | ... | ... | ... | ... | ...
50034653 | B | 12/06/2019 | -1 | Cycle Inspection / Re-inspection
camis | name | score | inspection_type | ...
---------+---------------------------------+-------+---------------------------------------+-----
... | ... | ... | ... | ...
41659848 | LA BRISA DEL CIBAO | 20 | Cycle Inspection / Initial Inspection | ...
40961447 | MESON SEVILLA RESTAURANT | 50 | Cycle Inspection / Initial Inspection | ...
50063071 | WA BAR | 15 | Cycle Inspection / Initial Inspection | ...
50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 10 | Cycle Inspection / Initial Inspection | ...
41104041 | THE SPARROW TAVERN | 13 | Cycle Inspection / Initial Inspection | ...
... | ... | ... | ... | ...
SELECT column_name | data_type SELECT
column_name, -----------------+----------- column_name,
data_type camis | bigint data_type
FROM name | text FROM

information_schema.columns boro | text information_schema.columns

WHERE building | text WHERE


table_name = 'restaurant_inspection' AND
table_name = 'restaurant_inspection'; street | text
column_name = 'camis';
zip_code | smallint
... | ...
column_name | data_type
------------+-----------
camis | bigint

CASE SELECT SELECT boro | avg


boro, boro, --------------+--------------------
A B C
AVG(grade_points) AVG(grade_points) Brooklyn | 2.7641196013289037

A B C FROM ( FROM ( Bronx | 2.7685589519650655


SELECT SELECT Manhattan | 2.7678381256656017
*, *, Queens | 2.7803571428571429
CASE CASE Staten Island | 2.8068181818181818
WHEN grade = 'A' then 3 WHEN grade = 'A' then 3
WHEN grade = 'B' then 2 WHEN grade = 'B' then 2
WHEN grade = 'C' then 1 WHEN grade = 'C' then 1
END AS grade_points END AS grade_points
FROM FROM
restaurant_inspection restaurant_inspection
) sub ) sub
GROUP BY boro; GROUP BY boro;
camis | diff SELECT CAST( value AS type)
---------+------ camis,
... | ... MAX(score) - MIN(score) AS diff SELECT
50080214 | 87 FROM camis,
50059239 | 74 restaurant_inspection MAX(CAST(score AS int)) - MIN(CAST(score AS int)) AS diff
50086316 | 74 WHERE FROM
41637438 | 71 score IS NOT NULL restaurant_inspection
41667902 | 64 GROUP BY WHERE
50067622 | 61 camis score IS NOT NULL
50017111 | 60 ORDER BY GROUP BY
50017056 | 60 diff DESC; camis
50045240 | 59 ORDER BY
50002403 | 59 diff DESC
... | ...

value::type

SELECT
camis,
MAX(score::int) - MIN(score::int) AS diff
FROM
restaurant_inspection
WHERE
score IS NOT NULL
GROUP BY
camis
ORDER BY
diff DESC
camis | name | inspection_date | grade_date | ... DATE TEXT
---------+----------------------+-----------------+------------+----
... | ... | ... | ... | ...
50034992 | EMPANADAS MONUMENTAL | 06/21/2019 | 06/21/2019 | ...
50095871 | ALPHONSO'S PIZZERIA | 01/16/2020 | 01/16/2020 | ...
41104041 | THE SPARROW TAVERN | 09/17/2019 | 09/17/2019 | ...
50016937 | BURGER KING | 09/14/2018 | 09/14/2018 | ...
50033304 | ASTORIA PIZZA | 12/18/2019 | 12/18/2019 | ...
... | ... | ... | ... | ...

DATE(string_date)
string_date DATE

DATE('2019-12-01') DATE

SELECT TO_DATE(date_string, format_string) DATE


camis,
name, DATE('Wednesday, June 10th, 2014')
DATE(inspection_date) AS inspection_date,
DATE(grade_date) AS grade_date TO_DATE('Wednesday, June 10th, 2014', 'Day, Month DDth, YYYY') DATE
FROM
restaurant_inspection;

camis | name | inspection_date | grade_date | ...


---------+---------------------------------+-----------------+------------+-----
... | ... | ... | ... | ...
50034992 | EMPANADAS MONUMENTAL | 2019-06-21 | 2019-06-21 | ...
50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 2020-01-16 | 2020-01-16 | ...
41104041 | THE SPARROW TAVERN | 2019-09-17 | 2019-09-17 | ...
50016937 | BURGER KING | 2018-09-14 | 2018-09-14 | ...
50033304 | ASTORIA PIZZA | 2019-12-18 | 2019-12-18 | ...
... | ... | ... | ... | ...
camis | name | inspection_date | grade_date | ... SELECT

--------+----------------------------------+-----------------+------------+----- camis,
name,
... | ... | ... | ... | ...
TO_CHAR(
41659848 | LA BRISA DEL CIBAO | 2018-01-30 | - | ...
TO_CHAR('2012-04-03', YYYY-DD-MM) inspection_date::date,
40961447 | MESON SEVILLA RESTAURANT | 2019-03-19 | - | ...
'MM/DD/YY'
50063071 | WA BAR | 2018-05-23 | - | ... ) AS inspection_date
TO_CHAR(date_value, format_string)
50034992 | EMPANADAS MONUMENTAL | 2019-06-21 | 2019-06-21 | ... FROM
50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 2020-01-16 | 2020-01-16 | ... restaurant_inspection;

... | ... | ... | ... | ...

camis | name | inspection_date | ... camis | name | inspection_date | ...


---------+---------------------------------+-----------------+----- ---------+---------------------------------+-----------------+-----
NULLIF(value1, value2) ... | ... | ... | ... ... | ... | ... | ...
41659848 | LA BRISA DEL CIBAO | 01/30/2018 | ... 41659848 | LA BRISA DEL CIBAO | 01/30/20 | ...
40961447 | MESON SEVILLA RESTAURANT | 03/19/2019 | ... 40961447 | MESON SEVILLA RESTAURANT | 03/19/20 | ...
SELECT
50063071 | WA BAR | 05/23/2018 | ... 50063071 | WA BAR | 05/23/20 | ...
NULLIF(grade_date, '-') 50034992 | EMPANADAS MONUMENTAL | 06/21/2019 | ... 50034992 | EMPANADAS MONUMENTAL | 06/21/20 | ...
FROM 50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/2020 | ... 50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/20 | ...
restaurant_inspection; ... | ... | ... | ... ... | ... | ... | ...

TO_DATE(date_string, format) YYYY MM

TO_CHAR(date_value, format)
TO_DATE('2012', 'YYYY') TO_DATE('09/2012', 'MM/YYYY')

DATE DATE

DD Day

TO_DATE('09/03/2012', 'MM/DD/YYYY') TO_DATE('Sunday, the 10th', 'Day, the DDth')

DATE DATE
YYYY MM

TO_CHAR('2012-09-03'::date, 'YYYY') TO_CHAR('09/03/2012'::date, 'MM/YYYY')

2012 09/2012

DD Day

TO_CHAR('09/03/2012'::date, 'MM/DD/YYYY') TO_CHAR('09/03/2012'::date, 'Day, the DDth')

09/03/2012 Monday, the 03rd

camis | name | inspection_datetime | inspection_type | ...


---------+-----------------------+----------------------+---------------------------------------+-----
... | ... | ... | ... | ...
50000458 | BEVERLEY PIZZA & CAFE | 2019-07-08 14:26 | Cycle Inspection / Initial Inspection | ...
50002521 | JADE PALACE | 2018-05-14 12:35 | Cycle Inspection / Initial Inspection | ...
40389732 | GIANDO | 2017-07-10 13:39 | Cycle Inspection / Re-inspection | ...
50044246 | FLEET BAKERY | 2019-10-29 15:40 | Cycle Inspection / Re-inspection | ...
50038120 | SHUN WON FLUSHING | 2018-07-17 16:20 | Cycle Inspection / Re-inspection | ...
... | ... | ... | ... | ...

inspection_datetime TIMESTAMP
TIMESTAMP TO_TIMESTAMP(ts_string, format)

TO_TIMESTAMP(ts_string, format_string) TIMESTAMP TO_CHAR(ts_value, format)

TO_DATE() YYYY MM Day


SELECT
camis,
name,
TO_TIMESTAMP(inspection_datetime, 'YYYY-MM-DD HH24:MI'),
inspection_type
FROM
restaurant_inspection;

TO_TIMESTAMP(ts_string, format) EXTRACT(time_unit FROM time_value)

time_value DATE TIMESTAMP


HH24 TO_TIMESTAMP('23', 'HH24') TIMESTAMP

HH12 TO_TIMESTAMP('01', 'HH12') TIMESTAMP

MI TO_TIMESTAMP('18:13', 'HH24:MI') TIMESTAMP

SS TO_TIMESTAMP('33:20', 'MI:SS') TIMESTAMP

PM AM TO_TIMESTAMP('5:35AM', 'HH12:MIPM') TIMESTAMP


SELECT
camis,
name,
year EXTRACT('year' FROM '2020-07-20 16:42:21'::timestamp)
inspection_datetime,
EXTRACT('year' FROM inspection_datetime) AS year,
inspection_type
month EXTRACT('month' FROM '2020-07-20 16:42:21'::timestamp)
FROM
restaurant_inspection; day EXTRACT('day' FROM '2020-07-20 16:42:21'::timestamp)

camis | name | inspection_datetime | year | inspection_type | ...


hour EXTRACT('hour' FROM '2020-07-20 16:42:21'::timestamp)
---------+-----------------------+----------------------------+------+---------------------------------------+-----
... | ... | ... | ... | ... | ... minute EXTRACT('minute' FROM '2020-07-20 16:42:21'::timestamp)
50000458 | BEVERLEY PIZZA & CAFE | 2019-07-08 06:37:46.658905 | 2019 | Cycle Inspection / Initial Inspection | ...
50002521 | JADE PALACE | 2018-05-14 03:47:24.474573 | 2018 | Cycle Inspection / Initial Inspection | ... second EXTRACT('second' FROM '2020-07-20 16:42:21'::timestamp)
40389732 | GIANDO | 2017-07-10 03:59:12.864428 | 2017 | Cycle Inspection / Re-inspection | ...
50044246 | FLEET BAKERY | 2019-10-29 02:06:33.614964 | 2019 | Cycle Inspection / Re-inspection | ...
50038120 | SHUN WON FLUSHING | 2018-07-17 01:15:04.15666 | 2018 | Cycle Inspection / Re-inspection | ...
... | ... | ... | ... | ... | ...
CONCAT(string1 [, string2, string3, ...])

name | boro | building | street | zip_code | ... CONCAT('data', 'cleaning', 'is', 'fun') datacleaningisfun
-----------------------------+--------+----------+------------------+----------+-----
... | ... | ... | ... | ... | ... CONCAT('data', ' ', 'cleaning', ' ', 'is', ' ', 'fun') data cleaning is fun
DARBAR'S CHICKEN & RIBS | Queens | 12609 | LIBERTY AVE | 11419 | ...
F & J PINE RESTAURANT | Bronx | 1913 | BRONXDALE AVENUE | 10462 | ...
EL RINCONCITO DE LOS SABORES | Queens | 13933 | 89TH AVE | 11435 | ...
DON NICO'S | Queens | 9014 | 161ST ST | 11432 | ...
ASTORIA PIZZA | Queens | 3204B | 30TH AVE | 11102 | ...
... | ... | ... | ... | ... | ...

Restaurant Name
Street Address
Boro, NY Zipcode

SELECT mailing_address name | boro | building | street | zip_code | ...


CONCAT( ---------------------------
-----------------------+-----------+----------+---------------------------+----------+-----
name, E'\n', DARBAR'S CHICKEN & RIBS +
building, ' ', street, E'\n', 12609 LIBERTY AVE + ... | ... | ... | ... | ... | ...
boro, ', NY ', zip_code Queens, NY 11419 IRVING FARMS | Queens | | CENTRAL TERMINAL BUILDING | 11371 | ...
) AS mailing_address F & J PINE RESTAURANT + DON PEPIS DELICATESSEN | Manhattan | | AMTRAK LEVEL | 10001 | ...
FROM 1913 BRONXDALE AVENUE +
DUNKIN' | Queens | | CENTRAL TERMINAL BLDG | 11371 | ...
restaurant_inspection; Bronx, NY 10462
EL RINCONCITO DE LOS SABORES+ | Queens | 17111 | JAMAICA AVE | 11432 | ...
13933 89TH AVE + | Brooklyn | 1489 | FULTON STREET | 11216 | ...
Queens, NY 11435 ... | ... | ... | ... | ... | ...
DON NICO'S +
9014 161ST ST +
Queens, NY 11432
ASTORIA PIZZA +
3204B 30TH AVE +
Queens, NY 11102
SELECT mailing_address string1 || string2 [ || string3 || ...]
CONCAT( ---------------------------
name, E'\n', IRVING FARMS + SELECT 'data' || ' ' || 'cleaning' || ' ' || 'is' || ' ' || 'fun';
building, ' ', street, E'\n', CENTRAL TERMINAL BUILDING+
boro, ', NY ', zip_code Queens, NY 11371
) AS mailing_address DON PEPIS DELICATESSEN + data cleaning is fun
FROM AMTRAK LEVEL +
restaurant_inspection; Manhattan, NY 10001
DUNKIN' +
NULL NULL
CENTRAL TERMINAL BLDG +
Queens, NY 11371
SELECT
+
name || E'\n' ||
17111 JAMAICA AVE +
Queens, NY 11432
building || ' ' || street || E'\n'
+ || boro || ', NY ' || zip_code AS mailing_address
1489 FULTON STREET + FROM
Brooklyn, NY 11216 restaurant_inspection

name | mailing_address
-----------------+-----------------------
SCHNIPPERS | SCHNIPPERS +
| 570 LEXINGTON AVENUE +
| Manhattan, NY 10022
ATOMIC WINGS |
WING LING | WING LING +
| 159B EAST 170 STREET+
| Bronx, NY 10452
JUAN VALDEZ CAFE | JUAN VALDEZ CAFE +
| 140 EAST 57 STREET +
| Manhattan, NY 10022
FULTON GRAND | FULTON GRAND +
| 1011 FULTON STREET +
| Brooklyn, NY 11238
camis | inspection_date | violation | ...
---------+-----------------+-------------------------------------------------+-----
... | ... | ... | ...
50038736 | 03/29/2018 | 09B Thawing procedures | ...
50033304 | 12/18/2019 | 02B Hot food item not held at or above 140º ... | ...
50081658 | 12/13/2018 | 06F Wiping cloths soiled or not stored in sa... | ...
50033733 | 02/12/2019 | 10B Plumbing not properly installed or maint... | ...
40559634 | 08/22/2017 | 04N Filth flies or food/refuse/sewage-associ... | ...
... | ... | ... | ...

STRPOS(source_string, search_string)

SELECT
STRPOS('09B Thawing procedures', ' '); SELECT
STRPOS('09B Thawing procedures', '?');
4

0
SUBSTRING(source_string FROM start_pos FOR num_chars)

SELECT
STRPOS('09B Thawing procedures', ' ');

SUBSTRING('Homerun' FROM 1 FOR 4) Home

SELECT
SUBSTRING(
'09B Thawing procedures'
FROM 1
FOR STRPOS('09B Thawing procedures', ' ') - 1
); SELECT
STRPOS('09B Thawing procedures', ' ') + 1;

09B
5
LENGTH('09B Thawing procedures') 22

STRPOS('09B Thawing procedures', ' ') 4

LENGTH('09B Thawing procedures') - STRPOS('09B Thawing procedures', ' ') 18

LENGTH('Thawing procedures') 18

LENGTH(string) INTEGER

SELECT LENGTH('hello')

SELECT SELECT
LENGTH('09B Thawing procedures') - SUBSTRING(

STRPOS('09B Thawing procedures', ' '); '09B Thawing procedures'


FROM
STRPOS('09B Thawing procedures', ' ')
18
+ 1
FOR
LENGTH('09B Thawing procedures')
- STRPOS('09B Thawing procedures', ' ')
);

Thawing procedures
SELECT
camis | inspection_date | violation | ...
camis,
inspection_date,
---------+-----------------+-------------------------------------------------+-----
... | ... | ... | ...
SUBSTRING( 50038736 | 03/29/2018 | 09B Thawing procedures | ...
violation
50033304 | 12/18/2019 | 02B Hot food item not held at or above 140º ... | ...
FROM 1
FOR STRPOS(violation, ' ') - 1 50081658 | 12/13/2018 | 06F Wiping cloths soiled or not stored in sa... | ...
) AS violation_code, 50033733 | 02/12/2019 | 10B Plumbing not properly installed or maint... | ...
40559634 | 08/22/2017 | 04N Filth flies or food/refuse/sewage-associ... | ...
SUBSTRING(
violation ... | ... | ... | ...
FROM STRPOS(violation, ' ') + 1
FOR LENGTH(violation) - STRPOS(violation, ' ')
) AS violation_description
FROM
restaurant_inspection;

camis | inspection_date | violation_code | violation_description | ...


---------+-----------------+----------------+---------------------------------------------+-----
... | ... | ... | ... | ...
50038736 | 03/29/2018 | 09B | Thawing procedures | ...
50033304 | 12/18/2019 | 02B | Hot food item not held at or above 140º ... | ...
50081658 | 12/13/2018 | 06F | Wiping cloths soiled or not stored in sa... | ...
50033733 | 02/12/2019 | 10B | Plumbing not properly installed or maint... | ...
40559634 | 08/22/2017 | 04N | Filth flies or food/refuse/sewage-associ... | ...
... | ... | ... | ... | ...
camis | name | inspection_type | ...
---------+-----------------------+----------------------------------------------+-----
... | ... | ... | ...
50084922 | JUICE POINT | Cycle Inspection / Re-inspection | ...
50075375 | ATOMIC WINGS | Administrative Miscellaneous / Re-inspection | ...
50048685 | KENNEDY FRIED CHICKEN | Cycle Inspection / Re-inspection | ...
50058910 | HUNGER PANG | Pre-permit (Operational) / Re-inspection | ...
50047834 | SUBWAY | Smoke-Free Air Act / Re-inspection | ...
... | ... | ... | ...

' / '

sub_inspection_type | count
-----------------------------+-------
Reopening Inspection | 56
Re-inspection | 1333
Initial Inspection | 3488
Second Compliance Inspection | 2
Compliance Inspection | 27

camis | name | inspection_type | ... SPLIT_PART(source_string, delimiter_string, part_number)


---------+-----------------------+----------------------------------------------+-----
... | ... | ... | ...
50084922 | JUICE POINT | Cycle Inspection / Re-inspection | ... SELECT
50075375 | ATOMIC WINGS | Administrative Miscellaneous / Re-inspection | ...
50048685 | KENNEDY FRIED CHICKEN | Cycle Inspection / Re-inspection | ... SPLIT_PART('Cycle Inspection / Re-inspection', ' / ', 1);
50058910 | HUNGER PANG | Pre-permit (Operational) / Re-inspection | ...
50047834 | SUBWAY | Smoke-Free Air Act / Re-inspection | ...
... | ... | ... | ...
Cycle Inspection

camis | name | main_inspection_type | sub_inspection_type | ...


---------+-----------------------+------------------------------+---------------------+-----
... | ... | ... | ... | ...
SELECT
50084922 | JUICE POINT | Cycle Inspection | Re-inspection | ...
SPLIT_PART('Cycle Inspection / Re-inspection', ' / ', 2);
50075375 | ATOMIC WINGS | Administrative Miscellaneous | Re-inspection | ...
50048685 | KENNEDY FRIED CHICKEN | Cycle Inspection | Re-inspection | ...
50058910 | HUNGER PANG | Pre-permit (Operational) | Re-inspection | ...
50047834 | SUBWAY | Smoke-Free Air Act | Re-inspection | ... Re-inspection
... | ... | ... | ... | ...
camis | name | cuisine_description | ...
SELECT
---------+-----------------------+---------------------+-----
camis,
... | ... | ... | ...
name, 50066768 | FIRST LAMB SHABU | Chinese | ...
SPLIT_PART(inspection_type, ' / ', 1) AS main_inspection_type, 41450971 | GIOVANNI'S RESTAURANT | Pizza/Italian | ...

SPLIT_PART(inspection_type, ' / ', 2) AS sub_inspection_type 41628459 | KFC | Chicken | ...


50043003 | BANGIA | Korean | ...
FROM
41418978 | BAGEL EXPRESS III | Bagels/Pretzels | ...
restaurant_inspection; ... | ... | ... | ...

camis | name | main_inspection_type | sub_inspection_type | ... camis | name | cuisine_description | ...


-------+-----------------------+---------------------+-----
---------+-----------------------+------------------------------+---------------------+-----
... | ... | ... | ...
... | ... | ... | ... | ...
50066768 | FIRST LAMB SHABU | Chinese | ...
50084922 | JUICE POINT | Cycle Inspection | Re-inspection | ... 41450971 | GIOVANNI'S RESTAURANT | Pizza | ...
50075375 | ATOMIC WINGS | Administrative Miscellaneous | Re-inspection | ... 41450971 | GIOVANNI'S RESTAURANT | Italian | ...

50048685 | KENNEDY FRIED CHICKEN | Cycle Inspection | Re-inspection | ... 41628459 | KFC | Chicken | ...
50043003 | BANGIA | Korean | ...
50058910 | HUNGER PANG | Pre-permit (Operational) | Re-inspection | ...
41418978 | BAGEL EXPRESS III | Bagels | ...
50047834 | SUBWAY | Smoke-Free Air Act | Re-inspection | ... 41418978 | BAGEL EXPRESS III | Pretzels | ...
... | ... | ... | ... | ... ... | ... | ... | ...

REGEXP_SPLIT_TO_TABLE(source, pattern) SELECT


camis,
name,
SELECT REGEXP_SPLIT_TO_TABLE('Pizza/Italian', '/'); REGEXP_SPLIT_TO_TABLE(cuisine_description, '/') AS cuisine_description,
...
FROM
Pizza
restaurant_inspection;
Italian

camis | name | cuisine_description | ...


---------+-----------------------+---------------------+-----
... | ... | ... | ...
50066768 | FIRST LAMB SHABU | Chinese | ...
41450971 | GIOVANNI'S RESTAURANT | Pizza | ...
41450971 | GIOVANNI'S RESTAURANT | Italian | ...
41628459 | KFC | Chicken | ...
50043003 | BANGIA | Korean | ...
41418978 | BAGEL EXPRESS III | Bagels | ...
41418978 | BAGEL EXPRESS III | Pretzels | ...
... | ... | ... | ...
SELECT cuisine_num | camis | name | cuisine_description | ...
cuisine_num | camis | name | cuisine_description | ...
ROW_NUMBER() OVER ( ------------+----------+-----------------------+---------------------+-----
------------+----------+-----------------------+---------------------+----- PARTITION BY ... | ... | ... | ... | ...
... | ... | ... | ... | ... -- group columns for numbering 1 | 41418978 | BAGEL EXPRESS III | Bagels | ...
camis, 2 | 41418978 | BAGEL EXPRESS III | Pretzels | ...
1 | 41418978 | BAGEL EXPRESS III | Bagels | ...
name 1 | 41450971 | GIOVANNI'S RESTAURANT | Pizza | ...
2 | 41418978 | BAGEL EXPRESS III | Pretzels | ... ORDER BY 2 | 41450971 | GIOVANNI'S RESTAURANT | Italian | ...
1 | 41450971 | GIOVANNI'S RESTAURANT | Pizza | ... -- set ordering of results 1 | 41628459 | KFC | Chicken | ...
camis, 1 | 50043003 | BANGIA | Korean | ...
2 | 41450971 | GIOVANNI'S RESTAURANT | Italian | ...
name 1 | 50066768 | FIRST LAMB SHABU | Chinese | ...
1 | 41628459 | KFC | Chicken | ... ) AS cuisine_num, ... | ... | ... | ... | ...
1 | 50043003 | BANGIA | Korean | ... *
FROM (
1 | 50066768 | FIRST LAMB SHABU | Chinese | ...
SELECT
... | ... | ... | ... | ... camis,
name,
REGEXP_SPLIT_TO_TABLE(cuisine_description, '/')
ROW_NUMBER() OVER() AS cuisine_description
FROM
restaurant_inspection;

PARTITION BY col1, col2, ...

ORDER BY colA, colB, ...


name | inspection_type | grade | ... SELECT
--------------------------------+---------------------------------------+-------+----- inspection_type,

... | ... | ... | ... grade,


COUNT(*)
EMPANADAS MONUMENTAL | Cycle Inspection / Re-inspection | B | ...
FROM
ALPHONSO'S PIZZERIA & TRATTORIA | Cycle Inspection / Initial Inspection | A | ...
restaurant_inspection
THE SPARROW TAVERN | Cycle Inspection / Initial Inspection | A | ...
WHERE
BURGER KING | Cycle Inspection / Re-inspection | A | ... grade IS NOT NULL
ASTORIA PIZZA | Cycle Inspection / Re-inspection | B | ... GROUP BY
... | ... | ... | ... inspection_type,
grade
ORDER BY
inspection_type,
grade;

inspection_type | grade | count


--------------------------------------------------+-------+-------
inspection_type | A | B | C | N | P | Z
Cycle Inspection / Initial Inspection | A | 1063 --------------------------------------------------+------+-----+----+----+----+----
Cycle Inspection / Re-inspection | A | 723
Cycle Inspection / Re-inspection | B | 270 Cycle Inspection / Re-inspection | 723 | 270 | 93 | 0 | 0 | 29
Cycle Inspection / Re-inspection | C | 93
Cycle Inspection / Re-inspection | Z | 29
Cycle Inspection / Initial Inspection | 1063 | 0 | 0 | 0 | 0 | 0
Cycle Inspection / Reopening Inspection | C | 8 Pre-permit (Operational) / Reopening Inspection | 0 | 0 | 3 | 0 | 3 | 1
Cycle Inspection / Reopening Inspection | P | 26
Cycle Inspection / Reopening Inspection | Z | 3 Cycle Inspection / Reopening Inspection | 0 | 0 | 8 | 0 | 26 | 3
Pre-permit (Non-operational) / Initial Inspection | N | 4
Pre-permit (Non-operational) / Initial Inspection | 0 | 0 | 0 | 4 | 0 | 0
Pre-permit (Operational) / Initial Inspection | A | 119
Pre-permit (Operational) / Initial Inspection | N | 17 Pre-permit (Operational) / Initial Inspection | 119 | 0 | 0 | 17 | 0 | 0
Pre-permit (Operational) / Re-inspection | A | 79
Pre-permit (Operational) / Re-inspection | B | 49 Pre-permit (Operational) / Re-inspection | 79 | 49 | 13 | 0 | 0 | 9
Pre-permit (Operational) / Re-inspection | C | 13
Pre-permit (Operational) / Re-inspection | Z | 9
Pre-permit (Operational) / Reopening Inspection | C | 3
Pre-permit (Operational) / Reopening Inspection | P | 3
Pre-permit (Operational) / Reopening Inspection | Z | 1
AVG(qty_sold) FILTER (WHERE qty_sold > 1)

WHERE AGG_FUNC(expression) FILTER (WHERE condition)


AGG_FUNC()
SELECT

SELECT
summary_column,
AGG(agg_column) FILTER (WHERE agg_column = PIVOT_VALUE_1) AS "pivot_column_1",
AGG(agg_column) FILTER (WHERE agg_column = PIVOT_VALUE_2) AS "pivot_column_2",
...
AGG(agg_column) FILTER (WHERE agg_column = PIVOT_VALUE_N) AS "pivot_column_N"
FROM
source_table
GROUP BY
summary_column;
SELECT inspection_type | A | B | C | N | P | Z
inspection_type, --------------------------------------------------+------+-----+----+----+----+----
COUNT(grade) FILTER (WHERE grade = 'A') AS "A",
Cycle Inspection / Re-inspection | 723 | 270 | 93 | 0 | 0 | 29
COUNT(grade) FILTER (WHERE grade = 'B') AS "B",
Cycle Inspection / Initial Inspection | 1063 | 0 | 0 | 0 | 0 | 0
COUNT(grade) FILTER (WHERE grade = 'C') AS "C",
COUNT(grade) FILTER (WHERE grade = 'N') AS "N", Pre-permit (Operational) / Reopening Inspection | 0 | 0 | 3 | 0 | 3 | 1
COUNT(grade) FILTER (WHERE grade = 'P') AS "P", Cycle Inspection / Reopening Inspection | 0 | 0 | 8 | 0 | 26 | 3
COUNT(grade) FILTER (WHERE grade = 'Z') AS "Z" Pre-permit (Non-operational) / Initial Inspection | 0 | 0 | 0 | 4 | 0 | 0
FROM Pre-permit (Operational) / Initial Inspection | 119 | 0 | 0 | 17 | 0 | 0
restaurant_inspections
Pre-permit (Operational) / Re-inspection | 79 | 49 | 13 | 0 | 0 | 9
WHERE
grade IS NOT NULL
GROUP BY
inspection_type;

You might also like