February 22, 2011 at 9:20 am
Hi guys,
I am trying to get result from following table where sensorID = j-2933 and result should contain rows only where datetime difference is greater than 5 Minutes
here is sample data
SET DATEFORMAT MDY
Create Table [dbo].[TempByVolume] (
[DateTime] datetimenot null,
SensorID varchar(6) not null,
Temperature float not null,
Volume float not null)
INSERT INTO TempByVolume
SELECT '9/28/2010 7:34:24 PM', 'J-2933', 44.673, 590.23 UNION ALL
SELECT '9/28/2010 7:35:29 PM', 'J-2933', 44.411, 590.02 UNION ALL
SELECT '9/28/2010 7:36:30 PM', 'J-2933', 44.954, 590.33 UNION ALL
SELECT '9/28/2010 7:38:33 PM', 'J-2933', 45.723, 591.04 UNION ALL
SELECT '9/28/2010 7:40:31 PM', 'J-2933', 47.498, 591.22 UNION ALL
SELECT '9/28/2010 7:42:34 PM', 'J-2933', 51.266, 591.41 UNION ALL
SELECT '9/28/2010 7:44:37 PM', 'M-7341', 87.455, 780.44 UNION ALL
SELECT '9/28/2010 7:44:38 PM', 'J-2933', 54.447, 591.61 UNION ALL
SELECT '9/28/2010 7:46:37 PM', 'M-7341', 85.731, 780.26 UNION ALL
SELECT '9/28/2010 7:46:24 PM', 'J-2933', 44.673, 590.23 UNION ALL
SELECT '9/28/2010 7:48:29 PM', 'J-2933', 44.411, 590.02 UNION ALL
SELECT '9/28/2010 7:50:30 PM', 'J-2933', 44.954, 590.33 UNION ALL
SELECT '9/28/2010 7:52:33 PM', 'J-2933', 45.723, 591.04 UNION ALL
SELECT '9/28/2010 7:54:31 PM', 'J-2933', 47.498, 591.22 UNION ALL
SELECT '9/28/2010 7:56:34 PM', 'J-2933', 51.266, 591.41 UNION ALL
SELECT '9/28/2010 7:57:37 PM', 'M-7341', 87.455, 780.44 UNION ALL
SELECT '9/28/2010 7:58:38 PM', 'J-2933', 54.447, 591.61
How this can be accomplished??
I appreciate in Advance
February 22, 2011 at 11:52 am
Based on your sample data there are no consecutive rows for the sensorID in question with the required 5min gap.
I think it would help a lot if you could post your expected result based on your sample data so we can eliminate any misinterpretation...
February 22, 2011 at 1:07 pm
Thanks for your response,
I am using SQL 2005
here are the results i am trying to get
datetime SensorID Temperature Volume
2010-09-28 19:34:24.000J-293344.673590.23
2010-09-28 19:40:31.000J-293347.498591.22
2010-09-28 19:46:24.000J-293344.673590.23
2010-09-28 19:52:33.000J-293345.723591.04
2010-09-28 19:58:38.000J-293354.447591.61
The time difference in above rows is 5min or more.
after first row it skiped 19:35:29, 19:36:30, 19:38:33 and gets 19:40:31 and again it skiped results between 19:40:41 and 19:46:24 and so on
February 22, 2011 at 2:15 pm
All I could come up with is using the "Quirky Update" method:
You'd need to add another column to your table or copy the data into a temp table if you're not allowed to modify the original table structure.
It's important to read the article refernced by the link in the code together with the related discussion.
DECLARE @Quirky TINYINT ,
@Sequence INT,
@SensorId CHAR(6),
@Date DATETIME;
SET @Sequence = 0
SET @Quirky = NULL
-- This form of the UPDATE statement has some particular rules.
-- See Jeff Moden's article at http://www.sqlservercentral.com/articles/T-SQL/68467/
-- for a complete discussion of how this works, and all of the rules for utilizing it.
;WITH SafetyCheck AS
(
SELECT
sensorid,
[DATETIME],Quirky,
SEQUENCE = ROW_NUMBER() OVER (ORDER BY sensorid,[DATETIME])
FROM TempByVolume
)
UPDATE t
SET
@Sequence = CASE WHEN SEQUENCE = @Sequence + 1 THEN @Sequence + 1 ELSE 1/0 END,
@Date =
CASE
WHEN DATEDIFF(mi,ISNULL(@Date,'19000101'),[DATETIME])>5 OR SensorId<> @SensorId
THEN [DATETIME]
ELSE @Date END,
@Quirky = Quirky = CASE WHEN @Date=[DATETIME] THEN 1 ELSE 0 END,
@SensorId=SensorId -- ANCHOR COLUMN
FROM SafetyCheck t WITH (TABLOCKX)
OPTION (MAXDOP 1);
SELECT *
FROM TempByVolume
WHERE quirky = 1
ORDER BY SensorId,[DATETIME];
Edit: It's not recommended to use keywords as column names... So you might wan to rename your DATETIME column...
February 22, 2011 at 3:10 pm
u sure its running on your machine? giving errors here....
the article you mentioned is a long but very informative, i really need to understand this... thanks for mentioning this article
February 22, 2011 at 3:27 pm
gemni7 (2/22/2011)
u sure its running on your machine? giving errors here....the article you mentioned is a long but very informative, i really need to understand this... thanks for mentioning this article
It's hard to help without the detailed error message... But I'd expect you didn't add the Quirky column as mentioned...
February 23, 2011 at 7:02 am
yes, my fault i forget to add quirky
I really appreciate your help
February 23, 2011 at 7:12 am
You are the SQL man Lutz, you saved me from a real big hassel
I thank you and appreciate your great help
rgds,
gem
February 23, 2011 at 12:01 pm
My pleasure. 😀
But all the honor belongs to Jeff for posting this solution and continuously improving it.
As a side note: do you completely understand how it works and when you should not use this concept? It's important only to use code in production you're completely able to support.
March 4, 2011 at 10:42 am
Hi LutzM,
I have a question about Exclusive Table Lock. What would be SQL servers behavior if my application open connection to insert data into same table while exclusive table lock is active. would sql server hold the query until exclusive lock ends or it return error?
so far I havent seen any error.
March 4, 2011 at 11:02 am
gemni7 (3/4/2011)
Hi LutzM,I have a question about Exclusive Table Lock. What would be SQL servers behavior if my application open connection to insert data into same table while exclusive table lock is active. would sql server hold the query until exclusive lock ends or it return error?
so far I havent seen any error.
That either means you have a high value for LOCK_TIMEOUT (or -1 which means "wait forever") or Jeff's code is really fast.
I'd bet money on the latter. 😀
To answer your original question: Your app will wait until the exclusive lock is released. The time it will wait is controlled by SET LOCK_TIMEOUT. Please see BOL for details and some possible side-effects.
But again, there are at least three reasons Jeffs code is recommended for such scenarios: speed, performance and duration 😀
March 4, 2011 at 5:49 pm
gemni7 (3/4/2011)
Hi LutzM,I have a question about Exclusive Table Lock. What would be SQL servers behavior if my application open connection to insert data into same table while exclusive table lock is active. would sql server hold the query until exclusive lock ends or it return error?
so far I havent seen any error.
How many rows in the table?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2011 at 5:50 pm
LutzM (3/4/2011)
gemni7 (3/4/2011)
But again, there are at least three reasons Jeffs code is recommended for such scenarios: speed, performance and duration 😀
Heh... that's a nice thing to say, Lutz. Thanks. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2011 at 6:03 pm
Jeff, got my mail?? (Gentle reminder to Jeff as his PM box if full; apologies for spamming in other's thread :pinch:)
March 5, 2011 at 9:46 am
I did get your email and I responded this morning.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply