LAG et GROUP BY non compatibles dans Maria DB SQL
J'ai cette requête SQL dans MariaDB
SELECT substr(sqlth_te.tagpath, 32), stringvalue,
((t_stamp - (CASE WHEN sqlth_te.tagpath = LAG(sqlth_te.tagpath,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp) Then LAG(t_stamp,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp)
ELSE NULL
END))/1000) as seconds
FROM sqlt_data_1_2022_04
LEFT JOIN sqlth_te
ON sqlt_data_1_2022_04.tagid = sqlth_te.id
WHERE stringvalue IS NOT NULL
ORDER BY sqlth_te.tagpath, t_stamp
code sql
Qui renvoie 3 colonnes ; une colonne avec les noms des machines, l'état d'exécution et la durée depuis le changement d'état.
résultats
J'aimerais additionner la durée par nom de machine et état d'exécution, mais lorsque j'essaie d'ajouter une somme et de grouper par, j'obtiens une erreur.
SELECT substr(sqlth_te.tagpath, 32), stringvalue,
SUM((t_stamp - (CASE WHEN sqlth_te.tagpath = LAG(sqlth_te.tagpath,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp) Then LAG(t_stamp,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp)
ELSE NULL
END))/1000) as seconds
FROM sqlt_data_1_2022_04
LEFT JOIN sqlth_te
ON sqlt_data_1_2022_04.tagid = sqlth_te.id
WHERE stringvalue IS NOT NULL
ORDER BY sqlth_te.tagpath, t_stamp
GROUP BY substr(sqlth_te.tagpath, 32), stringvalue
Erreur : java.sql.SQLSyntaxErrorException : (conn=8) Vous avez une erreur dans votre syntaxe SQL ; consultez le manuel qui correspond à la version de votre serveur MariaDB pour la bonne syntaxe à utiliser près de 'GROUP BY substr(sqlth_te.tagpath, 32), stringvalue' à la ligne 10
Des idées sur ce que je fais mal ou s'il est possible de regrouper une colonne générée avec la fonction de décalage ?
Merci
Solution du problème
Première chose: le GROUP BY
devrait venir avant leORDER BY
Vous devrez peut-être également l'imbriquer comme ceci :
SELECT tagpath, stringvalue, SUM(seconds) as seconds
FROM (
SELECT substr(sqlth_te.tagpath, 32) as tagpath, stringvalue,
((t_stamp - (CASE WHEN sqlth_te.tagpath = LAG(sqlth_te.tagpath,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp) Then LAG(t_stamp,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp)
ELSE NULL
END))/1000) as seconds
FROM sqlt_data_1_2022_04
LEFT JOIN sqlth_te
ON sqlt_data_1_2022_04.tagid = sqlth_te.id
WHERE stringvalue IS NOT NULL
)
GROUP BY tagpath, stringvalue
ORDER BY tagpath, stringvalue
Commentaires
Enregistrer un commentaire