Author |
Topic |
immad
Posting Yak Master
230 Posts |
Posted - 2013-06-04 : 05:12:24
|
how to calculate two columns from different row for examplethis is actual datadate--------------------------spendtime2013-01-08 00:00:00.000-----03:20:00--first row2013-01-08 00:00:00.000-----01:19:00--second row2013-01-09 00:00:00.000-----04:33:00--third rowthis is what i want-------------date------------spendtime2013-01-08 00:00:00.000-----04:39only that date 2013-01-08 00:00:00.000 spend time calculate and show in another column immad uddin ahmed |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-04 : 05:42:21
|
[code]-- Query to sum up TIME columnDECLARE @tab TABLE( date DATETIME , spendtime TIME)INSERT INTO @tabSELECT '2013-01-08 00:00:00.000', '03:20:00' union allSELECT '2013-01-08 00:00:00.000', '01:19:00' union allSELECT '2013-01-09 00:00:00.000', '04:33:00' /*this is what i want-------------date------------spendtime2013-01-08 00:00:00.000-----04:39*/;WITH CTE AS (SELECT [date], SUM(DATEDIFF(s, 0, spendtime)) TotalSecond FROM @tab GROUP BY [date] HAVING COUNT(*) >1 )SELECT DATE , CONVERT(VARCHAR(10),TotalSecond/3600)+ '.' + CONVERT(VARCHAR(20),TotalSecond%3600/60) + '.' + CONVERT(VARCHAR(20),TotalSecond%3600%60) AS [Time] --Total of TimeFROM CTE;--Alternate is:SELECT DATE, CAST( DATEADD(s, SUM(DATEDIFF(s, 0, spendtime)), 0) AS TIME)FROM @tabGROUP BY DATE[/code]--Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-04 : 06:08:05
|
quote: Originally posted by bandi
-- Query to sum up TIME columnDECLARE @tab TABLE( date DATETIME , spendtime TIME)INSERT INTO @tabSELECT '2013-01-08 00:00:00.000', '03:20:00' union allSELECT '2013-01-08 00:00:00.000', '01:19:00' union allSELECT '2013-01-09 00:00:00.000', '04:33:00' /*this is what i want-------------date------------spendtime2013-01-08 00:00:00.000-----04:39*/;WITH CTE AS (SELECT [date], SUM(DATEDIFF(s, 0, spendtime)) TotalSecond FROM @tab GROUP BY [date] HAVING COUNT(*) >1 )SELECT DATE , CONVERT(VARCHAR(10),TotalSecond/3600)+ '.' + CONVERT(VARCHAR(20),TotalSecond%3600/60) + '.' + CONVERT(VARCHAR(20),TotalSecond%3600%60) AS [Time] --Total of TimeFROM CTE --Chandu
why split it up to components and then concat again? its anyways for a day (date value) so you can very well use dateadd datediff logic as in posted link as its not possible to spend more than 24 hrs on same day.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
immad
Posting Yak Master
230 Posts |
Posted - 2013-06-04 : 06:14:40
|
quote: Originally posted by visakh16 how many times you will ask the same question?What have you understood from solutions provided here?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185683------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
this is another topic and the link that u showing to me is another i want to sum these two rows columnthe link that showed me that problem is solvedimmad uddin ahmed |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-04 : 06:26:53
|
quote: Originally posted by immad
quote: Originally posted by visakh16 how many times you will ask the same question?What have you understood from solutions provided here?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185683------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
this is another topic and the link that u showing to me is another i want to sum these two rows columnthe link that showed me that problem is solvedimmad uddin ahmed
thats good news, but may we ask what you've attempted so far to solve this issue? Can you share your attempt(s)? Since I believe if you've understood the logic of that (earlier) solution, you can solve this problem too.CheersMIK |
 |
