Calculating a value in a column using lag of the very same column

Viewed 12
MPAN CURRENT_SUPPLIER EAC EAC_EFD Days Applicable Overlap 1 Overlap 2 Overlap 3 Overlap 4 Overlap 5 Overlap 6 Overlap 7 Overlap 8 Overlap 9 Overlap 10 Overlap 11 Overlap 12 DailyUsage
ID1 TGPL 96.7 22/05/2021 365 0 0 0 0 0 0 0 0 0 0 0 0 0.26
ID1 TGPL 28009.9 22/06/2021 31 334 0 0 0 0 0 0 0 0 0 0 0 900.69
ID1 TGPL 30771.2 22/07/2021 30 31 304 0 0 0 0 0 0 0 0 0 0 92.31
ID1 TGPL 191.9 22/08/2021 31 30 31 273 0 0 0 0 0 0 0 0 0 -986.16
ID1 TGPL 217.9 22/09/2021 31 31 30 31 242 0 0 0 0 0 0 0 0 1.10
ID1 TGPL 118.1 22/10/2021 30 31 31 30 31 212 0 0 0 0 0 0 0 -3.06
ID1 TGPL 38 22/11/2021 31 30 31 31 30 31 181 0 0 0 0 0 0 -2.32
ID1 TGPL 18.2 22/12/2021 30 31 30 31 31 30 31 151 0 0 0 0 0 -0.40
ID1 TGPL 217.9 22/01/2022 31 30 31 30 31 31 30 31 120 0 0 0 0 6.71
ID1 TGPL 30771.2 22/02/2022 31 31 30 31 30 31 31 30 31 89 0 0 0 985.86
ID1 EMEB 18240.1 22/03/2022 28 31 31 30 31 30 31 31 30 31 61 0 0 -447.27
ID1 EMEB 18240.1 22/04/2022 31 28 31 31 30 31 30 31 31 30 31 30 0 0.26
ID1 EOND 33951 05/05/2022 13 31 28 31 31 30 31 30 31 31 30 31 17 1208.80

Hello, please see my above table of values we are using to calculate the final column DailyUsage

The above table has come from excel where the calculations work fine over a small number of entries but we're looking to do this calculation on over 100,000 lines of data...

The calculation itself is simple in theory but also kind of self-referencing. DailyUsage is initially all 0's then it should run the calculations LDU1Cal and do = EAC minus the (overlap_1 * LDU1)+(Overlap_2 * LDU2)...+ (Overlap_12 * LDU12) (LDU is a temp column using a lag to pull the previous rows DailyUsage the number of the LDU determines how many rows above it is referencing)

This means we need to loop through the calculation once, the first row is calculated correctly, the rest take LDU values as 0 then we run the calculation again and it now has a LAG value to reference amending the DailyUsage value and soon, this is why we tried to do the "while" within the initial "while". hope that helps with the charity of my query. Essentially if anyone knows why it's not working or how I can do it better let me know?

