|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with my insert statement
I need an insert statement that will grab the maximum of TimeGenerated from the Events table and insert it into LastEventTime in the Host Table. But it has to be inserted based on the relationship of SystemNetName.
For Example, Host Table -------------------------------------- SystemNetName LastEventTime Tweety 9/24/2004 Sylvester 9/25/2004 Events Table ---------------------------------------- SystemNetName TimeGenerated Tweety 9/24/2004 Tweety 8/26/2004 Sylvester 8/21/2004 Sylvester 9/25/2004 Sylvester 7/5/2004 This is what I had so far but didn't work: Insert into host(LastEventTime) Select(Max(TimeGenerated) ) from Events Where Events.SystemNetName = Host.SystemNetName Any Ideas? |
|
#2
|
||||
|
||||
|
Something like this perhaps
Code:
INSERT INTO HostTable ( SystemNetName, LastEventTime ) SELECT SystemNetName, MAX(TimeGenerated) FROM EventsTable GROUP BY EventsTable.SystemNetName
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month Looking for a perl job with kick-*** programmers in a well-known NASDAQ listed tech company with branches in the US and Europe? We're hiring. PM me for details. Requirements |
|
#3
|
|||
|
|||
|
I tried it and it didn't work. Primarily, I can't insert SystemNetName into the Host table because Host.SystemNetName is a primary key and Events.SystemNetName is the foreign key.
I pulled out the SystemNetName out of the 'Insert' and 'Select' and in the 'from' I did a left join on SystemNetName. Like this: Code:
INSERT INTO Host (LastEventTime)
SELECT MAX(Events.TimeGenerated) AS Expr1
FROM Events LEFT OUTER JOIN
Host ON Host.SystemNetName = Events.SystemNetName
GROUP BY Events.SystemNetName
Logically it makes sense but I am getting an error: Cannot insert the value NULL into column 'SystemNetName' Yet, I am not trying to insert into the SystemNetName column. Thanks, Laura |
|
#4
|
|||
|
|||
|
Laura,
What's you're reasoning in using an Insert? If there are new times for say "Tweety" always coming up, then you're trying to only keep 1 record for that NetName in your table, and simply "insert" the most recent date for that name, no? Sounds like you should be using an Update instead. Else, I think you have a flaw in the table design. |
|
#5
|
|||
|
|||
|
Yeah, I agree with you. But, don't I still need to insert the most recent time from the Events table into the Host table intially? Then after, I would do an update, correct?
The Events table keeps track of every event that occurs and never gets an update, just an insert and that works fine. Now, it is a matter of getting the most recent time and sticking it into the Host table, then from there the Host table is updated. -Laura |
|
#6
|
|||
|
|||
|
Actually, I don't think I even need to have a LastEventTime field in the Host table. I am creating a web application and it might just be better to sql the Events table for the most recent time of each SystemNetName instead of doing an insert.
Thanks everybody. -Laura |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Need help with my insert statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|