From 4e31c0408abdcb9b53e7358bcc82d609b367b8f2 Mon Sep 17 00:00:00 2001
From: Matthieu FAURE <mfaure@asqatasun.org>
Date: Wed, 25 Oct 2023 17:02:03 +0200
Subject: [PATCH] feat: enhance SQL query for CSV export

---
 Documentation/CSV_export.sql | 58 ++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 22 deletions(-)

diff --git a/Documentation/CSV_export.sql b/Documentation/CSV_export.sql
index 79fc9cc..f609175 100644
--- a/Documentation/CSV_export.sql
+++ b/Documentation/CSV_export.sql
@@ -1,25 +1,39 @@
-SELECT WR.Url,
-       a.Dt_Creation                           Audit_date,
-       a.Status,
-       COALESCE(WRS.Nb_Failed_Occurrences, "") Errors,
-       (CASE
-          WHEN WRS.Raw_Mark IS NULL THEN ""
-          WHEN WRS.Raw_Mark > 99 THEN "A"
-          WHEN WRS.Raw_Mark > 90 THEN "B"
-          WHEN WRS.Raw_Mark > 85 THEN "C"
-          WHEN WRS.Raw_Mark > 75 THEN "D"
-          WHEN WRS.Raw_Mark > 60 THEN "E"
-          ELSE "F"
-         END) as                               Mark,
-       COALESCE(WRS.Nb_Na, "")                 Tests_NA,
-       COALESCE(WRS.Nb_Passed, "")             Tests_C,
-       COALESCE(WRS.Nb_Failed, "")             Tests_NC
-INTO OUTFILE '/tmp/Observatoire_Accessibilité_2023-09-19.csv'
-    CHARACTER SET utf8mb4
+SELECT 'URL',
+       'Date',
+       'Statut',
+       'Erreurs',
+       'Tests non-applicables',
+       'Tests conformes',
+       'Tests non-conformes',
+       'Tests pré-qualifiés',
+       'Tests non-testés',
+       'Note Asqatasun'
+UNION ALL
+(SELECT *
+ FROM (SELECT WR.Url                                  as 'URL',
+              a.Dt_Creation                           as 'Date',
+              a.Status                                as 'Statut',
+              COALESCE(WRS.Nb_Failed_Occurrences, '') as 'Erreurs',
+              COALESCE(WRS.Nb_Na, '')                 as 'Tests non-applicables',
+              COALESCE(WRS.Nb_Passed, '')             as 'Tests conformes',
+              COALESCE(WRS.Nb_Failed, '')             as 'Tests non-conformes',
+              COALESCE(WRS.Nb_Nmi, '')                as 'Tests pré-qualifiés',
+              COALESCE(WRS.Nb_Not_Tested, '')         as 'Tests non-testés',
+              (CASE
+                   WHEN WRS.Raw_Mark IS NULL THEN ''
+                   WHEN WRS.Raw_Mark > 99 THEN 'A'
+                   WHEN WRS.Raw_Mark > 90 THEN 'B'
+                   WHEN WRS.Raw_Mark > 85 THEN 'C'
+                   WHEN WRS.Raw_Mark > 75 THEN 'D'
+                   WHEN WRS.Raw_Mark > 60 THEN 'E'
+                   ELSE 'F'
+                  END)                                as 'Note Asqatasun'
+       FROM AUDIT as a
+                INNER JOIN WEB_RESOURCE WR on a.Id_Audit = WR.Id_Audit
+                LEFT JOIN WEB_RESOURCE_STATISTICS WRS on a.Id_Audit = WRS.Id_Audit) as csv_export)
+INTO OUTFILE '/tmp/Observatoire_Accessibilite_2023-09-19.csv'
+    CHARACTER
+SET utf8mb4
     FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\n'
-FROM AUDIT as a
-  INNER JOIN WEB_RESOURCE WR on a.Id_Audit = WR.Id_Audit
-  LEFT JOIN WEB_RESOURCE_STATISTICS WRS on a.Id_Audit = WRS.Id_Audit
-ORDER BY WR.Url
 ;
\ No newline at end of file
-- 
GitLab