Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lambda committed Jan 24, 2024
2 parents 7ad42bc + 9ec6281 commit 1072443
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 23 deletions.
2 changes: 1 addition & 1 deletion _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ format:
- slate
- dark.css
code-link: true
code-fold: true
#code-fold: true
#mainfont: verdana
fontsize: 1.25rem
monofont: Fira Code
Expand Down
1 change: 1 addition & 0 deletions docs/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
www.chapelier-fou.fr
4 changes: 2 additions & 2 deletions docs/animate_plot.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ <h1 class="title">Animate plots</h1>

</header><section id="exemple-danimation" class="level3"><h3 class="anchored" data-anchor-id="exemple-danimation">1. Exemple d’animation</h3>
<div class="cell">
<details><summary>Code</summary><div class="sourceCode" id="cb1"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu">ggplot</span><span class="op">(</span><span class="va">mpg</span><span class="op">)</span> <span class="op">+</span></span>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu">ggplot</span><span class="op">(</span><span class="va">mpg</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu">geom_bar</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu">labs</span><span class="op">(</span>title <span class="op">=</span> <span class="st">'Number of cars in {closest_state} by number of cylinders'</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu">transition_states</span><span class="op">(</span>states <span class="op">=</span> <span class="va">year</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu">enter_grow</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu">exit_fade</span><span class="op">(</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details><div class="cell-output-display">
<div class="cell-output-display">
<p><img src="animate_plot_files/figure-html/geombar-animate-1.gif" class="img-fluid"></p>
</div>
</div>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ <h1 class="title">The chance</h1>
<div class="about-contents"><div id="ppc">
<p>Is your world choosing the same numbers as mine ?</p>
<div class="cell">
<details><summary>Code</summary><div class="sourceCode" id="cb1"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">roll</span> <span class="op">&lt;-</span> </span>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">roll</span> <span class="op">&lt;-</span> </span>
<span> <span class="fu"><a href="https://rdrr.io/r/base/sample.html">sample.int</a></span><span class="op">(</span>n <span class="op">=</span> <span class="fl">6</span>, size <span class="op">=</span> <span class="fl">42</span>, replace <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/table.html">table</a></span><span class="op">(</span><span class="va">roll</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details><div class="cell-output cell-output-stdout">
<div class="cell-output cell-output-stdout">
<pre><code>roll
1 2 3 4 5 6
9 7 12 3 8 3 </code></pre>
6 13 12 5 3 3 </code></pre>
</div>
</div>
<p>How many trials before getting an identical world ?</p>
Expand Down
41 changes: 31 additions & 10 deletions docs/search.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/site_libs/bootstrap/bootstrap-dark.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/site_libs/bootstrap/bootstrap.min.css

Large diffs are not rendered by default.

129 changes: 125 additions & 4 deletions docs/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>

<script src="site_libs/quarto-nav/quarto-nav.js"></script>
Expand Down Expand Up @@ -105,7 +139,9 @@
<nav id="TOC" role="doc-toc" class="toc-active"><h2 id="toc-title">On this page</h2>

<ul>
<li><a href="#postgresql-interesting-commands" id="toc-postgresql-interesting-commands" class="nav-link active" data-scroll-target="#postgresql-interesting-commands">1. PostgreSQL interesting commands</a></li>
<li><a href="#comment-supprimer-les-doublons-dune-table" id="toc-comment-supprimer-les-doublons-dune-table" class="nav-link active" data-scroll-target="#comment-supprimer-les-doublons-dune-table">1. Comment supprimer les doublons d’une table</a></li>
<li><a href="#postgresql-interesting-commands" id="toc-postgresql-interesting-commands" class="nav-link" data-scroll-target="#postgresql-interesting-commands">2. PostgreSQL interesting commands</a></li>
<li><a href="#section" id="toc-section" class="nav-link" data-scroll-target="#section"></a></li>
</ul></nav>
</div>
<!-- main -->
Expand All @@ -123,14 +159,99 @@ <h1 class="title">SQL</h1>
</div>


</header><section id="postgresql-interesting-commands" class="level3"><h3 class="anchored" data-anchor-id="postgresql-interesting-commands">1. PostgreSQL interesting commands</h3>
</header><section id="comment-supprimer-les-doublons-dune-table" class="level2"><h2 class="anchored" data-anchor-id="comment-supprimer-les-doublons-dune-table">1. Comment supprimer les doublons d’une table</h2>
<p>La table prise pour exemple sera la table elu composées des champs suivants :</p>
<ul>
<li>elu_id (primary key)</li>
<li>elu_nom</li>
<li>elu_prenom</li>
<li>elu_date_naissance</li>
<li>elu_lieu_naissance</li>
<li>elu_genre</li>
</ul>
<p>Nous pouvons identifier les doublons comme suit :</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span> elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre, <span class="fu">COUNT</span>(<span class="op">*</span>) <span class="kw">FROM</span> elu </span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">GROUP</span> <span class="kw">BY</span> elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre </span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">HAVING</span> <span class="fu">COUNT</span>(<span class="op">*</span>) <span class="op">&gt;</span> <span class="dv">1</span>; </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>3 méthodes possibles pour supprimer les doublons :</p>
<ol type="1">
<li>suppression depuis un INNER JOIN (USING avec postgresql)</li>
</ol>
<p>Cette méthode ne marche pas s’il n’existe pas d’index (comme elu_id ici)</p>
<ol start="2" type="1">
<li>La deuxième méthode consiste à</li>
</ol>
<ul>
<li>recréer une table identique</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">CREATE</span> <span class="kw">TABLE</span> <span class="cf">IF</span> <span class="kw">NOT</span> <span class="kw">EXISTS</span> new_elu (</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> elu_id SERIAL <span class="kw">PRIMARY</span> <span class="kw">KEY</span>,</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> elu_nom <span class="dt">VARCHAR</span>(<span class="dv">50</span>) <span class="kw">NOT</span> <span class="kw">NULL</span>,</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> elu_prenom <span class="dt">VARCHAR</span>(<span class="dv">50</span>) <span class="kw">NOT</span> <span class="kw">NULL</span>,</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> elu_date_naissance <span class="dt">DATE</span> <span class="kw">NOT</span> <span class="kw">NULL</span>,</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> elu_lieu_naissance <span class="dt">VARCHAR</span>(<span class="dv">50</span>),</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> elu_genre genre <span class="kw">NOT</span> <span class="kw">NULL</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> );</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ul>
<li>copier une version unique de chaque enregistrement</li>
</ul>
<ol type="a">
<li>première méthode</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">INSERT</span> <span class="kw">INTO</span> new_elu (elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre)</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span> elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre </span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> elu </span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="kw">GROUP</span> <span class="kw">BY</span> elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="2" type="a">
<li>deuxième méthode avec DISTINCT</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">INSERT</span> <span class="kw">INTO</span> new_elu (elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span> <span class="kw">DISTINCT</span> elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> elu;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ul>
<li>supprimer la table initiale</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">DROP</span> <span class="kw">table</span> elu;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ul>
<li>renommer la copie</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">ALTER</span> <span class="kw">table</span> new_elu</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="kw">RENAME</span> <span class="kw">TO</span> elu;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="3" type="1">
<li>la methode consiste à utiliser la fonction ROW_NUMBER()</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">DELETE</span> <span class="kw">FROM</span> elu</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="kw">WHERE</span> elu_id <span class="kw">IN</span> (</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span> elu_id</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> (</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span> elu_id,</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span>(<span class="kw">PARTITION</span> <span class="kw">BY</span> elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre) <span class="kw">AS</span> <span class="kw">rownum</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> elu</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> ) <span class="kw">AS</span> sub</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">WHERE</span> <span class="kw">rownum</span> <span class="op">&gt;</span> <span class="dv">1</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a>);</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section><section id="postgresql-interesting-commands" class="level2"><h2 class="anchored" data-anchor-id="postgresql-interesting-commands">2. PostgreSQL interesting commands</h2>
<ul>
<li><p>REASSIGN OWNED BY &lt;old_role&gt; to &lt;new_role&gt;;</p></li>
<li><p>REVOKE ALL PRIVILEGES ON DATABASE &lt;database&gt; FROM &lt;role&gt;;</p></li>
</ul>
</ul></section><section id="section" class="level2"><h2 class="anchored" data-anchor-id="section"></h2>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>[1] "elu" "csp" "ou594"</code></pre>
<pre><code>[1] "elu" "ou594"</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code> elu_genre elu_nom elu_prenom elu_lieu_naissance
Expand Down
107 changes: 106 additions & 1 deletion sql.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,117 @@
title: "SQL"
---

### 1. PostgreSQL interesting commands
## 1. Comment supprimer les doublons d'une table

La table prise pour exemple sera la table elu composées des champs suivants :

- elu_id (primary key)
- elu_nom
- elu_prenom
- elu_date_naissance
- elu_lieu_naissance
- elu_genre

Nous pouvons identifier les doublons comme suit :

```{SQL}
SELECT elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre, COUNT(*) FROM elu
GROUP BY elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre
HAVING COUNT(*) > 1;
```
3 méthodes possibles pour supprimer les doublons :

1. suppression depuis un INNER JOIN (USING avec postgresql)

```{SQL}
#| echo: false
# Cette méthode ne fonctionne pas ici
DELETE FROM elu e1
USING elu e2
WHERE e1.elu_nom = e2.elu_nom
AND e1.elu_prenom = e2.elu_prenom
AND e1.elu_date_naissance = e2.elu_date_naissance
AND e1.elu_lieu_naissance = e2.elu_lieu_naissance
AND e1.elu_genre = e2.elu_genre
AND e1.elu_id < e2.elu_id;
```
Cette méthode ne marche pas s'il n'existe pas d'index (comme elu_id ici)

2. La deuxième méthode consiste à

- recréer une table identique

```{SQL}
CREATE TABLE IF NOT EXISTS new_elu (
elu_id SERIAL PRIMARY KEY,
elu_nom VARCHAR(50) NOT NULL,
elu_prenom VARCHAR(50) NOT NULL,
elu_date_naissance DATE NOT NULL,
elu_lieu_naissance VARCHAR(50),
elu_genre genre NOT NULL
);
```
- copier une version unique de chaque enregistrement

a. première méthode

```{SQL}
INSERT INTO new_elu (elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre)
SELECT elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre
FROM elu
GROUP BY elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre;
```
b. deuxième méthode avec DISTINCT

```{SQL}
INSERT INTO new_elu (elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre)
SELECT DISTINCT elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre
FROM elu;
```

- supprimer la table initiale

```{SQL}
DROP table elu;
```

- renommer la copie

```{SQL}
ALTER table new_elu
RENAME TO elu;
```

3. la methode consiste à utiliser la fonction ROW_NUMBER()

```{SQL}
DELETE FROM elu
WHERE elu_id IN (
SELECT elu_id
FROM (
SELECT elu_id,
ROW_NUMBER() OVER(PARTITION BY elu_nom, elu_prenom, elu_date_naissance, elu_lieu_naissance, elu_genre) AS rownum
FROM elu
) AS sub
WHERE rownum > 1
);
```







## 2. PostgreSQL interesting commands

- REASSIGN OWNED BY \<old_role\> to \<new_role\>;

- REVOKE ALL PRIVILEGES ON DATABASE \<database\> FROM \<role\>;

##


```{r}
#| label: load-ggplot
#| echo: false
Expand Down

0 comments on commit 1072443

Please sign in to comment.