Solved

Get the max Time

Posted on 2009-04-15
3
171 Views
Last Modified: 2013-11-25
Hi

I have a query that inserts and updates all records from a temp table.

The problem I am having is that there are some fields that are identical, the only thing that makes them different is the time field.

I want to update all records with the latests time field from my temp table to my live table

The query I am running is below, as you can see the time is a varchar and then I update it to a date and time when I import the data into the table

CREATE PROCEDURE sp_InsertNAVData
	@CreateDate as DateTime
AS
 
BEGIN
  SET NOCOUNT ON
 
	---Update the NAV Information if it exists already overweite NAV, Shares, Price ---
	UPDATE  tblClassNAV 
	
		SET 
			tblClassNAV.TotalNAV = NTemp.NAV,
			tblClassNAV.TotalShares = NTemp.Quantity,
			tblClassNAV.SharePrice = NTemp.UnitPrice,
			tblClassNAV.ClassCCY = NTemp.ShareCCY,
			tblClassNAV.ClassUnitPrice = NTemp.UnitPrice_CCY,
			tblClassNAV.ClassRate = NTemp.FXRate,
			tblClassNAV.CCY = NTemp.CCY,
			tblClassNAV.Consolidated = NTemp.Consolidated,
			tblClassNAV.FundName = NTemp.FundName,
			tblClassNAV.ISIN = NTemp.ISIN,
			tblClassNAV.PreviousPrice = NTemp.PreviousPrice,
			tblClassNAV.TNA = NTemp.TNA,
			tblClassNAV.UpdatedDate = @CreateDate
 
 
		FROM 
			tblNAV_Temp AS NTemp 
			INNER JOIN tblClassNAV  
			ON NTemp.FundCode = tblClassNAV.FCode
			AND NTemp.CShare = tblClassNAV.FSCode
			AND NTemp.NAVDate = tblClassNAV.NAVDATE
                                      
	INSERT INTO 
			tblClassNAV 
			(FCode, FSCode, NAVTime, NAVDate, MonthID, TotalNAV, TotalShares, SharePrice, 
			ClassCCY, ClassUnitPrice, ClassRate, CCY, Consolidated, 
			FundName, ISIN, PreviousPrice, TNA, CreateDate)
	
	SELECT 	DISTINCT
			tblNAV_Temp.FundCode, 
			tblNAV_Temp.CShare, 
			Cast(tblNAV_Temp.NAVDate + Case when Len(Cast(NAVTime as varchar(6)))=6 Then 
			Left(Cast(NAVTime as varchar(6)),2)
			+':'+ Right(Cast(NAVTime as varchar(4)),2)
			+':'+ Right(Cast(NAVTime as varchar(6)),2)		
			END AS DateTime) as 'NAVTime',
			tblNAV_Temp.NAVDate, 
			tblNAV_Temp.MonthID,
			tblNAV_Temp.NAV,
			tblNAV_Temp.Quantity,
			tblNAV_Temp.UnitPrice,
			tblNAV_Temp.ShareCCY,
			tblNAV_Temp.UnitPrice_CCY,
			tblNAV_Temp.FXRate,
			tblNAV_Temp.CCY, 
			tblNAV_Temp.Consolidated, 
			tblNAV_Temp.FundName, 
			tblNAV_Temp.ISIN, 
			tblNAV_Temp.PreviousPrice, 
			tblNAV_Temp.TNA,
			CreateDate = @CreateDate
 
	FROM  
		tblNAV_Temp 
	WHERE 	
	 	NOT EXISTS 
	(SELECT * FROM 
		tblClassNav
 	WHERE 
		tblNAV_Temp.FundCode = tblClassNAV.FCode 
	AND 	tblNAV_Temp.CShare = tblClassNAV.FSCode
	AND	tblNAV_Temp.NAVDate = tblClassNAV.NAVDate)
	
END 
 
GO

Open in new window

0
Comment
Question by:ITHELPME
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
bprojoe earned 125 total points
ID: 24148406
After looking at your code and comments I would suggest that the issue is the database structure.  In order to efficiently update related records and add new ones you should have some kind of primary key(s).  If you are linking with three fields and you get identical records then you probably need to add a primary key field on each table.  This way when you do your insert, any record in the temp table that doesnt have a corresponding key in the live table is a new record.  Just remember to use identity insert when doing the insert so to keep the original key from the temp table.
0
 

Author Comment

by:ITHELPME
ID: 24148745
Thanks for your comment,
I have the primary keys set FundCode, ShareCode and NAVDate this is within my join

The insert part of it works it is not linking when I want to do an update.

I have tried the below, this gives me the correct result I am looking for but when I put the full thing together in the update section it will not get me the max time only I think it is because the prices are different

How do I get around this if some fields are different, I only want it to select the distinct values where there are more than one use the Time field to get the latest


SELECT  
			tblNAV_Temp.FundCode,
			tblNAV_Temp.CShare,
			tblNAV_Temp.CCY,
			tblNAV_Temp.Consolidated, 
			tblNAV_Temp.FundName,
			tblNAV_Temp.NAVDate,
			tblNAV_Temp.MonthID,
			tblNAV_Temp.ISIN,
			tblNAV_Temp.PreviousPrice,
			--tblNAV_Temp.Quantity,
			--tblNAV_Temp.UnitPrice,
			--tblNAV_Temp.NAV,
			--tblNAV_Temp.TNA,
			MAX(tblNAV_Temp.NAVTime) as 'NAV Time'
				
		FROM  
			tblNAV_Temp
		group by 
			tblNAV_Temp.FundCode, 
			tblNAV_Temp.CShare,
			tblNAV_Temp.CCY,
			tblNAV_Temp.Consolidated, 
			tblNAV_Temp.FundName,
			tblNAV_Temp.NAVDate,
			tblNAV_Temp.MonthID,
			tblNAV_Temp.ISIN,
			tblNAV_Temp.PreviousPrice

Open in new window

0
 

Author Closing Comment

by:ITHELPME
ID: 31570372
Thank you for your help
The problem was there was a trigger which was preventing the update.  Once I deleted the trigger it all worked fine, I had tested it on the test server and all worked from there but triggers do not copy accross when you copy the table, learn something new every day, thanks again
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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