Bonjour à tous,

J'ai la requête ci-dessous et j'aimerai savoir s'il est possible de l'améliorer notamment au niveau des sous requêtes qui me permettent de ramener la date associée à la valeur min (PMin) et la valeur max (PMax).
En fait j'aimerai bien ne pas avoir à faire des sous requêtes et donc parcourir 2X ma table.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
   WITH
      CRB_DAILY
      AS
         (SELECT compteur_id,
                 i1.[date] AS datepoint,
                 type_pt_id,
                 (sum (puissance) / 6) AS volume,
                 max (puissance) AS pmax,
                 min (puissance) AS pmin,
                 (SELECT TOP 1 datepoint
                  FROM codap.gtm_crb10mn AS a                 
                       JOIN
                       (SELECT MIN (puissance) AS PMin
                        FROM codap.gtm_crb10mn crb                             
                        WHERE     crb.compteur_id = c1.compteur_id
                              AND crb.type_pt_id = c1.type_pt_id
                              AND cast
                                  (dateadd (minute, -10, datepoint) AS DATE) =
                                  i1.[date] and crb.lastversion = 1
                        GROUP BY crb.compteur_id) AS b
                          ON (    a.puissance = b.PMin
                              AND a.compteur_id = c1.compteur_id
                              AND a.type_pt_id = c1.type_pt_id
                              AND cast
                                  (dateadd (minute, -10, datepoint) AS DATE) =
                                  i1.[date])) AS dtmin,
                 (SELECT TOP 1 datepoint
                  FROM codap.gtm_crb10mn AS a
                       JOIN
                       (SELECT MAX (puissance) AS PMax
                        FROM codap.gtm_crb10mn crb
                        WHERE     crb.compteur_id = c1.compteur_id
                              AND crb.type_pt_id = c1.type_pt_id
                              AND cast
                                  (dateadd (minute, -10, datepoint) AS DATE) =
                                  i1.[date] and crb.lastversion = 1
                        GROUP BY crb.compteur_id) AS b
                          ON (    a.puissance = b.PMax
                              AND a.compteur_id = c1.compteur_id
                              AND a.type_pt_id = c1.type_pt_id
                              AND cast
                                  (dateadd (minute, -10, datepoint) AS DATE) =
                                  i1.[date])) AS dtmax,
                 count (*) AS iCount
          FROM codap.gtm_crb10mn c1
               INNER JOIN codap.gtm_interval_10mn i1
                  ON i1.datepoint = c1.datepoint
          WHERE c1.lastversion = 1
          GROUP BY compteur_id, type_pt_id, i1.[date])
   SELECT compteur_id,
          datepoint,
          type_pt_id,
          volume,
          pmax,
          pmin,
          dtmin,
          dtmax,
          iCount
   FROM CRB_DAILY