Link to home
Start Free TrialLog in
Avatar of micamb
micamb

asked on

Calculating value of records from a column - classic asp

Yes - Classic ASP - I'm that old...

AnywaY - I have a table in my mysql database that I use to store all actions performed on a specific task. Let's call it TaskHistory. I have another table - let's call it TaskList - that contains the newest version of the task - a clumsy revision management system you might say.

Anyway - If my users choose the task category "Project" they are given the opportunity to enter a price for this current part of the project. Every time the task is updated a price is therefore added to TaskHistory containing (if applicable) a new price every time. Each of the tasks placed in TaskHistory have the same caseID appended as the original task in TaskList.

What I want to do is this: I want to run through TaskHistory & calculate the total from all of the tasks related to (e.g.) task 21. The current price in entered in Task 21 is 213, however if you calculate the total in TaskHistory the result is 5461. Is this possible?

Also: I have an overview page where I'd like this calculation done on every project in the database - so some kind of loop might be the solution. I'm a total arse head when it comes to programming stuff like this (ask my math teacher) so help is much appreciated!

BR
Michael
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of micamb
micamb

ASKER

Hi Ryan!

This is what worked on the individual task page:
    sSQL = _
     " SELECT "				&_
	"  SUM(Pris)     "			&_
	" AS Pris   "			&_
	"  FROM "			&_
	"  idFejlmelding "			&_
" WHERE   "			&_
	"  cid = '" & id & "' "			&_
" GROUP BY cid "

Set oRsTemp = Server.CreateObject("ADODB.RecordSet")
oRsTemp.Open sSQL, strConnect
Do While Not oRsTemp.EOF
Price = CInt(oRsTemp("Pris"))

oRsTemp.MoveNext
   Loop 

Open in new window


Seems to work... However the overview page proves more difficult. Here's the code where I'd want this to happen. The issue is looking in a different table (idFejlmelding in this case), calculating the total of Price for each record that corresponds to each record in Fejlmelding. Inserting above code doesn't work - that only works on an individual level (when you have the id):
<html>
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#000000" data-role="table" data-mode="columntoggle" class="ui-responsive ui-shadow" data-column-btn-text="Vis/skjul kolonner" id="myTable" data-filter="true" data-input="#filterTable-input"><thead><tr><th ><b>Sagsnr.</b></th><th data-priority="1"><b>Bruger</b></th><th><b><%= Matrikel %></b></th><th data-priority="3"><b>Type</b></th><th data-priority="2"><b>Fejlbeskrivelse</b></th><th data-priority="5"><b>Modtaget</th><th data-priority="1"><b>Status</b></th><th ><b>Bemærkninger</b></th></tr></thead>
<tbody>




<%  Set DATABASE = Server.CreateObject("ADODB.Connection")
    DATABASE.Open strConnect

If search = "" Then '*** Find brugernavn & adgangskode
    SQLmaal = _
     " SELECT "				&_
	"  *     "			&_
	" FROM   "			&_
	"  Fejlmelding "			&_
" Order by Datomodtaget DESC "

Else
 '*** Find brugernavn & adgangskode
    SQLmaal = _
     " SELECT "				&_
	"  *     "			&_
	" FROM   "			&_
	"  Fejlmelding "			&_
        " WHERE " &_
	     "Navn LIKE '" & search & "' " &_
" Order by Datomodtaget DESC "
End if



   Set RSDatabase = DATABASE.Execute(SQLmaal)
   Do While Not RSDatabase.EOF

   id = RSDatabase("Id")
   navn = RSDatabase("Navn")
   lokale = RSDatabase("Lokale")
   Opgavetype = RSDatabase("Opgavetype")
   Beskrivelse = RSDatabase("Beskrivelse")
   Institution = RSDatabase("Institution")
   filepath = RSDatabase("filepath")
   Datomodtaget = RSDatabase("Datomodtaget")
   Datoafsluttet = RSDatabase("Datoafsluttet")
   Status = RSDatabase ("Status")
   Bemaerkninger = RSDatabase("bemaerkninger")
   Sagsnr = RSDatabase("Sagsnr")
  Price = RSDatabase("Price")

