Author |
Topic |
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-13 : 06:58:35
|
I have a table with time1 and time2 datatype is varchar and the time format is hh:mm. I want to put the sum of time1 and time 2 in TOT with same format hh:mm.After this i need the sum of TOT for each IDNeed help. Thanx a lot in advance.ID Date Time 1 Time2 TOT19 10/05/2013 00:00 05:58 19 02/05/2013 01:50 06:24 19 11/05/2013 01:54 06:06 19 07/05/2013 01:50 06:11 19 01/05/2013 00:00 06:00 19 04/05/2013 01:42 05:57 19 05/05/2013 01:46 06:08 19 08/05/2013 01:52 06:13 19 03/05/2013 00:00 06:06 20 10/05/2013 00:00 05:58 20 02/05/2013 01:50 06:24 20 11/05/2013 01:54 06:06 20 07/05/2013 01:50 06:11 20 01/05/2013 00:00 06:00 20 04/05/2013 01:42 05:57 20 05/05/2013 01:46 06:08 20 08/05/2013 01:52 06:13 20 03/05/2013 00:00 06:06 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-13 : 07:02:14
|
[code]SELECT ID,[Date],CONVERT(varchar(8),DATEADD(ss,SUM(DATEDIFF(ss,Time1,Time2)),0),108) AS TOTFROM TableGROUP BY ID,[Date][/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-13 : 07:20:06
|
thanx but i want to put the total of time1 and time 2 in the coloumn TOT.Like ID Time1 Time 2 Tot19 02:00 06:00 08:0019 02:00 07:00 09:00 19 04:00 06:00 10:00Later I want for ID 19 tot hr to be 08:00+09:00+10:00 i.r 27:00ID TOT19 27:00 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-13 : 07:34:07
|
thats exactly what suggestion i gave will do. DO you mean you need to update value back into the table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-13 : 07:41:33
|
yes when i am updating its giving errorupdate Table set TOT=CONVERT(varchar(8),DATEADD(ss,SUM(DATEDIFF(ss,Time1,Time2)),0),108) An aggregate may not appear in the set list of an UPDATE statement. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-13 : 07:46:49
|
in that case do likeUPDATE tSET TOT= TOT1FROM(SELECT TOT,CONVERT(varchar(8),DATEADD(ss,SUM(DATEDIFF(ss,Time1,Time2) OVER (PARTITION BY ID,[Date]),0),108) AS TOT1FROM Table)t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-13 : 07:52:52
|
Getting error"DATEDIFF" is not a valid windowing function, and cannot be used with the OVER clause. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-13 : 07:59:15
|
quote: Originally posted by pnasz Getting error"DATEDIFF" is not a valid windowing function, and cannot be used with the OVER clause.
missed a bracesUPDATE tSET TOT= TOT1FROM(SELECT TOT,CONVERT(varchar(8),DATEADD(ss,SUM(DATEDIFF(ss,Time1,Time2)) OVER (PARTITION BY ID,[Date]),0),108) AS TOT1FROM Table)t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-13 : 08:16:38
|
After running the query not getting the correct value in TOTthis is what i am gettingID Date Time1 Time2 TOT19 01/05/2013 00:00 06:00 06:00:0019 02/05/2013 01:50 06:24 04:34:0019 03/05/2013 00:00 06:06 06:06:0019 04/05/2013 01:42 05:57 04:15:0019 05/05/2013 01:46 06:08 04:22:0019 06/05/2013 01:47 06:04 04:17:0019 07/05/2013 01:50 06:11 04:21:0019 08/05/2013 01:52 06:13 04:21:0019 09/05/2013 02:08 06:17 04:09:0019 10/05/2013 00:00 05:58 05:58:0019 11/05/2013 01:54 06:06 04:12:0019 12/05/2013 01:46 06:13 04:27:00 |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-13 : 08:18:22
|
data type of Time1 and time 2 is varchar |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2013-05-13 : 22:22:07
|
like this?update @aset TOT = dateadd(s, datediff(s, cast('00:00' as time), cast(Time2 as time)), cast(Time1 as time)) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 00:14:33
|
[code]UPDATE tSET TOT= TOT1FROM(SELECT TOT,CONVERT(varchar(8),DATEADD(ss,SUM(DATEDIFF(ss,0,Time1)+DATEDIFF(ss,0,Time2)) OVER (PARTITION BY ID,[Date]),0),108) AS TOT1FROM Table)t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2013-05-14 : 01:57:06
|
Its working. Thanx a lot for help |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-14 : 02:12:06
|
quote: Originally posted by pnasz Its working. Thanx a lot for help
Which one? mine or other suggestion?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|