|
immad
Posting Yak Master
230 Posts |
Posted - 2013-06-04 : 06:29:38
|
quote: Originally posted by visakh16
quote: Originally posted by bandi
-- Query to sum up TIME columnDECLARE @tab TABLE( date DATETIME , spendtime TIME)INSERT INTO @tabSELECT '2013-01-08 00:00:00.000', '03:20:00' union allSELECT '2013-01-08 00:00:00.000', '01:19:00' union allSELECT '2013-01-09 00:00:00.000', '04:33:00' /*this is what i want-------------date------------spendtime2013-01-08 00:00:00.000-----04:39*/;WITH CTE AS (SELECT [date], SUM(DATEDIFF(s, 0, spendtime)) TotalSecond FROM @tab GROUP BY [date] HAVING COUNT(*) >1 )SELECT DATE , CONVERT(VARCHAR(10),TotalSecond/3600)+ '.' + CONVERT(VARCHAR(20),TotalSecond%3600/60) + '.' + CONVERT(VARCHAR(20),TotalSecond%3600%60) AS [Time] --Total of TimeFROM CTE --Chandu
why split it up to components and then concat again? its anyways for a day (date value) so you can very well use dateadd datediff logic as in posted link as its not possible to spend more than 24 hrs on same day.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
some times employee go out from a factory and come back for exampleemployee come 9:17am and for some reason he timeout 12:37pmthen he come 17:21pm and timeout for go home at 18:40pmso the data display like this--------------date--------------------timein---------------------------timeout--------------------spendtime2013-01-08 00:00:00.000------2013-01-08 09:17:00.000----2013-01-08 12:37:00.000----03:20:002013-01-08 00:00:00.000------2013-01-08 17:21:00.000----2013-01-08 18:40:00.000---------01:19:00i want this--------------date--------------------timein---------------------------timeout--------------------spendtime2013-01-08 00:00:00.000------2013-01-08 09:17:00.000----2013-01-08 12:37:00.000----04:39:002013-01-08 00:00:00.000------2013-01-08 17:21:00.000----2013-01-08 18:40:00.000----immad uddin ahmed |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-04 : 06:37:23
|
quote: Originally posted by immadsome times employee go out from a factory and come back for exampleemployee come 9:17am and for some reason he timeout 12:37pmthen he come 17:21pm and timeout for go home at 18:40pmso the data display like this--------------date--------------------timein---------------------------timeout--------------------spendtime2013-01-08 00:00:00.000------2013-01-08 09:17:00.000----2013-01-08 12:37:00.000----03:20:002013-01-08 00:00:00.000------2013-01-08 17:21:00.000----2013-01-08 18:40:00.000---------01:19:00i want this--------------date--------------------timein---------------------------timeout--------------------spendtime2013-01-08 00:00:00.000------2013-01-08 09:17:00.000----2013-01-08 12:37:00.000----04:39:002013-01-08 00:00:00.000------2013-01-08 17:21:00.000----2013-01-08 18:40:00.000----immad uddin ahmed
I understood thatMy post was not for you. I was just asking on Bandi's suggestionYou could have very well used earlier suggestion itself in the other link for this.Understand and try to learn posted solutions. Dont always rely upon spoonfed answers for every issues. As MIK pointed out you could have easily applied the earlier suggestion here instead of again posting for the same.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-04 : 07:23:07
|
quote: Originally posted by immad
quote: Originally posted by visakh16
quote: Originally posted by bandi
-- Query to sum up TIME columnDECLARE @tab TABLE( date DATETIME , spendtime TIME)INSERT INTO @tabSELECT '2013-01-08 00:00:00.000', '03:20:00' union allSELECT '2013-01-08 00:00:00.000', '01:19:00' union allSELECT '2013-01-09 00:00:00.000', '04:33:00' /*this is what i want-------------date------------spendtime2013-01-08 00:00:00.000-----04:39*/;WITH CTE AS (SELECT [date], SUM(DATEDIFF(s, 0, spendtime)) TotalSecond FROM @tab GROUP BY [date] HAVING COUNT(*) >1 )SELECT DATE , CONVERT(VARCHAR(10),TotalSecond/3600)+ '.' + CONVERT(VARCHAR(20),TotalSecond%3600/60) + '.' + CONVERT(VARCHAR(20),TotalSecond%3600%60) AS [Time] --Total of TimeFROM CTE --Chandu
why split it up to components and then concat again? its anyways for a day (date value) so you can very well use dateadd datediff logic as in posted link as its not possible to spend more than 24 hrs on same day.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
some times employee go out from a factory and come back for exampleemployee come 9:17am and for some reason he timeout 12:37pmthen he come 17:21pm and timeout for go home at 18:40pmso the data display like this--------------date--------------------timein---------------------------timeout--------------------spendtime2013-01-08 00:00:00.000------2013-01-08 09:17:00.000----2013-01-08 12:37:00.000----03:20:002013-01-08 00:00:00.000------2013-01-08 17:21:00.000----2013-01-08 18:40:00.000---------01:19:00i want this--------------date--------------------timein---------------------------timeout--------------------spendtime2013-01-08 00:00:00.000------2013-01-08 09:17:00.000----2013-01-08 12:37:00.000----04:39:002013-01-08 00:00:00.000------2013-01-08 17:21:00.000----2013-01-08 18:40:00.000----immad uddin ahmed
Immad, the solution does make sense and is correct in my openion. In case if its wrong or should be a bit different, then at least you've got an idea how to make necessary changes. Google it and find, and still if you face any issue you can ask for help on that specific part. However the ouput you're looking for does not make sense at all. One of the basic concept in DBMS is that a table's record should provide a complete information. Information is composed of several unit pieces (columns). To that end, when you say that you want this ouput --> representing the total time for a date only against the first row and nothing against the second row. Which logically make no sense - since looking at the corresponding time in and out for the same row, the spent time should have been 3:20 not 4:39. This would have made sense only if you specified the total time against the second row as well. Or to exclude the "in" and "outs" and just day/date level information (this is what the query is doing). CheersMIK |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-04 : 08:11:43
|
quote: Originally posted by immad Dear Sir,i under stand what you r saying actullay i am new in this field that why i am making some mistakes.i search this problem from google.so far thanks for supporting me immad uddin ahmed
That's alright, keep trying and if you face any problem, post it here (but at least after trying once yourself) :)CheersMIK |
 |
|
|