Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Find a record
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01287531
Message ID:
01287590
Views:
18
Try (not tested)
SELECT	@Count = COUNT(*),
	@MinTime = MIN([Clock]),
	@MaxTime = Max([Clock]) 
FROM	[MyTable]  
WHERE	[Fk] = @fk AND
	YEAR([Clock])  = YEAR(@clock) AND
	MONTH([Clock]) = MONTH(@clock) AND
	DAY([Clock])   = DAY(@clock) 

IF @Count < 2 BEGIN
	PRINT 'INSERT'
END ELSE IF (@clock BETWEEN @MinTime AND @MaxTime) BEGIN 
    --- DO NOTHING 
END ELSE
    BEGIN
        UPDATE [MyTable] SET ....
               FROM [MyTable] 
        INNER JOIN (SELECT TOP 1 [PK] FROM [MyTable] 
                     	   WHERE [FK]    = @fk AND
                                 [Clock] = CASE WHEN @clock < @MinTime
                                                THEN @MinTime
                                                ELSE @MAxTime END
                           ORDER BY [Fk]) Tbl2
         ON [MyTable].[Fk] = [Tbl2].[PK]
    END
END
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform