Neo4j statistical data - average of count - database

I have 2 nodes p:Person and d:DateTime. I need to make statistics : for each hour of the week
Monday 0:
Monday 1:
...
Monday 23:
Tuesday 0:
etc...
Total count is easy
MATCH (p:Person)-[dt:DATE]-(d:DateTime) RETURN d.datetime.dayOfWeek, d.datetime.hour, COUNT(p);
This will output for each hour of week total count, but i need to make average
ex.
10.1.2022 - Monday hour 0 - total count 5000
17.1.2022 - Monday hour 0 - total count 1000
OUTPUT:
Monday 0: 3000;

If I understand correctly, you want something like:
MATCH (:Person)-[:DATE]-(d:DateTime)
WITH d, d.datetime.dayOfWeek AS day, d.datetime.hour AS hour, COUNT(p) AS count
RETURN day, hour, avg(count) AS average

Related

How do I group my data by specific date ranges, like hour, day, month?

My data contains two timestamps start and end. How do I group it so that I can show all the data points where the diff of end-start is less than a month, less than a week, day etc.?
Example data:
start
end
price
2021-12-15 23:00:00 UTC
2022-06-24 22:00:00 UTC
120
2021-12-17 23:00:00 UTC
2022-01-16 23:00:00 UTC
93
2021-12-17 23:00:00 UTC
2022-01-16 23:00:00 UTC
99
I want to annotate the data with a new field called duration with values like hour, day, week, month
When editing the data source, it's possible to add new 'calculated fields'.
I used DATETIME_DIFF to annotate the duration like this:
CASE
WHEN DATETIME_DIFF(end, start, HOUR) <= 1 THEN "Hour"
WHEN DATETIME_DIFF(end, start, DAY) <= 1 THEN "Day"
WHEN DATETIME_DIFF(end, start, DAY) <= 7 THEN "Week"
ELSE "Month"
END

Why the function weekOfYear(2019.12.31) returns 1?

which week in the year does this function return? Why is the return of 2019.12.31 the first week?
I have checked this function in dolphindb-help, and it says that "weekOfYear(X): For each element in X, return a number indicating which week of the year it falls in. Each week starts on Monday." So I think it is not right for the date 12.31 to return 1 right?
The weekOfYear in dolphindb is consistent with the rule of weekofyear in pandas, and the specific rules are as follows:
If December 31st falls on a Monday, Tuesday or Wednesday, it returns as week 01 of the next year;
If it falls on a Thursday, it returns as week 53 of the year just ended;
If it falls on a Friday, it returns as week 52 (or week 53 if the year just ended is a leap year);
If it falls on a Saturday or Sunday, it returns as week 52 of the year just ended.

SSRS. How to group in a group?

I have SSRS report like below with Boolean parameter to show 12h view or 24h view. To fit report into single screen the 24h report need to group by every 2hr.
07:00 08:00 09:00 10:00 11:00 12:00 13:00 14:00 ...
Line 1 25 30 24 26 25 25 30 30 ...
08:00 10:00 12:00 14:00 ...
Line 1 55 50 50 60 ...
The query for the dataset is:
SELECT LineID
,Hour
,HourValue
,Target
FROM vwData
ORDER BY LineID, CASE WHEN [Hour] > 6 THEN - 1 ELSE [Hour] END
How can I achieve this?
This declares your bit variable (which should be true when they want the 24 hour view - false when 12 hour)
DECLARE #24Hour bit = 0
SELECT CASE WHEN #24Hour = 0
THEN Hour
ELSE Hour + (Hour % 2)
END AS [HourGroup]
,SUM(Target) AS [TargetTotal]
FROM vwData
GROUP BY CASE WHEN #24Hour = 0
THEN Hour
ELSE Hour + (Hour % 2)
END
If they want the 24 hour view, we make hour = hour + hour % 2. (7 = 8, 8=8, 9=10, etc., etc.). If you had a more complex query, I would suggest reading up on cross apply, but this is so simple I think this will suffice. The grouping by makes sure to aggregate the REAL 7 and REAL 8 hour records (which will both be returned as "8", if using the 24 hour view). If you don't group your results, you will get two 8 oclock records - one with the REAL 7 hour total and one with the REAL 8 hour total.
EDIT:
Since you didn't include the schema of your DB, I'm guessing that 'Target' is the value being summated, but it could just as easily be 'HourValue'. Furthermore, I have no idea why you would need LineID, so I omitted it from my answer. But you can easily modify that if it's inaccurate. In the future, you should provide some sample data and your database schema so that others aren't forced to make assumptions or guess.
You could add a calculated field with a value given by something like this: `Fields!Hour.Value + Fields!Hour.Value Mod 2' and then group on that field, using a parameter to choose the Group By field in the report (Your new field or the actual hour value).

How to check if week number is even or odd in ANSI C?

I`ve a small app that returns is week even or not.
time_t now = time(0);
tm *ltm = localtime(&now);
int twin=(ltm->tm_yday/7)%2
But independently from the 1st day of the year so it returns
mon, thu, we, etc
0,1,1,1,1,1,1
in the next week
1,0,0,0,0,0,0
In the next year
mon, thu, we, etc
0,0,1,1,1,1,1
in the next week
1,1,0,0,0,0,0
and so on..
Twin- if number modulo 2 = 0
So I have to add shift to change week number in each sunday or monday. Any suggestions?
You are assuming that first week has exactly 7 days which is incorrect.
For example Jan 1st 2013 was Tuesday, so the first week is only 5 days long.
How about using strftime? Something like:
time_t now = time(0);
tm *ltm = localtime(&now);
char weekNr[3];
strftime(weekNr, sizeof(weekNr), "%W", ltm);
int isOdd = atoi(weeknr) % 2;
What you call twin, in English is usually called even.
About your question, the issue here is that you are not calculating the week number correctly: you are simply dividing by 7, and that's not enough because the start of year and the start of week vary each year.
Moreover, there are several different ways to decide which one is week 1. See for example this code, to get started.
UPDATE: Copying shamelessly from the eglibc source code:
1) The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01 (strftime("%U")):
tp->tm_yday - tp->tm_wday + 7) / 7
2) The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01 (strftime("%W")):
(tp->tm_yday - (tp->tm_wday - 1 + 7) % 7 + 7) / 7
3) The ISO 8601 week number (see NOTES) of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year (strftime("%V")):
Well this is complicated... so you are better with the idea by #MaikuMori of using strftime``, but with"%V", and then parse the result, withatoi()`.

How to segregate string into different column in MATLAB?

How to segregate ddmmyyhhmmss (day, month, year, hour, minute, second) 12 digit number and put in the separate 6 column in MATLAB? I need 7 column now in total. For example, I have following string
051210151255 (which is 05 day, 12 month, 2010 year, 15 hours, 12 minute, 55 second)
Now I need 7 column (whole 12 digit number, dd, mm, yy, hh, mm, ss)
d = '051210151255';
output = zeros(1, 6);
for i=1:6
output(1, i) = str2num(d(i*2-1:i*2));
end
output = [str2num(d) output];
gives:
>> output =
051210151255 5 12 10 15 12 55

Resources