I have a column ID which has values 10,20,10,10,60,60 and so on. I have another column which is a diff of (modified_date - created_date) as TS which has datetime values like 6 5:28:13.0 (6 is the days), 1 1:34:54.0 and so on for each ID. How can I get the total days and time for each ID value? For example: how can I get the total value of TS for ID = 10?
From the image: for each value of eowstage_nbr column, how can I get total days and time in column TS?
A regular sum() aggregation over the ID column combined with some data type conversions should do it...
Sample data
create table data
(
id int,
created datetime,
updated datetime
);
insert into data (id, created, updated) values
(10, '2020-01-01 00:00:00', '2020-01-01 05:00:10'),
(10, '2020-01-02 00:00:00', '2020-01-03 11:00:03'),
(20, '2020-02-05 00:00:00', '2020-02-05 01:02:03'),
(20, '2020-02-15 00:10:00', '2020-02-17 00:00:10');
Solution
Describing the steps in plain English would sound like:
Take the difference between the created and updated timestamp, expressed in seconds;
add them up the seconds for each id;
convert the seconds to a suitable date format.
I took care of step 3 by adding the seconds up to a reference date 1900-01-01.
Query version:
select d.id,
dateadd(second, sum(datediff(second, d.created, d.updated)), '1900-01-01 00:00:00') as diff_sum
from data d
group by d.id;
Result
The values are not formatted like in your screenshot, but that should boil down to data type selection, datetime formatting and localization.
id diff_sum
-- -----------------------
10 1900-01-02 16:00:13.000
20 1900-01-03 00:52:13.000
Fiddle
Since you did not give any sample data and looking at your immage I assume you have data in TS column as per your image. (I have used dummy data to replicate your TS column). So the steps would be to separate day from time in your TS column, then converting that time into seconds, and doing summation of those seconds and days seperately, and finally combining the days and seconds converted to proper d hh:mm:ss format.
select ID,
cast((sum(noofdays) + SUM(DATEDIFF(S, '00:00:00', noofhrs))/3600/24) as varchar(5)) + ' ' +
right('00'+CAST(SUM(DATEDIFF(S, '00:00:00', noofhrs))/3600%24 AS VARCHAR(2)),2) + ':'
+ right('00'+CAST(SUM(DATEDIFF(S, '00:00:00', noofhrs))%3600/60 AS VARCHAR(2)),2) + ':'
+ right('00'+CAST(((SUM(DATEDIFF(S, '00:00:00', noofhrs))%3600)%60) AS VARCHAR(2)),2)
from
(
select ID, case left(TS,charindex(' ',TS)) when '' then 0 else left(TS,charindex(' ',TS)) end as noofdays,
cast(right(TS,len(TS)-charindex(' ',TS)) as datetime) as noofhrs from
(
select 1 ID,'6 5:28:13.0' TS union
select 1,'1 8:28:13.0' a union
select 1,'2 12:28:13.0' a union
select 2,'5:28:13.0' a union
select 2,'6 5:28:13.0' a
)
tablename
) A
group by ID
I have a table in which one column as registrationDate whose type is datetime. I need to find all the rows where registrationDate as timestamp as 00:00:00.000.
for example :
registrationDate: '2019-03-20 00:00:00.000'
I need to query something similar as below :
select * from table where registrationDate like '%00:00:00.000';
You can cast datetime as time:
WHERE CAST(registrationDate AS TIME) = '00:00'
WHERE registrationDate = CONVERT(date, registrationDate);
See UPDATE below
Create Table #tbl
(
registrationDate Datetime
)
Insert Into #tbl Values
('2019-03-20 00:00:00.000'),
('2019-03-20 00:00:25.000')
Query
Select * From #tbl
Where Cast(registrationDate As Time) = '00:00:00.0000000'
Result:
registrationDate
2019-03-20 00:00:00.000
Update: If you really need to use "Like"
Select * From #tbl
Where convert(VarChar(50), registrationDate, 121) Like '%00:00:00.000'
select * from table where FORMAT(registrationDate, 'HH:mm:ss:ms') ='00:00:00.000'
The following expression gets for every datetime value the corresponding date with time portion:
dateadd(day, datediff(day, 0, '<date_time>'), 0)
Hence, the following will do the job:
create table foo (id int, registrationDate datetime)
insert foo values
(1, '2019-04-04T03:22:48.00'),
(2, '2019-04-04T00:00:00.00')
select * from foo
where registrationDate =
dateadd(day, datediff(day, 0, registrationDate), 0)
I have Date time when engine has started working and how long was it working. but sometimes it can work more than 24 Hours.
if it worked for 28 Hours on the starting date i will have record
Name started_working Finished working hours_worked
obj-00123 07/02/2018 13:30 08/02/2018 17:30 28
I need to to have record that will show that engine has worked for 10:30 in 07 and 17:30 in 08.
Name started_working Finished working hours_worked
obj-00123 07/02/2018 13:30 07/02/2018 00:00 10:30
obj-00123 07/02/2018 13:30 08/02/2018 17:30 17:30
or something like that. I don't have any idea how can i get this done. can you give me some clues. i dont ask for writing code if its not too easy.
thank you
This might do the trick for you
--Using CTE to show sample data
;WITH cteX( Name,started_working,Finished_working)
AS
(
SELECT
'obj-00123','07/02/2018 13:30','08/02/2018 17:30' UNION ALL
SELECT 'obj-00155','07/02/2018 15:00','07/02/2018 22:30'
)
SELECT
X.Name
, X.started_working
, X.Finished_working
, HoursWorked = CONVERT(VARCHAR(12), DATEADD(minute, DATEDIFF(minute, X.started_working, X.Finished_working), 0), 114)
FROM
(
SELECT
T1.Name
,T1.started_working
,Finished_working = DATEADD(SECOND,0,DATEADD(DAY, DATEDIFF(DAY,-1,T1.started_working),0)) -- Dummy finish time # Midnight
FROM
cteX T1
WHERE
DATEDIFF(DAY,T1.started_working,T1.Finished_working) <> 0 --Create a dummy finish time #Midnight when start and end not on same day
UNION ALL
SELECT
T2.Name
,started_working = CASE WHEN DATEDIFF(DAY,T2.started_working,T2.Finished_working) <> 0
THEN DATEADD(DAY, DATEDIFF(DAY, 0, T2.Finished_working), 0) --Start # Midnight
ELSE T2.started_working
END
,T2.Finished_working
FROM
cteX T2
) X
ORDER BY
X.Name, X.started_working
OUTPUT
Name started_working Finished_working HoursWorked
obj-00123 2018-07-02 13:30:00.000 2018-07-03 00:00:00.000 10:30:00:000
obj-00123 2018-08-02 00:00:00.000 2018-08-02 17:30:00.000 17:30:00:000
obj-00155 2018-07-02 15:00:00.000 2018-07-02 22:30:00.000 07:30:00:000
According to your sample data working hours may be more than several days. In this case you need to use tally table or recursive CTE. I have used recursive CTE since it's easier to handle result fields. Also there are two columns in result named started_working and started_working2. started_working is from your expected output, but I believe you need started_working2 column
declare #T as table (
Name varchar(100)
, started_working datetime
, finished_working datetime
--, hours_worked int
)
insert into #T
values
('obj-00123', '20180207 13:30', '20180208 17:30')
, ('obj-00123', '20180208 19:00', '20180209 05:00')
, ('obj-00123', '20180209 19:00', '20180209 22:00')
, ('obj-00123', '20180210 19:00', '20180213 22:00')
;with rcte as (
select
*, started_working2 = started_working
, next_date = cast(dateadd(dd, 1, started_working) as date), 1 step
from
#T
union all
select
Name, started_working, finished_working
, cast(next_date as datetime)
, dateadd(dd, 1, next_date), step + 1
from
rcte
where
next_date < finished_working
)
select
Name, started_working, started_working2, finished_working
, right(replace(str(diff / 60), ' ', 0), 2) + ':' + right(replace(str(diff % 60), ' ', 0), 2) hours_worked
from (
select
Name, started_working
, case
when step = 1 then started_working
else started_working2
end started_working2
, case
when step = max(step) over (partition by Name, started_working)
then finished_working else next_date
end finished_working
from
rcte
) t
cross apply (select datediff(mi, started_working2, finished_working) diff) ca
I'd approach the solution something like this:
WITH dynamic_twelths_of_hr_table(datetime2_value) AS
(
SELECT '2017-01-01'
UNION ALL
SELECT DATEADD(MINUTE, 5, datetime2_value)
FROM dynamic_twelths_of_hr_table
WHERE DATEADD(MINUTE, 5, datetime2_value) <= '2019-01-01'
)
,twelths_hr_table AS
(
SELECT
DATEADD(DAY, DATEDIFF(DAY, 0, datetime2_value), 0) AS date_value
,datetime2_value
FROM dynamic_twelths_of_hr_table
)
,modified_source_table AS
(
SELECT
name
,objectid
,engine_start
,ISNULL(engine_stop, GETDATE()) AS engine_stop
,IIF(engine_start IS NULL OR engine_stop IS NULL, 1, 0) AS is_still_running
FROM [YOUR_SOURCE_TABLE]
)
SELECT
name
,objectid
,is_still_running
,date_value
,(COUNT(datetime2_value)/12.0) AS hours_run_on_this_day
FROM
modified_source_table
LEFT JOIN
twelths_hr_table AS tht
ON (tht.datetime2_value BETWEEN engine_start AND engine_stop)
GROUP BY
name, objectid, is_still_running, date_value
ORDER BY
name, objectid, is_still_running, date_value
Note I haven't tested this code so please excuse any small syntax errors.
I've also baked in an assumption about the range of dates to be considered (these can be widened, or made dynamic based on when the query runs), and it has a 5 minute resolution (based on the fact that, at a glance, I could only see one value in the engine_stop column that didn't fall on a 5-minute threshold - so I assume sub-5-minute precision is not required).
Basically what it does is expand each engine row out into 5-minute windows (twelths of an hour), and then simply groups these by day and counts the number of windows per day during which the engine was running.
For currently-running engines, it will calculate how long it has run so far. I trust you can tweak the code to your exact requirements.
thank you to all. this worked perfectly. it needed slight polishing and recursion needed to be set to 0.
But creating view is a trouble with CTE.
create view mroobjectenginerowkinghoursdeclare as
declare #T as table (
Name nvarchar(100)
, OBJECTID varchar(50)
, started_working datetime
,STOPFROM datetime
,STARTDATE datetime
,STOPDATE datetime
,MODIFIEDDATETIME datetime
,START_STOP int
,STARTDESCRIPTION nvarchar(300)
,STOPDESCRIPTION nvarchar(300)
,wattage nvarchar (50)
,purpose nvarchar(300)
,location nvarchar(300)
,finished_working datetime
,oldDiff int
)
insert into #T
select
NAME
,OBJECTID
,STOPTO
,STOPFROM
,STARTDATE
,STOPDATE
,MODIFIEDDATETIME
,START_STOP
,STARTDESCRIPTION
,STOPDESCRIPTION
,wattage
,purpose
,location
,next_stopfrom
,diff
FROM [MicrosoftDynamicsAX].[dbo].[mroobjectengineworkinghours]
;with rcte as (
select
*, started_working2 = started_working
, next_date = cast(dateadd(dd, 1, started_working) as date), 1 step
from
#T
union all
select
Name,OBJECTID, started_working,STOPFROM,STARTDATE,STOPDATE,MODIFIEDDATETIME,START_STOP,STARTDESCRIPTION
,STOPDESCRIPTION,wattage
,purpose
,location, finished_working,oldDiff
, cast(next_date as datetime)
, dateadd(dd, 1, next_date), step + 1
from
rcte
where
next_date < finished_working
)
select
Name,OBJECTID, started_working,STOPFROM,STARTDATE,STOPDATE,MODIFIEDDATETIME,START_STOP,STARTDESCRIPTION
,STOPDESCRIPTION,wattage
,purpose
,location,oldDiff, started_working2, finished_working
, right(replace(str(diff / 60), ' ', 0), 2) + ':' + right(replace(str(diff % 60), ' ', 0), 2) hours_worked
from (
select
Name,OBJECTID, started_working,STOPFROM,STARTDATE,STOPDATE,MODIFIEDDATETIME,START_STOP,STARTDESCRIPTION
,STOPDESCRIPTION,wattage
,purpose
,location,oldDiff
, case
when step = 1 then started_working
else started_working2
end started_working2
, case
when step = max(step) over (partition by Name, started_working)
then finished_working else next_date
end finished_working
from
rcte
) t
cross apply (select datediff(mi, started_working2, finished_working) diff) ca
OPTION (MAXRECURSION 0);
I have multiple row with different column values sharing the same id .
for e.g
col-A col-B col-C col-D Col-E
1 12 2012-12-01 1900-12-01 2:00:00 1900-12-01 3:30:00
2 12 2012-12-02 1900-12-01 3:00:00 1900-12-01 4:O0:00
I would like to get in single row preferably with separator ( * )
12 2012-12-01 2:00 - 3:30 * 2012-12-02 3:00 - 4:00
To avoid confusion - Edited the separator uses * instead of newline .
It is a pain to eliminate the [col-B] on the second line. The following formats the datetime's the way you seem to want them:
select [col-B],
(convert(varchar(19), [col-C] + [col-d], 121) + ' - ' +
right(convert(varchar(19), [col-E], 121), 8)
) col
from t
As noted by others, this sort of formatting is really not a database issue, but something for your application to handle. That said, and with thanks to Gordon Linoff for the conversion:
declare #TimeRanges as Table
( [col-A] Int Identity, [col-B] Int, [col-C] Date, [col-D] DateTime, [col-E] DateTime );
insert into #TimeRanges ( [col-B], [col-C], [col-D], [col-E] ) values
( 12, '20121201', '19001201 02:00:00', '19001201 03:30:00' ),
( 12, '20121202', '19001201 03:00:00', '19001201 04:00:00' ),
( 13, '20121219', '19001201 09:00:00', '19001201 17:00:00' );
select * from #TimeRanges;
select [col-A],
case when RN = 1 then Cast( [col-B] as VarChar(10) ) else '' end as [col-B], Range
from (
select [col-B], Row_Number() over ( partition by [col-B] order by [col-A] ) as RN,
( Convert( VarChar(19), [col-C] + [col-D], 121 ) + ' - ' +
Right( Convert( VarChar(19), [col-E], 121 ), 8 ) ) as Range
from #TimeRanges ) as ArbitraryPlaceholder
order by [col-A];
So what I am trying to is generate all the hours that are inside a specific time range.
So given the range 11 AM to 2:00 PM, I would get:
11:00 AM
12:00 PM
1:00 PM
2:00 PM
I am trying to avoid having to store every specific hour a store might be open and just store the range (I need to compare the hours against other times)
Thanks
No loops, recursive CTEs or numbers table required.
DECLARE
#start TIME(0) = '11:00 AM',
#end TIME(0) = '2:00 PM';
WITH x(n) AS
(
SELECT TOP (DATEDIFF(HOUR, #start, #end) + 1)
rn = ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_columns ORDER BY [object_id]
)
SELECT t = DATEADD(HOUR, n-1, #start) FROM x ORDER BY t;
You could use a recursive CTE. This would generate the hours between 11 and 14:
;with Hours as
(
select 11 as hr
union all
select hr + 1
from Hours
where hr < 14
)
select *
from Hours
Live example at SQL Fiddle.
If you have a numbers table (click the link to create one if you don't)...
create table test(
startTime time
, endTime time
)
insert into test
select '11:00', '14:00'
select
dateadd(hh, n.n, t.startTime) as times
from test t
inner join Numbers n
-- assuming your numbers start at 1 rather than 0
on n.n-1 <= datediff(hh, t.startTime, t.endTime)
If this is specialized, you can create an hours table with just 24 values.
create table HoursInADay(
[hours] time not null
, constraint PK_HoursInADay primary key ([hours])
)
-- insert
insert into HoursInADay select '1:00'
insert into HoursInADay select '2:00'
insert into HoursInADay select '3:00'
insert into HoursInADay select '4:00'
insert into HoursInADay select '5:00'
insert into HoursInADay select '6:00'
insert into HoursInADay select '7:00'
...
select
h.[hours]
from test t
inner join HoursInADay h
on h.[hours] between t.startTime and t.endTime
The easiest way I can think of to do this is to have only 1 permanent table with a list of all hours; 24 entries total.
Create table dbo.Hours (Hourly_Time Time NOT NULL)
Insert into dbo.Hours ...
Then, given times A & B:
select * from dbo.Hours where Hourly_Time<=A and Hourly_Time>=B
#Andomar Thanks a lot, you helped me, there is my add above your code.
*----------------------------
create view vw_hoursalot as
with Hours as
(
select DATEADD(
dd, 0, DATEDIFF(
dd, 0, DATEADD (
year , -5 , getDate()
)
)
) as dtHr
union all
select DATEADD (minute , 30 , dtHr )
from Hours
where dtHr < DATEADD(
dd, 0, DATEDIFF(
dd, 0, DATEADD (
year , +5 , getDate()
)
)
)
)
select * from Hours
----------------------------
select * from vw_hoursalot option (maxrecursion 0)
----------------------------*