This is my table data:
Fromdatetime Todatetime
2018-12-8 10:25 AM 2018-12-8 16:25 PM
2018-12-8 16:25 PM 2018-12-8 20:25 PM
2018-12-8 20:25 PM 2018-12-9 01:25 AM
2018-12-9 01:25 AM 2018-12-10 23:25 PM
Here is my list of parameter value:
2018-12-8 10:25 AM to 2018-12-9 10:25Am
2018-12-9 10:25 AM to 2018-12-10 10:25AM
For 1 parameter my output should be:
It should returned me 4 first row data Hours difference by substarcting fromdatetime and Todatetime
But in case of 4th row my hours should only calculate the difference of fromdatetime and Todatetime
Example result should be
Hours
6
4
5
9
So can you please help me to write this query
Are you simply looking for datediff()?
select datediff(hour, Fromdatetime, Todatetime) as num_hours
Related
I have a sql table like this
occ_val time
0 2/1/2022 3:35:08 pm
1 2/1/2022 3:59:08 pm
2 2/1/2022 4:55:08 pm
3 2/1/2022 5:32:08 pm
2 3/1/2022 4:43:08 pm
3 4/1/2022 2:15:08 pm
How to I sort by each hour to get this:
time occ_val
2:00:00 pm 3
3:00:00 pm 1
4:00:00 pm 4
5:00:00 pm 3
Your help is very much appreciated :D
Here is a MySQL solution:
select DATE_FORMAT(min(time), '%h:00'),sum(occ_val) from tbl group by hour(time)
See https://www.db-fiddle.com/f/czudbp5zug6HxX4TFV26GT/0
SELECT * from YourTableName
ORDER BY (timeCol - FLOOR(CAST(timeCol AS float)) ASC
I have the following table with start and end dates:
dataid TS EndTS
1744 7/27/17 1:57:34 PM 7/27/2017 1:57:38 PM
1743 7/27/17 1:57:31 PM 7/27/2017 1:57:34 PM
1742 7/27/17 1:57:23 PM 7/27/2017 1:57:31 PM
1741 7/27/17 1:57:16 PM 7/27/2017 1:57:23 PM
1740 7/27/17 1:57:04 PM 7/27/2017 1:57:16 PM
1739 7/27/17 1:56:57 PM 7/27/2017 1:57:04 PM
1738 7/27/17 1:56:38 PM 7/27/2017 1:56:57 PM
I would like to get the date/time interval (in seconds) and then calculate a running total.
Here is what I have so far:
SELECT
[dataid] AS [dataid]
DateDiff("s", [TS],[EndTS]) AS [durationsec]
DSum("[durationsec]","[HX32]","[dataid] <=" & [dataid]) AS [add]
FROM [HX32];
I think the datediff() funtion is possibly causing formatting problems. With "[durationsec]" I get all nulls as a result, with [durationsec] I get the following results:
durationsec add
4 6896
3 5169
8 13776
7 12047
12 20640
7 12033
19 32642
I also tried cint(DateDiff("s", [TS],[EndTS])) No change.
I also tried passing durationsec to a table and running a seperate query. No change. (Also I would prefer to do this all in one query)
Here are the results I would like to achieve:
dataid TS EndTS durationsec add
1744 7/27/17 1:57:34 PM 7/27/2017 1:57:38 PM 4 60
1743 7/27/17 1:57:31 PM 7/27/2017 1:57:34 PM 3 56
1742 7/27/17 1:57:23 PM 7/27/2017 1:57:31 PM 8 53
1741 7/27/17 1:57:16 PM 7/27/2017 1:57:23 PM 7 45
1740 7/27/17 1:57:04 PM 7/27/2017 1:57:16 PM 12 38
1739 7/27/17 1:56:57 PM 7/27/2017 1:57:04 PM 7 26
1738 7/27/17 1:56:38 PM 7/27/2017 1:56:57 PM 19 19
Thanks, I'm a beginner.
Time is not seconds but integer days, so you could try:
SELECT
[dataid],
DateDiff("s", [TS], [EndTS]) AS [durationsec],
DSum("[EndTS]-[TS]", "[HX32]", "[dataid] <= " & [dataid] & "") * 86400 AS [add]
FROM
[HX32];
That said, June's method should work as well. If both fail, something else is going on.
SELECT dataid,
startts,
endts,
DATEDIFF("s", startts,endts) AS durationsec,
SUM(DATEDIFF("s", startts,endts)) OVER (ORDER BY endts ROWS UNBOUNDED PRECEDING) AS runningtotal
FROM durtab
ORDER BY 5 DESC;
Result:
dataid startts endts durationsec runningtotal
1744 2017-07-27 13:57:34.000 2017-07-27 13:57:38.000 4 60
1743 2017-07-27 13:57:31.000 2017-07-27 13:57:34.000 3 56
1742 2017-07-27 13:57:23.000 2017-07-27 13:57:31.000 8 53
1741 2017-07-27 13:57:16.000 2017-07-27 13:57:23.000 7 45
1740 2017-07-27 13:57:04.000 2017-07-27 13:57:16.000 12 38
1739 2017-07-27 13:56:57.000 2017-07-27 13:57:04.000 7 26
1738 2017-07-27 13:56:38.000 2017-07-27 13:56:57.000 19 19
DSum is looking at the [HX32] table or query to find a field named [durationsec]. It doesn't exist there.
SELECT
[dataid],
DateDiff("s",[TS],[EndTS]) AS [durationsec],
DSum("DateDiff('s',[TS],[EndTS])","[HX32]","[dataid] <=" & [dataid]) AS [add]
FROM [HX32];`
Note the use of apostrophes to delimit the 's' parameter in the nested DateDiff.
An alternative approach is to do the running sum in Report because textbox in report has a RunningSum property. Domain aggregate functions in queries can perform slowly in large datasets.
I have a table where data will be Inserting every 15 min. so time stamp will be as follows
table Name: tblresultset
ID Date count
1 2017-05-03 1:15:00 10
2 2017-05-03 1:16:00 11
3 2017-05-03 1:27:00 2
4 2017-05-03 1:28:00 3
5 2017-05-03 1:29:00 6
6 2017-05-03 1:30:00 8
7 2017-05-03 1:31:00 2
8 2017-05-03 1:32:00 1
9 2017-05-03 1:33:00 2
Now I am looking for the query which will get me the total count from
2017-05-03 1:15 to 2017-05-03 1:30
I have to get this kind of count for each 15 min interval on the given date.
Could anybody help me out please?
Use group by and datediff:
GROUP BY DATEDIFF(MINUTE, '1990-01-01T00:00:00', date) / 15
See this post for more info.
Table has columns Receiptno: and [TransDate] and Transtime. eg: Data below
0080052594 2012-10-28 1899-12-30 19:01:38.000
0080052595 2012-10-28 1899-12-30 19:05:09.000
0080052596 2012-10-28 1899-12-30 19:05:15.000
I need query to get hourly interval and the no: of transactions in the below format
Hour Inetrval No: Trans
09:01-10:00 10
10:01-11:00 16
Which DBMS are you using? What specific data type are your fields?
In Access, you'd use a Totals query (GROUP BY in SQL), you could do it using an additional field (or layered queries). You could also use inline code string comparisons. This would work with string or DateTime fields for transaction date and time.
For example, TABLE1:
RECEIPTNO TRANSDATE TRANSTIME
1 1/12/2015 4:32:00 PM
2 1/12/2015 4:45:00 PM
3 1/12/2015 4:52:00 PM
4 1/12/2015 3:57:00 PM
5 1/12/2015 4:07:00 PM
6 1/12/2015 4:09:00 PM
7 1/12/2015 6:15:00 PM
8 1/12/2015 12:34:00 PM
9 1/12/2015 2:45:00 PM
10 1/12/2015 3:15:00 PM
11 1/12/2015 3:17:00 PM
12 1/12/2015 3:49:00 PM
13 1/12/2015 3:47:00 PM
14 1/12/2015 2:52:00 PM
15 1/12/2015 2:36:00 PM
16 1/12/2015 2:17:00 PM
17 1/12/2015 2:25:00 PM
18 1/12/2015 4:12:00 PM
QUERY1 to count by hour as string:
SELECT Count(TABLE1.RECEIPTNO) AS CountOfRECEIPTNO, TABLE1.TRANSDATE, Left([TRANSTIME],InStr([TRANSTIME],":")-1) AS [HOUR]
FROM TABLE1
GROUP BY TABLE1.TRANSDATE, Left([TRANSTIME],InStr([TRANSTIME],":")-1)
ORDER BY Left([TRANSTIME],InStr([TRANSTIME],":")-1);
Results:
CountOfRECEIPTNO TRANSDATE HOUR
1 1/12/2015 12
5 1/12/2015 2
5 1/12/2015 3
6 1/12/2015 4
1 1/12/2015 6
I have a table which looks like you can see below:
Id Date ScheduledTimeFrom ScheduledTimeTo ActualTimeFrom ActualTimeTo
1 2013-01-01 1899-12-30 07:00:00 1899-12-30 18:00:00 1899-12-30 07:23:00 1899-12-30 17:15:00
I need to calculate per half hour how many records exists, the output should be like:
Time Actual Count:
7:00 4
7:30 4
8:00 4
8:30 4
9:00 4
9:30 5
10:00 5
10:30 6
11:00 7
11:30 8
12:00 8
12:30 8
13:00 8
13:30 8
14:00 8
14:30 8
15:00 7
15:30 7
16:00 7
16:30 6
17:00 5
17:30 4
18:00 4
I already tried to make a helper table which should hold the times per halfhour. I have joined this helpertable with the table that contains the data and after that I tried to use a group by function but it was not working.
My query was like:
Create table period (timefrom datetime, timeto datetime)
insert into period
select '1899-12-30 07:00:00.000', '1899-12-30 07:30:00.000'
Union all
select '1899-12-30 07:30:00.000', '1899-12-30 08:00:00.000'
select *
from period p left join table1 t on t.ActualTimeFrom < p.timeto and t.ActualTimeTo >=p.timefrom
Grouping this give me no desired result....
Anyone an idea how to come to the result?
P.s. I am using sql server 2005.
After snooping around and testing it on my side, looks like this date function could be the answer:
DATEADD(mi,DATEDIFF(mi,0,YOUR_DATE_COLUMN)/30*30,0)