0

I am using control center HFSQL of windev 20 to execute a query. I would like to calculate the time and date difference, between the maximum and the minimum MAX (hour) and MIN (hour) for each service and also the number of days if there is.

I tried the functions DateDifference and DateTimeDifference but unfortunately I have an error.

the following code is just to see the maximum and the minimum hour for each service it works perfectly but not what i want :

SELECT  Service.Libellé AS Libellé,  
Min(DetailCircuitFacture.Heure),MAX(DetailCircuitFacture.Heure) 
FROM detailcircuitfacture
joIN Service on
Service.CodeSce=detailcircuitfacture.CodeSce
group by Service.Libellé

i want to make difference of date and hour between MAX and Min for each service like so :

SELECT Service.Libellé AS Libellé, WL.DateDifférence(Min(DetailCircuitFacture.DATE),
Max(DetailCircuitFacture.DATE)) AS Nombre_jours,
    WL.DateHeureDifférence(Min(DetailCircuitFacture.Heure),Max(DetailCircuitFacture.Heure)) AS Nombre_heurs
    FROM detailcircuitfacture
    JOIN Service on
    Service.CodeSce=detailcircuitfacture.CodeSce
    group by Service.Libellé

I expect the output without error, but the actual output is

Error in the SQL code of the <> request. Initialization of the query impossible. Aggregate functions (COUNT, AVG, SUM, MIN, MAX) are not allowed outside SELECT or HAVING clauses


expect result

expected result

Thanks by advance

4

1 回答 1

0

我想说你的问题是试图使用聚合函数的结果作为函数参数。

您可以使用子查询解决此问题:

  SELECT Libellé,
         WL.DateDifférence(min_date, max_date) AS Nombre_jours,
         WL.DateHeureDifférence(min_heurs, max_heurs) AS Nombre_heurs
    FROM (
     SELECT Service.Libellé AS Libellé, 
            MIN(DetailCircuitFacture.DATE) AS min_date,
            MAX(DetailCircuitFacture.DATE) AS max_date,
            MIN(DetailCircuitFacture.Heure) AS min_heurs,
            MAX(DetailCircuitFacture.Heure) AS max_heurs
       FROM detailcircuitfacture
       JOIN Service ON Service.CodeSce=detailcircuitfacture.CodeSce
   GROUP BY Service.Libellé
     ) core
 GROUP BY Libellé;

编辑:由于原始问题已更新为包括关于 HFSQL,我应该说我没有使用 HFSQL 的经验。此答案基于 MySQL、PostgreSQL 和 SQL Server 等更常见数据库共享的基本 SQL 语法。

于 2019-04-04T17:01:54.003 回答