BestAviation
asked on
Basic math in MS SQL stored procedure across three tables
I'm trying to do some very basic math in MS SQL by using one stored procedure to SUM the value of a column in Table_A, then divide the SUM from Table_A by the page view total from Table_B - and UPDATE the value in Table_C.
Basically I'm trying to work out the eCPC for a lead generating site on a page view basis (so every time a user visits a profile the eCPC is updated against the SUM of leads generated).
val = common denominator across all three tables
So step 1 is:
SELECT SUM(lead_value) as lead_sum FROM Table_A WHERE camp_id='val'
Step 2:
SELECT page_views FROM Table_B WHERE camp_id='val'
Step 3:
new_eCPC = lead_sum / page_views
Step 4:
UPDATE Table_C SET lead_eCPC='new_eCPC' WHERE camp_id='val'
Now I can easilly do this in ASP, but I have to execute an SQL script three times to do this - which I believe is terribily inefficient as I want it to happen each time a profile is viewed.
Can someone please help we write a stored procedure for this so I only have to execute it once on each profile view?
Many Thanks!
C
Basically I'm trying to work out the eCPC for a lead generating site on a page view basis (so every time a user visits a profile the eCPC is updated against the SUM of leads generated).
val = common denominator across all three tables
So step 1 is:
SELECT SUM(lead_value) as lead_sum FROM Table_A WHERE camp_id='val'
Step 2:
SELECT page_views FROM Table_B WHERE camp_id='val'
Step 3:
new_eCPC = lead_sum / page_views
Step 4:
UPDATE Table_C SET lead_eCPC='new_eCPC' WHERE camp_id='val'
Now I can easilly do this in ASP, but I have to execute an SQL script three times to do this - which I believe is terribily inefficient as I want it to happen each time a profile is viewed.
Can someone please help we write a stored procedure for this so I only have to execute it once on each profile view?
Many Thanks!
C
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is brilliant! Thanks very much guys - I don't use stored procedures very often so wasn't aware you could declare variables that easilly and execute multiple lines.
Strange choice. Multiple SELECT statements over a single UPDATE statement? Obviously performance is not important to you.
Open in new window