We've used PHP with MYSQLI to calculate everything so far and this is what we have got (prints and breaks are to see what I'm outputting but the most common error is Warning: Undefined array key "LDU1" and its the same for all of the LDU's please help if you're able) ;

  $SQLCal = "SELECT Record_ID, MPAN, EAC, EAC_EFD, Days_Applicable, Overlap_1, "
           ."Overlap_2, Overlap_3, Overlap_4, Overlap_5, Overlap_6, "
           ."Overlap_7, Overlap_8, Overlap_9, Overlap_10, Overlap_11, Overlap_12, DailyUsage "
           ." FROM db_DEV.tbl_AVR_ELK_NHH_Overlap ORDER BY MPAN, EAC_EFD ";

  $SQLCALUpd = mysqli_query($conn,$SQLCal);


 if (mysqli_num_rows($SQLCALUpd)> 0 ) {
         while ($row = mysqli_fetch_assoc($SQLCALUpd)) {


 $LDUsql1 = "SELECT *, LAG(DailyUsage,1,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU1 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql2 = "SELECT *, LAG(DailyUsage,2,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU2 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql3 = "SELECT *, LAG(DailyUsage,3,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU3 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql4 = "SELECT *, LAG(DailyUsage,4,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU4 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql5 = "SELECT *, LAG(DailyUsage,5,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU5 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql6 = "SELECT *, LAG(DailyUsage,6,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU6 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql7 = "SELECT *, LAG(DailyUsage,7,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU7 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql8 = "SELECT *, LAG(DailyUsage,8,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU8 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql9 = "SELECT *, LAG(DailyUsage,9,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU9 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql10 = "SELECT *, LAG(DailyUsage,10,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU10 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql11 = "SELECT *, LAG(DailyUsage,11,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU11 FROM tbl_AVR_ELK_NHH_Overlap";

 $LDUsql12 = "SELECT *, LAG(DailyUsage,12,0) over (partition by MPAN order by MPAN, EAC_EFD) AS LDU12 FROM tbl_AVR_ELK_NHH_Overlap";



 $LDUData1 =  mysqli_query($conn,$LDUsql1);
 $LDUData2 =  mysqli_query($conn,$LDUsql2);
 $LDUData3 =  mysqli_query($conn,$LDUsql3);
 $LDUData4 =  mysqli_query($conn,$LDUsql4);
 $LDUData5 =  mysqli_query($conn,$LDUsql5);
 $LDUData6 =  mysqli_query($conn,$LDUsql6);
 $LDUData7 =  mysqli_query($conn,$LDUsql7);
 $LDUData8 =  mysqli_query($conn,$LDUsql8);
 $LDUData9 =  mysqli_query($conn,$LDUsql9);
 $LDUData10 =  mysqli_query($conn,$LDUsql10);
 $LDUData11 =  mysqli_query($conn,$LDUsql11);
 $LDUData12 =  mysqli_query($conn,$LDUsql12);

$VLDU1 = $row["LDU1"] ;
$VLDU2 = $row["LDU2"] ;
$VLDU3 = $row["LDU3"] ;
$VLDU4 = $row["LDU4"] ;
$VLDU5 = $row["LDU5"] ;
$VLDU6 = $row["LDU6"] ;
$VLDU7 = $row["LDU7"] ;
$VLDU8 = $row["LDU8"] ;
$VLDU9 = $row["LDU9"] ;
$VLDU10 = $row["LDU10"] ;
$VLDU11 = $row["LDU11"] ;
$VLDU12 = $row["LDU12"] ;




         $RecID = $row["Record_ID"];
         $OL1xDU = $row["Overlap_1"] * $VLDU1 ;
         $OL2xDU = $row["Overlap_2"] * $VLDU2 ;
         $OL3xDU = $row["Overlap_3"] * $VLDU3 ;
         $OL4xDU = $row["Overlap_4"] * $VLDU4 ;
         $OL5xDU = $row["Overlap_5"] * $VLDU5 ;
         $OL6xDU = $row["Overlap_6"] * $VLDU6 ;
         $OL7xDU = $row["Overlap_7"] * $VLDU7 ;
         $OL8xDU = $row["Overlap_8"] * $VLDU8 ;
         $OL9xDU = $row["Overlap_9"] * $VLDU9 ;
         $OL10xDU = $row["Overlap_10"] * $VLDU10 ;
         $OL11xDU = $row["Overlap_11"] * $VLDU11 ;
         $OL12xDU = $row["Overlap_12"] * $VLDU12 ;
         $OLSUM  =  $OL1xDU + $OL2xDU + $OL3xDU + $OL4xDU + $OL5xDU + $OL6xDU + $OL7xDU + $OL8xDU + $OL9xDU + $OL10xDU + $OL11xDU + $OL12xDU;

         $LDU1Cal = ($row["EAC"] - $OLSUM)  / $row["Days_Applicable"] ;

         $DUpd = "UPDATE db_DEV.tbl_AVR_ELK_NHH_Overlap "
                ."SET "
                ."DailyUsage = '$LDU1Cal'  "
                ."WHERE "
                ." Record_ID = '$RecID'   ";


         $VUpd = mysqli_query($conn,$DUpd);

                 echo"<br>";
                 echo $DUpd;
                 echo"<br>";
echo"<br>";
print_r($row["Overlap_1"]);
echo"<br>";

echo"<br>";
print_r($row["LDU1"]);
echo"<br>";
echo"<br>";
print_r($row["Overlap_2"]);
echo"<br>";

echo"<br>";
print_r($row["LDU2"]);
echo"<br>";

}

 echo"<br><br>Daily Usage Calc";
 echo"<br>------------------------------------------------------------------------------------------<br>";


}


mysqli_close($conn);
?>
0 Answers
Related