Solved

Insert/Update records and calculating column based on Row_Number()

Posted on 2009-05-05
6
686 Views
Last Modified: 2012-05-06
Hi,

I have have two tables Tbl1 has Id, CalcFld. and Tbl2 that has Id, Value, Date.  For a given sets of Tbl2.Date in Tbl2 I want to set the Tbl1CalcFld = Tbl1.CalcFld + Tbl2.Value if it is an update and if it is an insert the nTbl1.CalcFld = Tbl2.Value.

I can get one or the other to work correctly only once and then it seems to just add 1 to the Tbl1.CalcFld if the Tbl2.Id value alread exist in Tbl1.

What am I missing?

Thanks,
John
UPDATE Tbl1
SET CalcFld = (select row_Number() over(ORDER by Value DESC) as Row
					from Tbl2 where tbl2.Id = Tbl1.Id and Tbl2.Date = '4/1/09') 
from Tbl1
IF @@ROWCOUNT = 0
BEGIN
    INSERT Tbl1 (Id, CalcFld)
	select Id, row_Number() over(ORDER by Value DESC) as Row
	from Tbl2
	where Tbl2.Date = '4/1/09' 
END

Open in new window

0
Comment
Question by:JohnJMA
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24308834
update t1
Set CalcFld  = t1.CalcFld + t2.Value
FROM tbl1 t1
INNER JOIN tbl2 t2 on t1.id = t2.id
WHERE t2.date = '4/1/09'
IF @@ROWCOUNT = 0
      INSERT Tbl1 (Id, CalcFld)
      SELECT id, Value
     FROM tbl2 where date = '4/1/09'
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24308889
I would do like this, instead of testing row count at the end of update.
INSERT Tbl1 (Id, CalcFld)
SELECT DISTINCT Id, 0
FROM Tbl2
WHERE Tbl2.Date = '4/1/09' 
AND NOT EXISTS (SELECT null FROM Tbl1 WHERE Tbl1.Id = Tbl2.Id)
 
UPDATE t1
SET t1.CalcFld = t1.CalcFld + t2.[Value]
FROM Tbl1 t1
INNER JOIN (
	SELECT Id, SUM([Value]) AS [Value]
	FROM Tbl2
	WHERE Date = '4/1/09'
	GROUP BY Id 
) t2 ON t1.Id = t2.Id

Open in new window

0
 

Author Comment

by:JohnJMA
ID: 24310690
Hi mswvisa1,
Thanks for the help.  Your UPDATE process works great and I am able to get the Row_Count() added to the CalcFld.  
When I run the INSERT section I get the following error that has me stumped:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Any idea what is causing the problem?
Thanks,
  • John
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.

 

Author Comment

by:JohnJMA
ID: 24310717
Hi aneeshattingal,
I can get your solution to work the way it is but I can't figure out how to incorporate a SELECT statement so that the value of tbl2.is actually the vlue of the Row_Count() of each row in Ttbl2.
Thanks,
John
0
 

Author Closing Comment

by:JohnJMA
ID: 31578208
Hi mswvisa1,

Thanks for the help.  I figured out the error message,  I had a typo in my INSERT statement, which makes everything in your solution work great.  By the way your solution is easy to incorporate into other calculations and INSERT/UPDATE routines that I will need to do.

Thanks again,
John
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24313680
Glad you found the issue and it worked for you.

Best regards,
Kevin
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

Suggested Solutions

Title # Comments Views Activity
T-SQL: "HAVING CASE" Clause 1 25
[SQL server / powershell] bulk delete table from CSV 8 33
Query to capture 5 and 9 digit zip code? 4 20
Create snapshot on MSSQL 2012 3 18
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

777 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