DolphinDB Calculate the time difference between TIMESTAMP and DATETIME? - time

I tried the script in DolphinDB:
t =table(
array(DATETIME, 0) as MDTime,
array(TIMESTAMP, 0) as ReceivedTime
)
share t as testShareTable;
select * from testShareTable;
select avg(ReceivedTime - MDTime) as DelayTime from testShareTable;
The table is
But the output is not as expected
Any suggestions to modify the code? Btw, I'd like to add 8 hours to ReceivedTime. How can I obtain the result?

Modified the last line of code:
select avg(temporalAdd(ReceivedTime,8H) - timestamp(MDTime)) as DelayTime from testShareTable;

Related

SAS: creating benchmark period dummies (around event)

I want to do an event study and need some descriptives around the events I am testing with different window sizes.
Let's assume I have a data set with 10000 observations (stocks, dates, measures) and merge them with an announcement data set. Now, I want to get a dummy variable that is always 0 except for parameters given by me:
if date = announcement_date then;
window_dummy at t-60 to t-11 = 1
or
window_dummy at t-5 to t+5 = 1
I hope I described it appropriately and there is a way because there is no lead function or similar.
Best
M
From what I gather from this, you wish to have additional variable, which creates window (or binning) variable reaching x days to past and future;
First we generate some test data. SAS dates are just numbers so:
data begin;
do i = 21040 to 21080;
my_date= i;
output;
end;
drop i;
run;
Next we calculate the window:
%let act_date= 21060;
%let min_date= %eval(&act_date - 5); /*5 days to past*/
%let max_date= %eval(&act_date + 3); /*3 days to future*/
Then it's left to generate the dummy variable:
data refined;
set begin;
if &min_date <= my_date and
my_date <= &max_date then
dummy = 1;
else dummy = 0;
run;
Now, one can calculate this all in single datastep, but maybe this is more informative way.
Edit: you can specify windows as you please. In your case you could have:
min_date1= %eval(&act_date - 60);
max_date2= %eval(&act_date - 11);
....
Then just add them to the if clause with or operator.

Query taking too much time to execute

I have this query which takes around 11-12 hrs to execute:
SELECT A.EVENT_DATE SENT_DATE
,A.TRANSACTION_CODE EVENT_CODE
,COUNT(a.transaction_id) COUNT
FROM customer A
WHERE A.REQUEST_DATE >= (select max(RUNPERIODFROM_DATE) from auditeventbatch where auditevent_code = 'CDPE')
AND A.TRANSACTION_STATUS = 'STC'
AND NOT EXISTS
(SELECT /*use_nl(a b)*/1 FROM EVENTS_V1 B
WHERE B.TRANSACTIONID=a.transaction_id
AND TRUNC(B.EVENT_DATE) = A.CCE_EVENT_DATE
AND B.TRANSACTION_STATUS='STC'
)
GROUP BY A.CCE_EVENT_DATE
,A.TRANSACTION_CODE
Is there any way I can rewrite this to reduce the execution time of this.The view CDS_EVENTS_V1 has millions of record it it.I don't have the option the make the view as materialized view.
Well, one of the problems is this row:
AND TRUNC(B.CCE_EVENT_DATE) = A.CCE_EVENT_DATE
TRUNC makes the optimizer to ignore the index(if exists , if not - add it) . I suggest adding another column containing the trunced value and comparing by that column.
Also, if not exists, add the following index:
CDS_EVENTS_V1 (transactionid,CCE_EVENT_DATE,TRANSACTION_STATUS)
cds_auditeventbatch (RUNPERIODFROM_DATE,auditevent_code )

How stock a numeric value (diff of 2 date)

