Hello All,I have a question. Maybe someone can help. I have a few tables. One is Users Profile. The other is Trip Information. What I would like to do or see if it can be done, is add all miles for that user from Trip Information and list how many total miles ran on there User Profile.
So User profile table is:
profile_id
namepasswordtotal_miles
so on.....
Then in Trip Info table is:
trip_info_idprofile_idstatemilesso on....
The two tables are joined by profile_id
So how would I add all miles by the profile_id on the Trip Info Table, and have it posted on the users profile at the total_miles?
Thank You for any help on this.James
The are several ways to do this depending on the overall objective and ones own personal coding style preferences.
What is the overall purpose of adding the trip miles to the users profile; is it a one off exercise to get a total or is this a recurring activity, i.e. every time the users profile is displayed you want to see the current total, or you want the total miles on the users profile for some other activity like payment for miles travelled.
- The one off situation, I'd create a list page for the User Profile table with code in the List Page: Before Process event, that would loop through all the user profile records in the Trip Info Table total by user id and update each User Profile table record with the total miles. When the list page displays it will show the total miles for each User, depending on volumes this can take a while to execute.
- To display the current total miles whenever the User profile is displayed does not require the total miles to be held on the user profile. In a custom view join the User Profile to the Trip Info table, as you have done, then modify the SQL to have SUM(Trip Info.statemiles) as user_total_miles, include a GROUP BY Profile Id. This will result in the total miles per user being calculated dynamically everytime the user profile is displayed.
- To use the Users total miles in some other process like calculating expenses ore reimbursing for fuel usage, you would probably need a date in each Trip Info record, or record that the trip has been processed. As you haven't mentioned that we ignore that bit as it doesn't affect the basic solution. This solution uses a combination of the (a) and (<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=94014&image=1&table=forumreplies' class='bbc_emoticon' alt='B)' /> above. Assuming a list screen to display the users profile, include a calculated field (PHPRunner Help explains how to do this) in the before display event add code to loop through all the trip records for the user total the miles and store the result in the calculated field on the page. There is another way to do this. Define a calculated field and then on the Fields Tab properties column for the calculated field, View As Custom put the code to calculate the trip total miles.
As an aside I've generally created my own dummy fields in these circumstances using CONCAT(" ") as My_Dummy_Field in the SQL for the custom view.
Hope this helps.
Regards