Calculated field

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
petrescucld
Posts: 9
Joined: 2016-07-14 17:36

Calculated field

Post by petrescucld » 2020-01-09 00:48

Hi,
I have :
cars.id,
card.registration

diesel.id
diesel.car(lookup to cars.id)
diesel.date
diesel.km
difference - calculated field.

How can i calculate difference if i want it to show the difference between the last 2 inputs for the same car?
Basically when a user adds diesel to car he inputs the km number every time and i want to calculate the difference between the last 2 inputs automatically.

Thank you!

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Calculated field

Post by pbottcher » 2020-01-09 21:44

Hi,

you could try:

Code: Select all

select `diesel`.`km` - IFNULL((SELECT `diesel2`.`km` FROM `diesel` `diesel1` left join `diesel` as `diesel2` on `diesel2`.`car` = `diesel1`.`car` where `diesel1`.`id`=%ID% and `diesel2`.`id`<>%ID% order by `diesel2`.`date` DESC limit 1 ),0) from `diesel` where `diesel`.`id`=%ID%
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

petrescucld
Posts: 9
Joined: 2016-07-14 17:36

Re: Calculated field

Post by petrescucld » 2020-01-10 16:01

Thanks for the help!

This worked:

Code: Select all

SELECT diesel.km - IFNULL((SELECT diesel.km FROM diesel WHERE diesel.car=(select diesel.car from diesel where diesel.id = %ID%) AND diesel.id < %ID% order by diesel.data DESC limit 1),0) from diesel where diesel.id=%ID%

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Calculated field

Post by pbottcher » 2020-01-10 20:19

Great, this assumes that you enter the data in a direct time/date dependent order which helps.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply