Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear sir,

i have two column i.e Hour and minute in datagrid. i want to disaply sum of hour and minutes.

EX- suppose i have entered in one column 2 Hour and 59 Minute and another column 2 Hour and 59 minutes. result should come Total 6.58(Means 6 hour and 58 minute). i need SQL queries.
Posted

Try:
SQL
SELECT  CAST((SUM(HOURS)*60+SUM(MINS))/60 as float) + 
       (CAST((SUM(HOURS)*60+SUM(MINS))%60 as float)) / 100 
FROM MyTable
WHERE ...



I have tried again. i am not able to find out any difference in my code which one i posted. If you dont mind,please can you rectify my code and reply me.


Look at your code.
Your original question was about adding hours and minutes in two columns, and converting teh result to a float.

If what you are trying to do is add two hour columns to two minute columns, and return that as a float *for each row* then it is different: you need to convert the hours to minutes, add them together with the other two minutes, and convert the result back to hours and minutes, then convert that to a float.
So, you don't need SUM at all:
SQL
SELECT  CAST((Effort1*60 + Effort2 * 60 + Effort1Minutes + Effort2Minutes) / 60 as float) + 
       (CAST((Effort1*60 + Effort2 * 60 + Effort1Minutes + Effort2Minutes) % 60 as float)) / 100 
FROM MyTable
WHERE ...
See what I mean?

If you don't tell us what you want to start with, then you have to think about the answer we give rather than grab bits of it and hope! :laugh:
 
Share this answer
 
v2
Comments
kamalsekhar 25-Nov-13 12:45pm    
This above queries which one you posted, that is not giving correct result.i want to display like this 5 Hour 58 minutes + 7 Hour 58 minutes=13 hour 56 minutes(answer should). but your queries giving 15.09 .please reply me with any other solution.(here Effort1 is 5 Hour column,Effort1Minutes is 58 minute column and same Effort2 is 7 Hour column,Effort2Minutes is 58 minute column

select
CAST((SUM(Effort1)*60+SUM(Effort1Minutes))/60 as float) +
(CAST((SUM(Effort1)*60+SUM(Effort1Minutes))% 60 as float)) / 100 +

CAST((SUM(Effort2)*60+SUM(Effort2Minutes))/60 as float) +
(CAST((SUM(Effort2)*60+SUM(Effort2Minutes))% 60 as float)) / 100

from TimesheetDetails
OriginalGriff 25-Nov-13 14:25pm    
Well, that's interesting, because when I try it here, I get 13.56 - which is exactly what I would expect. But then, what I wrote and what you wrote are significantly different.
Since you have taken elements of my code, not bothered to understand them and bolted them together in the hope it might work, I can see exactly what your mistake is. And so will you if you look at the difference between your code and mine...and think for a bit. 30 seconds should do it, it's pretty obvious!
kamalsekhar 26-Nov-13 0:11am    
Dear sir,
I have tried again. i am not able to find out any difference in my code which one i posted. If you dont mind,please can you rectify my code and reply me.


Thanks & Regards
Kamal
OriginalGriff 26-Nov-13 4:58am    
Answer updated.
2 Hour 59 minutes + 2 Hour 59 minutes = not 6 Hour 58 minutes
the answer is 5 hour 58 minutes

Similar question has been answered 2 days ago, check it out
How to add minute into hours when minute is exceeding 60 minutes[^]

In SQL, you have aggregate functions like SUM, etc., using that you could calculate.

And you didn't show how are you saving the data in columns. If you stored like 2.59, then you have to split the values( 2 , 59) & convert the hours to minutes & add it to remaining minutes then sum two columns minutes values. Finally generate Hours - minutes format values from final minutes total.
 
Share this answer
 
Comments
kamalsekhar 25-Nov-13 12:47pm    
I have written one queries.but it not giving correct answer.i want to display like this 5 Hour 58 minutes + 7 Hour 58 minutes=13 hour 56 minutes(answer should). but below queries giving 15.09 .please reply me with any other solution.(here Effort1 is 5 Hour column,Effort1Minutes is 58 minute column and same Effort2 is 7 Hour column,Effort2Minutes is 58 minute column

select
CAST((SUM(Effort1)*60+SUM(Effort1Minutes))/60 as float) +
(CAST((SUM(Effort1)*60+SUM(Effort1Minutes))% 60 as float)) / 100 +

CAST((SUM(Effort2)*60+SUM(Effort2Minutes))/60 as float) +
(CAST((SUM(Effort2)*60+SUM(Effort2Minutes))% 60 as float)) / 100

from TimesheetDetails

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900