I've to calculate the différence between two Dates : TODAY() and DATE_DEB_VAC.
With Oracle, it's kinda easy : TODAY()-DATE_DEB_VAC -> give the number of day between those 2 date.
But I've to do it with in an ETL (GENIO). I've a column to stock it like that :
NUMBER_DAY_DIFF (NUMBER 10) = TODAY()-DATE_DEB_VAC. But it's impossible to stock it cause it's 2 date.
How can i do this ? :(
You can try the val function of GENIO ETL
VAL(TODAY()-DATE_DEB_VAC)
this is equivalent to to_numbre in Oracle
NUMBER_DAY_DIFF (NUMBER 10) = DATEDIFF (TODAY; DATE_DEB_VAC)
Should give you what you need.

How to optimize EF/LINQ query to get a single rather than nested SELECT statement

The following LINQ executes query which takes 90 milliseconds to execute:
.Where(Function(i) (i.RequestedByUserId = MySession.ApplicationUserId)
And (i.RequestKey1 = searchJson)
And (i.RequestMethod = "ProjectPlanService.GetProjectPlanMaintenanceData"))
.Select(Function(i) i.ResultJson).FirstOrDefault
The SQL generated is as below :
SELECT
[Limit1].[ResultJson] AS [ResultJson]
FROM ( SELECT TOP (1)
[Extent1].[ResultJson] AS [ResultJson]
FROM [dbo].[ApplicationCache] AS [Extent1]
WHERE ([Extent1].[RequestedByUserId] = 2) AND ([Extent1].[RequestKey1] = '{"SortProperty":"","SortOrder":0,"PageNumber":1,"RecordsPerPage":15,"CriteriaCount":"1","CriteriaString":"~=~Id"}') AND ('ProjectPlanService.GetProjectPlanMaintenanceData' = [Extent1].[RequestMethod])
) AS [Limit1]
How Can I optimize the above LINQ expression to reduce the time taken to execute?
Is there a way to get a single Select statement like below:
SELECT
[ResultJson]
FROM [dbo].[ApplicationCache] AS [Extent1]
WHERE ([Extent1].[RequestedByUserId] = 2) AND ([Extent1].[RequestKey1] = '{"SortProperty":"","SortOrder":0,"PageNumber":1,"RecordsPerPage":15,"CriteriaCount":"1","CriteriaString":"~=~Id"}') AND ('ProjectPlanService.GetProjectPlanMaintenanceData' = [Extent1].[RequestMethod])
1: There's no need to optimise, it's just fine as it is - The extra "wrapping" won't change anything.
2: It's doing a top 1 because you asked for FirstOrDefault(), if you want a full list of results don't do that.

need help in sum of time in rdlc

I am working on attendance management system.
In my project i want to display total worked hours of the employee in the daily report.
In my database table i have already calculated working hours for the each employee.
Now i want to display Total worked hours of each and every entry at the bottom of the report.
e.g
EmployeeId EmployeeName WorkedHours
1 ABC 04:00:25
2 XYZ 07:23:01
3 PQR 11:02:15
SO i want to display total of all 3 employees at the end of report in RDLC.
like Total: 22:25:42
Please let me know how can i achieve it?
You just need to add =Sum(Fields!WorkedHours.Value) in footer row for "WorkedHours" column.
see the link in MSDN http://msdn.microsoft.com/en-us/library/ms252113(v=vs.80).aspx
You can use the TimeStamp class gether with the Sum function,
follow an example:
=TimeSpan.FromMinutes(Sum(Fields!Dirigindo.Value))
Try This out and see if it works
=(SUM(Cint(Split(Fields!WORKEDHOUR.value,":").GetValue(0))) + (SUM(Cint(Split(Fields!WORKEDHOUR.Value,":").GetValue(1))) + Sum(Cint(split(Fields!WORKEDHOUR.Value,":").GetValue(2)))\60)\60 ).ToString + ":" + ((SUM(Cint(Split(Fields!WORKEDHOUR.Value,":").GetValue(1))) + Sum(Cint(split(Fields!WORKEDHOUR.Value,":").GetValue(2)))\60) Mod 60).ToString + ":" + (Sum(Cint(split(Fields!WORKEDHOUR.Value,":").GetValue(2))) Mod 60).ToString
Facing the same issue here is your final answer.
Step1: Go to your textbox expression and past below code
=Code.MinutesToHoursMinutes(SUM(Hour(Fields!TTShortTime.Value)*60)+SUM(Minute(Fields!TTShortTime.Value)))
Step2: Goto your rdlc report properties => Tab Code, Here past below function
Function MinutesToHoursMinutes(ByVal vMins As Integer) As String
Dim Mins As Integer
Dim Hours As Integer
vMins = IIF(vMins <= 0, 0, vMins)
Hours = Floor(vMins / 60)
Mins = vMins - (Hours * 60)
MinutesToHoursMinutes = IIF(Hours < 9, "0" & CStr(Hours), CStr(Hours)) & ":" & IIF(Mins < 9, "0" &
CStr(Mins), CStr(Mins))
Return MinutesToHoursMinutes
End Function
Here in Step1 we get hours and convert it into minutes then get minutes and sum with hour calculated minutes.
then passes it to function which returns string format like hh:mm

Resources