Solved

Basic math in MS SQL stored procedure across three tables

Posted on 2010-08-24
6
1,006 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:BestAviation
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33510284
Something like this perhaps:
U PDATE  c
SET     lead_eCPC = a.lead_sum / b.page_views
FROM    Table_C c
        INNER JOIN (SELECT  SUM(lead_value) lead_sum
                    FROM    Table_A
                    WHERE   camp_id = 'val'
                   ) a ON c.camp_id = a.camp_id
        INNER JOIN Table_B b ON c.camp_id = b.camp_id

Open in new window

0
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 150 total points
ID: 33510298
try
Create procedure yourProc (@camp_id varchar(20))
As 
Begin
	declare @new_eCPC int	
	declare @lead_sum int
	SELECT @lead_sum = SUM(lead_value) as lead_sum FROM Table_A WHERE camp_id= @camp_id
	SELECT @new_eCPC = @lead_sum / page_views  FROM Table_B WHERE camp_id=@camp_id
	UPDATE Table_C SET lead_eCPC= @new_eCPC WHERE camp_id=@camp_id
end

Open in new window

0
 
LVL 4

Assisted Solution

by:udaydidigam
udaydidigam earned 150 total points
ID: 33510377


Create procedure Proc_name (@camp_id varchar(50))
As
declare @sum_A int      
declare @div
Begin      
      SELECT @sum_a = SUM(lead_value) as lead_sum FROM Table_A WHERE camp_id= @camp_id
      SELECT @div = @sum_A /SUM(page_views)  FROM Table_B WHERE camp_id=@camp_id
      UPDATE Table_C SET lead_eCPC= @div WHERE camp_id=@camp_id
end

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Accepted Solution

by:
DerZauberer earned 200 total points
ID: 33510468
Well i cannot see what datatypes your fields have, but should be someting like the following.

You can execute it like this: EXEC UpdateECPC 'campid'

CREATE PROCEDURE UpdateECPC @camp_id varchar(20)
AS
BEGIN
	SET NOCOUNT ON
	DECLARE @lead_sum int
	DECLARE @page_views int
	SET @lead_sum = (SELECT SUM(lead_value) FROM Table_A WHERE camp_id = @camp_id)
	SET @page_views = (SELECT page_views FROM Table_B WHERE camp_id = @camp_id)
	UPDATE Table_C SET lead_eCPC = @lead_sum / @page_views WHERE camp_id = @camp_id
	SET NOCOUNT OFF
END

Open in new window

0
 

Author Comment

by:BestAviation
ID: 33511260
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33517187
Strange choice.  Multiple SELECT statements over a single UPDATE statement?  Obviously performance is not important to you.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question