Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 updating a record with the results of a query

Author  Topic 

finalmatt
Starting Member

6 Posts

Posted - 2010-08-25 : 02:18:33
hi, i have a query, well a sp, that i am trying to run to update the contents of a table by compairing two timestamps with a date diff. for the life of me i cannot get this sp to work correctly. I can run the query to return the results i want but not to do the update and record them to another col...

this is the functional query:-
SELECT     CONVERT(float, DATEDIFF(ms,
(SELECT MAX(_TIMESTAMP) AS Expr1
FROM [m_data_aq].[dbo].t_idi_37
WHERE (_VALUE = 1) AND (_TIMESTAMP < T ._TIMESTAMP)), _TIMESTAMP)) / 1000 AS cycle_time
FROM [m_data_aq].[dbo].t_idi_37) AS T
WHERE (_VALUE = '1')


this is the sp i am trying to create to make the updates i require

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_insert_idi_10_data]

as UPDATE t_idi_37
SET cycle_time =
(SELECT CONVERT(float, DATEDIFF(ms,
(SELECT MAX(_TIMESTAMP) AS Expr1
FROM [m_data_aq].[dbo].t_idi_37
WHERE (_VALUE = 1) AND (_TIMESTAMP < T ._TIMESTAMP)), _TIMESTAMP)) / 1000 AS cycle_time
FROM [m_data_aq].[dbo].t_idi_37 AS T
WHERE (_VALUE = '1'))



when executing the sp i get the following result/error

Msg 512, Level 16, State 1, Procedure sp_insert_idi_10_data, Line 3
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

(0 row(s) affected)

(1 row(s) affected)


now this is probably a bit strong for my sql skills, maybe not 5 years ago but not touched it since so to say i am rusty is an understatement!!!

If anyone can point me in the right direction i would be really greatful!

here by the way this is a sample of the data set i am working with

[URL=http://img192.imageshack.us/i/tableik.jpg/][/URL]

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-08-25 : 02:49:42
Your subquery is returning more than one values like the message says. So sql server doesn't know what row to pick up to update t_idi_37.cycle_time.
Go to Top of Page

finalmatt
Starting Member

6 Posts

Posted - 2010-08-25 : 03:33:14
well i suppose it will the table contains thousands of rows. i am obviously going about the update completely wrong... how could i get it to cycle the table and do the comparison and update?!?!
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-08-25 : 04:07:56
You are probably missing a join condition. Without knowing your data, It'd be hard to tell exactly. Why don't you post some sample data and desired output.
Go to Top of Page

finalmatt
Starting Member

6 Posts

Posted - 2010-08-25 : 04:23:15
ok lets try... this is the data in the table. i have manually entered the cycle_time to show what i am trying to get to - basically want to enter the datediff in ms between the rows that have a value of 1. hope this makes sense!!!

ID _Name _Value _Timestamp _Quality Cycle_time
79161 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:19:24 192 NULL
79162 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:19:33 192 NULL
79163 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:19:36 192 NULL
79164 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:19:42 192 9.32
79165 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:19:43 192 NULL
79166 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:19:46 192 4.44
79167 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:19:49 192 NULL
79168 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:19:58 192 12.12
79169 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:20:02 192 NULL
79170 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:20:08 192 10.52
79171 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:20:08 192 NULL
79172 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:20:11 192 3.01
79173 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:20:15 192 NULL
79174 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:20:24 192 13.09
79175 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:20:27 192 NULL
79176 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:20:33 192 9.87
79177 Dispatch.Dispatch.MSK Vented 0 24/08/2010 15:20:34 192 NULL
79178 Dispatch.Dispatch.MSK Vented 1 24/08/2010 15:20:37 192 4.54
Go to Top of Page

finalmatt
Starting Member

6 Posts

Posted - 2010-08-25 : 04:26:59
the standard view i use now shown in my first post return the data as follows and is basically what i want to be recording to the table in the cycle_time field!

time_stamp	cycle_time	_value
24/08/2010 13:40:16 NULL 1
24/08/2010 13:40:36 20.31 1
24/08/2010 13:40:56 19.8 1
24/08/2010 13:41:27 31.013 1
24/08/2010 13:41:54 27.78 1
24/08/2010 13:42:08 13.673 1
24/08/2010 13:42:45 36.42 1
24/08/2010 13:42:50 5.61 1
24/08/2010 13:42:54 4.03 1
24/08/2010 13:42:58 3.47 1
24/08/2010 13:43:22 24.653 1
24/08/2010 13:43:28 5.72 1
24/08/2010 13:43:32 3.953 1
24/08/2010 13:44:16 44.343 1
24/08/2010 13:44:30 13.733 1
24/08/2010 13:44:54 23.733 1
24/08/2010 13:45:07 13.656 1
24/08/2010 13:45:39 31.156 1
24/08/2010 13:45:44 5.67 1
24/08/2010 13:45:48 4.016 1
24/08/2010 13:45:52 3.25 1
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-08-25 : 04:37:03
can you try this... I don't have sql server installed to check :(

update T
SET T.cycle_time = DATEDIFF(ms,(select MAX(b._TIMESTAMP) FROM [m_data_aq].[dbo].t_idi_37 b where b._TIMESTAMP<T._TIMESTAMP and b._VALUE=1),T._TIMESTAMP)/1000
from [m_data_aq].[dbo].t_idi_37 T
WHERE T._VALUE = '1'
Go to Top of Page

finalmatt
Starting Member

6 Posts

Posted - 2010-08-25 : 04:56:27
I am impressed! from your head without sql to test that ran straight of!

result set is :-

id	_name				_value	_timestamp		_quality	cycle_time
80456 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:47:37 192 NULL
80457 Dispatch.Dispatch.MSK Vented 0 25/08/2010 09:47:38 192 NULL
80458 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:47:41 192 3
80459 Dispatch.Dispatch.MSK Vented 0 25/08/2010 09:47:42 192 NULL
80460 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:47:45 192 3
80461 Dispatch.Dispatch.MSK Vented 0 25/08/2010 09:47:49 192 NULL
80462 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:48:00 192 14
80463 Dispatch.Dispatch.MSK Vented 0 25/08/2010 09:48:03 192 NULL
80464 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:48:13 192 13
80465 Dispatch.Dispatch.MSK Vented 0 25/08/2010 09:48:17 192 NULL
80466 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:48:46 192 33
80467 Dispatch.Dispatch.MSK Vented 0 25/08/2010 09:48:50 192 NULL
80468 Dispatch.Dispatch.MSK Vented 1 25/08/2010 09:48:52 192 5


The cycle time is correct except its returned the whole number whereas before it was converted to a float to get the cycle time in with ms. so 20 s 13ms 20.13. i have tried adding the convert float in but am cocking that up too lol!!!!
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-08-25 : 05:00:33
try making that 1000.0 instead of 1000 in the query.
Go to Top of Page

finalmatt
Starting Member

6 Posts

Posted - 2010-08-25 : 05:22:05
spot on! thanks very much! i have been struggling with this for ages!!!!!!!!
Go to Top of Page
   

- Advertisement -