If filepath = "" Then
file = "-"
Else
file = "<br><a href=files/" & filepath &" class='ui-btn ui-icon-camera ui-btn-icon-top ui-btn-inline' data-ajax='false' target='_blank'></a>"
End if

If lokale = "" Then
lokalevis = ""
Else
lokalevis = "<br>Lokale:<font color=#FF0000>" & lokale & "</font>"
End if 

If sagsnr = "" then
vis_sagsnr = ""
Else
vis_sagsnr = "<br>Ext. sagsnr: " & sagsnr & ""
End if

If Status = 1 Then StatusSag = "<td bgcolor=FF0000><b><font color=ffffff>Modtaget<br>" & Datoafsluttet & "</font>"
If Status = 2 Then StatusSag = "<td bgcolor=FFFF00><b><font color=000000>Se bemærkninger<br>" & Datoafsluttet & "</font>"
If Status = 23 Then StatusSag = "<td bgcolor=009933><b><font color=ffffff>Afsluttet<br>" & Datoafsluttet & "</font>"
If Status = 24 Then StatusSag = "<td bgcolor=FFFFff><b><font color=000000>Afvist<br>" & Datoafsluttet & "</font>"

If Datoafsluttet = "0000-00-00 00:00:00" Then Datoafsluttet1 = "<font color=000000>Fejlfinding afventer"
 

   %><tr onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#FFFFFF'"><%
  
Response.write "<td><b><a href=opd_fejlmelding.asp?id=" & id & " data-ajax='false' class='ui-btn ui-icon-edit ui-btn-icon-top ui-btn-inline'>" & id & "</a></font></td><td><b>" & navn & "</a></font></td>"
   Response.write "<td><b>" & Institution & "" & lokalevis & "</font></td><td><b>" & opgavetype & "</font></td><td><left><b>" & Beskrivelse & "</font></td><td><b>" & Datomodtaget & "</font></td>" & StatusSag & "</font></td><td><b>" & Bemaerkninger & "" & file & "" & vis_sagsnr & "</font></td>"
    
    RSDatabase.MoveNext
   Loop  
    
  %></tr></tbody></table>
<div data-role="footer">
    <h1>Fejlmeld-alt <%= version %></h1>
  </div>
</div>


</body>
</html>

Open in new window

so just to clear things up, you have 2 tables with respective fields here:

table: Fejlmelding:

-Id
-Navn
-Lokale
-Opgavetype
-Beskrivelse
-Institution
-filepath
-Datomodtaget
-Datoafsluttet
-Status
-bemaerkninger
-Sagsnr
-Price

table: idFejlmelding
-cid
-Pris

which info you would like to display in your page?

is that we joining Fejlmelding.id with idFejlmelding.cid together?
Avatar of micamb

ASKER

idFejlmelding contains the exact same fields as Fejlmelding. Only difference is cid which I use to mirror actions taking place in Fejlmelding. idFejlmelding contains multiple versions of the task found in Fejlmelding.

The Price field in fejlmelding is irrelevant for the table I'd like to display. That price is already part of idFejlmelding when the task is entered into the database the first time. So I need a column in my table that contains the calculation of all the Pris values in idFejlmelding that matches each specific task id in Fejlmelding.

The calculation needs to run through idFejlmelding and calculate the total price for a given task with the same id. Given that this overview page displays all tasks (taken from Fejlmelding which only contains the latest version of a task) it would have to run through all tasks in idfejlmelding and display the total for each task on this page.

So to answer your question to the best of my ability - the only link between those two is the id and the cid respectively. I.E.: There is ONLY one task with id 21 in Fejlmelding, but the task has been updated 7 times so in idFejlmelding you'll find 7 versions of the same task with cid 21. Each containing a different price. Makes sense?
BR
Michael
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of micamb

ASKER

Hi Scott,

Thank you for a very elaborative answer! I think my database setup is almost ok - except that my overview takes its tasks from Tasks in your setup. An identical task is logged in Task history. So I can use Task history (idFejlmelding in my case) - I just can't figure out how to only display the latest edition of a task. Some tasks have been changed 5-6 times and they will then show up in my overview 5-6 times. How do I prevent that?
Avatar of micamb

ASKER

I will refrain from calculation in my overview - it will be too heavy in terms of rescources.