Using T-SQL, how do you find the sum of DateTime values for x amount of records?
Example:
id(int) | name(varchar) | TimeInWomb(datetime)
---------------------------------------
id1 | name1 | TiW1
id2 | name2 | TiW2
id3 | name1 | TiW3
id4 | name1 | TiW4
. | . | .
. | . | .
. | . | .
I want to find the sum of the TimeInWomb’s where name = name1.
Thanks.
EDIT:
I admit, in hindsight that was a pretty bad example which I’ve now changed. By “sum” I mean the sum of the hours/days/weeks/months of all the values where name = name1.
ANSWER:
You can’t sum datetimes.
I noticed in your edit that you changed your “date of birth” column to “time in womb”. Those are two very different concepts. “date of birth” is a SINGLE point in time, where “time in womb” is a DURATION of time. Which of these two concepts are you trying to represent?
DATETIMEcolumn.DATETIMEis used to refer to a single point in time. You should either have twoDATETIMEcolumns (a start and an end), or you should have number column (number of seconds, or something similar).Either way something is conceptually wrong here and we can’t give you an answer until you get things sorted out. Some sample data with expected output would help dramatically.
Edit:
Since you’re trying to represent a duration of time (the time in the womb), then you need to either have two
DATETIMEcolumns (time of conception and time of birth, for example) or a single number column (seconds in womb, for example). The second option is easier to work with, but the first option gives you more flexibility.Let’s assume that you’ve got a
SecondsInWombcolumn that is a number. The answer to your original question would be:That will tell you the total amount of seconds spent in the womb for that name.