Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Comment faire la moyenne des 4 dernières valeurs de colonnes d'un tableau ( il faudrait détecter ces 4 dernières colonnes automatiquement) groupé par ID
Une nouvelle version
Data:
LOAD
VmDmd,
DateDmd,
BatterieDmd,
UtilisationDmd,
[A.h Consommee]
FROM .\Downloads\output.csv
(txt, utf8, embedded labels, delimiter is ',', msq);
Dataoutput:
load distinct BatterieDmd resident Data;
Final:
LOAD 1 as Tmp autogenerate 1;
For vRow = 1 to NoOfRows('Dataoutput')
Let MyVar = Peek('BatterieDmd',vRow-1,'Dataoutput');
Tmp:
noconcatenate
load * resident Data where BatterieDmd='$(MyVar)' order by DateDmd ;
Let vMin = Floor(Peek('DateDmd',0));
Let vMax = Floor(Peek('DateDmd',-1));
Join LOAD Date($(vMin))+recno()-1 as DateDmd
AutoGenerate $(vMax)-$(vMin)+1;
outputTmp:
noconcatenate
load * resident Tmp order by DateDmd DESC;
drop table Tmp;
Join(Final)
load if(IsNull(UtilisationDmd),peek(UtilisationDmd),UtilisationDmd) as UtilisationDmd,if(IsNull(VmDmd),peek(VmDmd),VmDmd) as VmDmd,if(IsNull(BatterieDmd),peek(BatterieDmd),BatterieDmd) as BatterieDmd,DateDmd,Year(DateDmd) as Année,Week(DateDmd) as Semaine ,if(IsNull([A.h Consommee]),0,[A.h Consommee]) as [A.h Consommee] resident outputTmp;
drop table outputTmp;
Next
drop table Data,Dataoutput;
DROP Field Tmp;
Tmp0:
noconcatenate
load [BatterieDmd],Semaine,avg(DateDmd) as DateDmdSort,Année, avg([A.h Consommee]) as MoyenParSemaine resident Final group by [BatterieDmd],Année,Semaine ;
drop table Final;
Data_:
noconcatenate
load * resident Tmp0 order by BatterieDmd,Année,Semaine;
drop table Tmp0;
Tmp_:
noconcatenate
load *, if(rowno()=0,0,if(previous([BatterieDmd])=[BatterieDmd],peek(Val)+1,0)) as Val
resident Data_;
drop table Data_;
output:
noconcatenate
load BatterieDmd,
RangeAvg(FirstSortedValue(MoyenParSemaine,-Val,1),FirstSortedValue(MoyenParSemaine,-Val,2),FirstSortedValue(MoyenParSemaine,-Val,3),FirstSortedValue(MoyenParSemaine,-Val,4)) as Moyenne
resident Tmp_ group by BatterieDmd;
c-àd que ici tu veux la moyenne de ?
16/06/2020 | 17/06/2020 | 18/06/2020 | 19/06/2020 |
pour chaque ID ?
oui, sachant que certains champs sont vides
si par exemple pour ces deux IDs 3 et 4
est ce toujours les 4 ou les 4 non vides ?
par exemple pour 4 :
5+0+0+8/4 ou 5+8+45+12/4 ?
3 | 12 | 35 | 36 | 12 | 12 | 43 | 27 | 25 |
4 | 37 | 12 | 45 | 8 | 5 |
toujours les 4; donc : 5+0+0+8
Une proposition
J'ai laissé le code un peu long pour expliquer l'approche :
Data:
CrossTable(Date, Data)
LOAD * inline [
ID, 12/06/2020, 13/06/2020, 14/06/2020, 15/06/2020, 16/06/2020, 17/06/2020, 18/06/2020, 19/06/2020
1, 23, 25, 39, 45, 35, 15, 38, 48
2, 43, 12, 32, 46, 42, 13, 36, 35
3, 12, 35, 36, 12, 12, 43, 27, 25
4, 37, 12, 45, , 8, , , 5
5, 35, 35, 25, 23, 32, 31, 43, 43
6, 32, 25, , 15, 86, 13, 3, 3
7, 27, 18, 28, , , 11, 23,
8, 48, 26, 55, 23, 46, 36, 54, 45
];
Tmp:
noconcatenate
load ID,Date(MakeDate(subfield(Date,'/',3),subfield(Date,'/',2) ,subfield(Date,'/',1) )) as Date,Data as Valeur,
if(rowno()=0,0,if(previous(ID)=ID,peek(Val)+1,0)) as Val
resident Data;
drop table Data;
Tmp1:
load max(Val) as MaxValue resident Tmp;
let vMaxvaleur=peek('MaxValue');
output:
noconcatenate
load ID,
RangeAvg(FirstSortedValue(Valeur,-Date,1),FirstSortedValue(Valeur,-Date,2),FirstSortedValue(Valeur,-Date,3),FirstSortedValue(Valeur,-Date,4)) as Moyenne
resident Tmp where Val >$(vMaxvaleur)-4 group by ID;
drop table Tmp,Tmp1;
output :
Une nouvelle version
Data:
LOAD
VmDmd,
DateDmd,
BatterieDmd,
UtilisationDmd,
[A.h Consommee]
FROM .\Downloads\output.csv
(txt, utf8, embedded labels, delimiter is ',', msq);
Dataoutput:
load distinct BatterieDmd resident Data;
Final:
LOAD 1 as Tmp autogenerate 1;
For vRow = 1 to NoOfRows('Dataoutput')
Let MyVar = Peek('BatterieDmd',vRow-1,'Dataoutput');
Tmp:
noconcatenate
load * resident Data where BatterieDmd='$(MyVar)' order by DateDmd ;
Let vMin = Floor(Peek('DateDmd',0));
Let vMax = Floor(Peek('DateDmd',-1));
Join LOAD Date($(vMin))+recno()-1 as DateDmd
AutoGenerate $(vMax)-$(vMin)+1;
outputTmp:
noconcatenate
load * resident Tmp order by DateDmd DESC;
drop table Tmp;
Join(Final)
load if(IsNull(UtilisationDmd),peek(UtilisationDmd),UtilisationDmd) as UtilisationDmd,if(IsNull(VmDmd),peek(VmDmd),VmDmd) as VmDmd,if(IsNull(BatterieDmd),peek(BatterieDmd),BatterieDmd) as BatterieDmd,DateDmd,Year(DateDmd) as Année,Week(DateDmd) as Semaine ,if(IsNull([A.h Consommee]),0,[A.h Consommee]) as [A.h Consommee] resident outputTmp;
drop table outputTmp;
Next
drop table Data,Dataoutput;
DROP Field Tmp;
Tmp0:
noconcatenate
load [BatterieDmd],Semaine,avg(DateDmd) as DateDmdSort,Année, avg([A.h Consommee]) as MoyenParSemaine resident Final group by [BatterieDmd],Année,Semaine ;
drop table Final;
Data_:
noconcatenate
load * resident Tmp0 order by BatterieDmd,Année,Semaine;
drop table Tmp0;
Tmp_:
noconcatenate
load *, if(rowno()=0,0,if(previous([BatterieDmd])=[BatterieDmd],peek(Val)+1,0)) as Val
resident Data_;
drop table Data_;
output:
noconcatenate
load BatterieDmd,
RangeAvg(FirstSortedValue(MoyenParSemaine,-Val,1),FirstSortedValue(MoyenParSemaine,-Val,2),FirstSortedValue(MoyenParSemaine,-Val,3),FirstSortedValue(MoyenParSemaine,-Val,4)) as Moyenne
resident Tmp_ group by BatterieDmd;