mysql

时间:2017-12-26 13:42:54

标签: mysql sql postgresql

我在创建查询时遇到一些问题,该查询为我提供了按日期分组的数据。我在stackoverflow中阅读了一些示例,但无法做到。任何人都可以帮我理解如何做到这一点吗?这是我的数据:

veiculo_id | data_pacote              | hodometro_inicial | hodometro_final | velocidade

1125370    | '2017-11-13 22:53:12-02' | 6673657           | 6673657         | '20'
1125370    | '2017-11-13 22:53:11-02' | 42526             | 42526           | '35' 
1125370    | '2017-11-13 22:53:09-02' | 2532721           | 2532721         | '47'
1125370    | '2017-11-13 22:53:05-02' | 386358            | 386358          | '0'
1125370    | '2017-11-14 12:53:03-02' | 273085            | 273085          | '45'
1125370    | '2017-11-14 12:53:02-02' | 2004482           | 2004482         | '43'

我需要得到每个veiculo_id(dadtveioid)当天的摘要,这将是当天dadtodometro的最大值和最小值以及当天的平均dadtvelocidade。结果是这样的:

veiculo_id | data_pacote   | hodometro_inicial | hodometro_final | velocidade

1125370    | '2017-11-13'  | 6673657           | 42526           | '51'
1125370    | '2017-11-14'  | 2004482           | 273085          | '44'

我现在拥有的是:

SELECT dadtveioid AS veiculo_id,
       dadtdt_pacote AS data_pacote, 
       min(dadtodometro) AS hodometro_inicial, 
       max(dadtodometro) AS hodometro_final, 
       sum(dadtvelocidade) AS velocidade 
FROM dados_telemetria  
GROUP BY dadtveioid, dadtdt_pacote 
ORDER BY dadtdt_pacote DESC 

感谢。

1 个答案:

答案 0 :(得分:0)

您可以在postgreSQL中尝试以下代码

WITH dados_telemetria AS (
SELECT 1125370::int AS veiculo_id, '2017-11-13 22:53:12-02'::TIMESTAMP AS data_pacote, 6673657::int AS hodometro_inicial, 6673657::int AS hodometro_final,'20'::int AS velocidade
UNION ALL
SELECT 1125370::int AS veiculo_id, '2017-11-13 22:53:11-02'::TIMESTAMP AS data_pacote, 42526::int AS hodometro_inicial,   42526::int AS hodometro_final, '35'::int AS velocidade
UNION ALL
SELECT 1125370::int AS veiculo_id, '2017-11-13 22:53:09-02'::TIMESTAMP AS data_pacote, 2532721::int AS hodometro_inicial, 2532721::int AS hodometro_final,'47'::int AS velocidade
UNION ALL
SELECT 1125370::int AS veiculo_id, '2017-11-13 22:53:05-02'::TIMESTAMP AS data_pacote, 386358::int AS hodometro_inicial,  386358::int AS hodometro_final, '0'::int AS velocidade
UNION ALL
SELECT 1125370::int AS veiculo_id, '2017-11-14 12:53:03-02'::TIMESTAMP AS data_pacote, 273085::int AS hodometro_inicial,  273085::int AS hodometro_final, '45'::int AS velocidade
UNION ALL
SELECT 1125370::int AS veiculo_id, '2017-11-14 12:53:02-02'::TIMESTAMP AS data_pacote, 2004482::int AS hodometro_inicial, 2004482::int AS hodometro_final,'43'::int AS velocidade
)
SELECT veiculo_id,
       DATE(data_pacote) AS data_pacote,
       min(hodometro_inicial) AS min_hodometro_inicial, 
       max(hodometro_inicial) AS max_hodometro_inicial, 
       min(hodometro_final) AS min_hodometro_final, 
       max(hodometro_final) AS max_hodometro_final, 
       sum(velocidade) AS velocidade 
FROM 
    dados_telemetria  
GROUP BY 
    veiculo_id,
    DATE(data_pacote)
ORDER BY data_pacote DESC 

您将获得以下结果

veiculo_id data_pacote min_hodometro_inicial max_hodometro_inicial min_hodometro_final max_hodometro_final velocidade 
---------- ----------- --------------------- --------------------- ------------------- ------------------- ---------- 
1125370    2017-11-13  42526                 6673657               42526               6673657             102        
1125370    2017-11-14  273085                2004482               273085              2004482             88