I am trying to sum the hours from a datetime column in a sql but I am getting an error when I try this:
SELECT
DATEADD(HH, SUM(DATEDIFF(HH, '00:00:00.000', myDate)), '00:00:00.000') as Total_Hours
FROM myTable
WHERE ID in (1, 2)
My date column looks like this
2015-02-18 00:00:00.000
2015-02-18 00:30:00.000
2015-02-18 01:00:00.000
2015-02-18 01:30:00.000
How can I sum and only show the total hours with out the date?
Perhaps you want to extract the hours and add them up? The conversion back to a data is definitely unnecessary:
SELECT SUM(DATEPART(hour, myDate)) as Total_Hours
FROM myTable
WHERE ID in (1, 2);
EDIT;
If you want to add the time component, then use minutes or seconds:
SELECT SUM(DATEPART(second, myDate)) / (60.0 * 60.0) as Total_Hours
FROM myTable
WHERE ID in (1, 2);
I get the impression that you want to add the time parts together:
select Total_Hours = datepart(hour, cast(myDate as time))
from myTable
where id in (1, 2)
Related
let's assume that I have a table with entries and these entries contains timestamp column (as Long) which is telling us when that entry arrived into a table.
Now, I want to make a SELECT query, in which I want to know how many entries came in selected interval with concrete frequency.
For example: interval is from 27.10.2020 to 30.10.2020 and frequency is 6 hours. The result of the query would tell me how many entries came in this interval in 6 hour groups.
Like:
27.10.2020 00:00:00 - 27.10.2020 06:00:00 : 2 entries
27.10.2020 06:00:00 - 27.10.2020 12:00:00 : 5 entries
27.10.2020 12:00:00 - 27.10.2020 18:00:00 : 0 entries
27.10.2020 18:00:00 - 28.10.2020 00:00:00 : 11 entries
28.10.2020 00:00:00 - 28.10.2020 06:00:00 : 8 entries
etc ...
The frequency parameter can be inserted in hours, days, weeks ...
Thank you all for you help!
First you need a recursive CTE like that returns the time intervals:
with cte as (
select '2020-10-27 00:00:00' datestart,
datetime('2020-10-27 00:00:00', '+6 hour') dateend
union all
select dateend,
min('2020-10-30 00:00:00', datetime(dateend, '+6 hour'))
from cte
where dateend < '2020-10-30 00:00:00'
)
Then you must do LEFT join of this CTE to the table and aggregate:
with cte as (
select '2020-10-27 00:00:00' datestart,
datetime('2020-10-27 00:00:00', '+6 hour') dateend
union all
select dateend,
min('2020-10-30 00:00:00', datetime(dateend, '+6 hour'))
from cte
where dateend < '2020-10-30 00:00:00'
)
select c.datestart, c.dateend, count(t.datecol) entries
from cte c left join tablename t
on datetime(t.datecol, 'unixepoch') >= c.datestart and datetime(t.datecol, 'unixepoch') < c.dateend
group by c.datestart, c.dateend
Replace tablename and datecol with the names of your table and date column.
If your date column contains milliseconds then change the ON clause to this:
on datetime(t.datecol / 1000, 'unixepoch') >= c.datestart
and datetime(t.datecol / 1000, 'unixepoch') < c.dateend
Here is one option:
select
datetime((strftime('%s', ts) / (6 * 60 * 60)) * 6 * 60 * 60, 'unixepoch') newts,
count(*) cnt
from mytable
where ts >= '2020-10-27' and ts < '2020-10-30'
group by newts
order by newts
ts represents the datetime column in your table. SQLite does not have a long datatype, so this assumes that you have a legitimate date stored as text.
The logic of the query is to turn the date to an epoch timestamp, then round it to 6 hours, which is represented by 6 * 60 * 60.
I'm trying to SELECT all the rows from a SQL database which are between an hour interval, for every day.
The datetime column is called "Dt" and has the following datetime format: 2019-10-17 16:03:43
I'd like to extract all the rows from this table where the Dt was between 22:00:00 and 02:00:00, for everyday.
SELECT *
FROM MY_TABLE
WHERE "Dt" BETWEEN '*-*- 22:00:00' AND '*-*- 02:00:00';
where * should be any...
Thanks for your support!
EDIT: I forgot to mention: I'm using the integrated SQL interpreter from DB Browser for SQLite
You need to extract the time part of the date and compare that it is within the range. Since midnight is between 22 and 2, you will need to split it to two comparisons, time between 22 and 0 and between 0 and 2.
To see how to extract the time take a look at this question.
With Postgres, assuming dt is defined as timestamp you can do the following:
SELECT *
FROM MY_TABLE
WHERE "Dt" BETWEEN "Dt"::date + time '22:00:00' and ("Dt"::date + 1) + time '02:00:00'
Or if you want to exclude timestamps at precisely 02:00:00
SELECT *
FROM MY_TABLE
WHERE "Dt" >= "Dt"::date + time '22:00:00'
and "Dt" < ("Dt"::date + 1) + time '02:00:00'
select DT_time from (
select cast (substr(to_char(Dt,'dd-mm-yyyy HH:MM:SS'),12,2) as integer ) as DT_time from MY_TABLE )
where DT_time between 2 and 22;
between 22:00:00 and 02:00:00
means:
SELECT *
FROM MY_TABLE
WHERE
substr(Dt, 12) BETWEEN '22:00:00' AND '23:59:59'
OR
substr(Dt, 12) BETWEEN '00:00:00' AND '02:00:00'
This will work ::
SELECT *
FROM MY_TABLE
WHERE DATEPART(HOUR, Dt)>22
AND DATEPART(HOUR, Dt)<2
Update :
SELECT *
FROM MY_TABLE
WHERE Dt Between DATEADD (hour,22,DATEADD(day, DATEDIFF(day, 0, Dt), 0)) AND DATEADD (hour,2,DATEADD(day, DATEDIFF(day, -1, Dt), 0))
SELECT *
FROM MY_TABLE
WHERE DATEPART(HOUR, Dt)>22
OR DATEPART(HOUR, Dt)<2
Above query work for you..
1st one will check only for particular date and consecutive next date along with your time range.
But If you don't care about dates and only looking for time interval in particular hours then 2nd one is for you.
For SQLite :
SELECT *
FROM MY_TABLE
WHERE strftime('%H','Dt')>22
OR strftime('%H','Dt')<2
I have a column called total_hours which is a set of times in Time type and it is in HH:MM:SS format in SQL server. I want to know how can I get the sum of total_hours.
For example:
total_hours
00:10:30
01:20:00
00:20:00
---------
01:50:30
Could you please give me the query to exactly do the same thing with the same format?
Try this
SELECT SUM( DATEPART(SECOND, [time]) + 60 *
DATEPART(MINUTE, [time]) + 3600 *
DATEPART(HOUR, [time] )
) as 'TotalTime'
FROM Table1
(Or)
SELECT dateadd(millisecond,sum(datediff(millisecond,0, [Time])),0)
FROM Table1
I wrote SQL expression in format:
select date1,date2,
cast ( Floor ( timestampdiff (4, char ( timestamp (DATE2)-
timestamp (DATE1)))/1440) as char (3))||'days' ||' '
|| cast ( Floor ( Mod ( timestampdiff (4, char( timestamp (DATE2)-
timestamp (DATE1))) , 1440) / 60) as char (3)) ||'hours'
So I am having difference in minutes and then transforming it to days and hours.
Example result will be:
DATE1 DATE2 DIFFERENCE
2012-01-01 10:00:00 2012-01-03 12:00 2 days 2 hours
2012-01-03 11:00:00 2012-01-03 12:00 0 days 1 hours
This is result which I am getting at the moment and it is ok.
But now I want to have seperate result with AVERAGE value for difference.
So for first row number of minutes is 3000.
For second row number of minutes is 60.
Average value is (3000+60)/2=1530 = 1 days 1 hours
So I want result in this format: average value: "1 days 1 hours"
I am using db2.
Thank you
i dont know db2, but i did it for sql server, guess there wont be many changes
sqlFiddle
what you need is OVER() clause in aggregating function
select *, DATEDIFF(hour, date1, date2)/24 as days
,DATEDIFF(hour, date1, date2)%24 as hours
,DATEDIFF(hour, date1, date2) as asdf,
AVG(DATEDIFF(hour, date1, date2)) over() as avgHours
from #temp
I have a database that I need to sum 2 values using the datetime column. Example:
Date Offered
4/16/2012 08:00:00 2
4/16/2012 08:30:00 18
4/16/2012 09:00:00 14
4/16/2012 09:30:30 42
I need to sum the values of 08:00:00 with 08:30:00 (total: 20) and 09:00:00 with 09:30:00 (total: 56) and so on.
This should work for you
select datepart(hour,myDate), SUM(Offered)
from myTable
group by
datepart(hour,myDate),
dateadd(d, 0, datediff(d, 0, myDate))
You need to group by both the hour and the date if you want it summed by individual day, otherwise you'll include other days (IE April 15 etc...)
Your pseudo code
Select HOUR(date), sum(offered) as sumO
FROM YourTable
Group By Hour(date)
Hour(date) would need to be altered to the correct syntax for the database you're working with.
SELECT [Hour] = DATEPART(HOUR, [Date]), Offered = SUM(Offered)
FROM dbo.table_name
WHERE [Date] >= '20120416'
AND [Date] < '20120417'
GROUP BY DATEPART(HOUR, [Date])
ORDER BY [Hour];
Use a DatePart function.
Syntax depends on your database
Select DatePart("year", Date), Sum(Offered)
From table1
Group By DatePart("year", Date)