Difference between revisions of "Fraud Book"

From Bitnami MediaWiki
Jump to navigation Jump to search
(Created page with ";Style Guides * [https://www.sqlstyle.guide/ SQL Style Guide] * [https://about.gitlab.com/handbook/business-technology/data-team/platform/sql-style-guide/ GitLab guide] * [htt...")
 
 
(7 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
* [https://about.gitlab.com/handbook/business-technology/data-team/platform/sql-style-guide/ GitLab guide]
 
* [https://about.gitlab.com/handbook/business-technology/data-team/platform/sql-style-guide/ GitLab guide]
 
* [https://docs.telemetry.mozilla.org/concepts/sql_style.html Mozilla (Firefox) documentation]
 
* [https://docs.telemetry.mozilla.org/concepts/sql_style.html Mozilla (Firefox) documentation]
 +
* [https://learnsql.com/blog/24-rules-sql-code-formatting-standard/ learnsql.com]
 +
* [https://s3.amazonaws.com/codecademy-content/programs/learn-sql-from-scratch/SQL+Style+Guide+for+Learn+SQL+from+Scratch.pdf AWS SQL standard]
 +
* [https://gist.github.com/mattmc3/38a85e6a4ca1093816c08d4815fbebfb "Modern" SQL from github]
 +
* [https://www.dpriver.com/pp/sqlformat.htm The Holy Grail] (SQL formatter)
 +
 +
p. 136
 +
 +
===The SQLizer===
 +
<font face="Courier New" size="2">
 +
<font color = "blue">WITH</font>&nbsp;<font color = "maroon">table_of_how_many_times_each_combination_of_zip_and_city_appeared</font>
 +
<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color = "blue">AS</font>&nbsp;<font color = "maroon">(</font><font color = "blue">SELECT</font>&nbsp;<font color = "maroon">zipcode</font><font color = "silver">,</font><font color = "maroon">city</font><font color = "silver">,</font><font color = "fuchsia"><i>Count</i></font><font color = "maroon">(</font><font color = "silver">*</font><font color = "maroon">)</font>&nbsp;<font color = "blue">AS</font>&nbsp;<font color = "maroon">count_orders</font>
 +
<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color = "blue">FROM</font>&nbsp;&nbsp;&nbsp;<font color = "maroon">shipping_addresses</font>
 +
<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color = "blue">GROUP</font>&nbsp;&nbsp;<font color = "blue">BY</font>&nbsp;<font color = "black">1</font><font color = "silver">,</font><font color = "black">2</font><font color = "maroon">)</font>
 +
<br/><font color = "blue">SELECT</font>&nbsp;<font color = "maroon">city</font><font color = "silver">,</font><font color = "#FF0080"><b>Percent_rank</b></font><font color = "maroon">(</font><font color = "maroon">)</font>
 +
<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color = "blue">OVER</font>&nbsp;<font color = "maroon">(</font>
 +
<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color = "maroon">partition</font>&nbsp;<font color = "blue">BY</font>&nbsp;<font color = "maroon">zipcode</font>
 +
<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color = "blue">ORDER</font>&nbsp;<font color = "blue">BY</font>&nbsp;<font color = "maroon">count_orders</font><font color = "maroon">)</font>&nbsp;<font color = "blue">AS</font>&nbsp;<font color = "maroon">PctRank</font>
 +
<br/><font color = "blue">ORDER</font>&nbsp;&nbsp;<font color = "blue">BY</font>&nbsp;<font color = "maroon">pctrank</font>&nbsp;<font color = "blue">ASC</font><font color = "silver">;</font>&nbsp;
 +
</font>
 +
 +
===Using wiki's built-in syntax highlighting===
 +
<syntaxhighlight lang='SQL'>
 +
WITH table_of_how_many_times_each_combination_of_zip_and_city_appeared
 +
  AS (SELECT zipcode, city, count(*) AS count_orders
 +
      FROM shipping_addresses
 +
      GROUP BY 1,2)
 +
SELECT city, Percent_rank()
 +
  OVER ( partition BY zipcode ORDER BY count_orders)
 +
  AS PctRank
 +
ORDER BY pctrank ASC;
 +
</syntaxhighlight> 
 +
 +
 +
 +
<syntaxhighlight lang='SQL'>
 +
SELECT c.fname, c.lname, a.address
 +
FROM customer c, address a
 +
WHERE c.address_id = a.address_id
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight lang='SQL'>
 +
SELECT product, AVG(price), MIN(price), MAX(price),
 +
    MAX(price) - MIN(price) AS price_change
 +
FROM store_prices_log
 +
WHERE DayID BETWEEN {min_day} AND {max_day}
 +
GROUP BY 1
 +
ORDER BY price_change DESC;
 +
</syntaxhighlight>

Latest revision as of 03:26, 11 March 2022

Style Guides

p. 136

The SQLizer[edit]

WITH table_of_how_many_times_each_combination_of_zip_and_city_appeared
     AS (SELECT zipcode,city,Count(*) AS count_orders
         FROM   shipping_addresses
         GROUP  BY 1,2)
SELECT city,Percent_rank()
              OVER (
                partition BY zipcode
                ORDER BY count_orders) AS PctRank
ORDER  BY pctrank ASC; 

Using wiki's built-in syntax highlighting[edit]

WITH table_of_how_many_times_each_combination_of_zip_and_city_appeared
   AS (SELECT zipcode, city, count(*) AS count_orders
      FROM shipping_addresses
      GROUP BY 1,2)
SELECT city, Percent_rank()
   OVER ( partition BY zipcode ORDER BY count_orders)
   AS PctRank
ORDER BY pctrank ASC;


SELECT c.fname, c.lname, a.address
FROM customer c, address a
WHERE c.address_id = a.address_id
 
SELECT product, AVG(price), MIN(price), MAX(price), 
    MAX(price) - MIN(price) AS price_change
FROM store_prices_log
WHERE DayID BETWEEN {min_day} AND {max_day}
GROUP BY 1
ORDER BY price_change DESC;