I want to show occupied slots and available slots of interviewer from users table for a day and for remaining week by selecting date time - sql

I am not an expert in writing postgresql queries, need some help in here.
I am using a tool to make a dashboard retool.com.
I have 2 tables users and interviewers. A user is an interviewer as well as interviewee. When users id is saved as interviewer_id in interviews table he is considered as interviewer, in the same way when the user id is saved as interviewee_id he is considered as interviewee. start_time column in interviews saves the interview start time. interviews_slots column in users table has the interviewer slots in json form.
see the sample data here.
`
{
"monday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"tuesday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"wednesday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"thursday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"friday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
]
}
This is my sql statement.
SELECT users_tbl.id,
users_tbl.name,
(select name from users where id = interview_tbl.interviewee_id) as interviewee,
interview_tbl,start_time,
interview_tbl.duration,
interview_slots,
concat(users_tbl.interview_slots->'monday'->0->'start-time',' - ', users_tbl.interview_slots->'monday'->0->'end-time')
as Monday,
concat(users_tbl.interview_slots->'tuesday'->0->'start-time',' - ', users_tbl.interview_slots->'tuesday'->0->'end-time')
as Tuesday,
concat(users_tbl.interview_slots->'wednesday'->0->'start-time',' - ', users_tbl.interview_slots->'wednesday'->0->'end-time')
as Wednesday,
concat(users_tbl.interview_slots->'thursday'->0->'start-time',' - ', users_tbl.interview_slots->'thursday'->0->'end-time')
as Thursday,
concat(users_tbl.interview_slots->'friday'->0->'start-time',' - ', users_tbl.interview_slots->'friday'->0->'end-time')
as Friday,
concat(users_tbl.interview_slots->'saturday'->0->'start-time',' - ', users_tbl.interview_slots->'saturday'->0->'end-time')
as Saturday,
concat(users_tbl.interview_slots->'sunday'->0->'start-time',' - ',users_tbl.interview_slots->'sunday'->0->'end-time')
as Sunday
FROM users as users_tbl
JOIN interviews interview_tbl
ON users_tbl.id=interview_tbl.interviewer_id
AND interview_tbl.created_at >= CURRENT_DATE;
I want to query out the data based on the interview start_time
suppose a interview is set for Tuesday i.e 20th Dec at 10:00 am for 1 hour, i need to display a interviewer is available slots for Tuesday like
consider the sample data is used to display
Interviewer
Tuesday occupied slots
Available Slots
Batman
20th Dec at 10:00 am
9:00 - 10:00,
11:00 - 17:30.
Again in need to display similar data for the whole current week.
Thanks in advance.

Related

Joining events into a single row

I have some events that capture the times when different jobs start or end. Here are two events that capture the start and end times of a job-
[
{
"appName": "a1",
"eventName": "START",
"eventTime": "t1"
},
{
"appName": "a1",
"eventName": "END",
"eventTime": "t2"
},
{
"appName": "a1",
"eventName": "START",
"eventTime": "t3"
},
{
"appName": "a2",
"eventName": "START",
"eventTime": "t4"
}
]
I am looking to visualize this information in a table showing the latest start and end times of each application, something like this -
--AppName--Last Start Time--Last End Time--
--a1--t3--t2--
--a2--t4--null--
The above table is assuming t3 comes after t1. How do i get to this ? I am able to extract the latest events for each into separate rows with this -
stats latest(eventTime) by appName, eventName but need them to be combined into one single tuple.
Create separate fields for start and end times, then use stats to get the latest for each.
| eval start_time=if(eventName="START", eventTime, null())
| eval end_time=if(eventName="END", eventTime, null())
| stats latest(start_time) as last_start, latest(end_time) as last_end by appName
XXX Note that for the latest function to work properly, eventTime must be in epoch format. If it isn't, use the strptime function in a eval to convert it. XXX (Disregard this last part.)

How count e.g. ice days over month/years

I have a weather station with data over 14 years every 10min: like this
_id:60fbcf880000000000000000
datum:2021-07-24T08:30:00.000+00:00
temperature:19.5
Now I want to count per year and per month certain days.
Ice days (temperature is a 24h always below 0°),
winter days (temperature is at a time under 0°)
cold days (max temp <10°C)
hot days (max temp 25-30°C)
very hot days (max temp over 30°).
I have no real clue about the best query code for mongo.
I can group for days, but then I have the issue to count those certain days ($buckets?)
I come until the grouping:
{$group: {
_id: [{$year: '$datum'}, <br>
{$month: '$datum'}, <br>
{$dayOfMonth: '$datum'}], <br>
temp_avg: {$avg: '$tempAussen'}, <br>
temp_min: {$min: '$tempAussen'}, <br>
temp_max: {$max: '$tempAussen'}, <br>
}}
Result in a list with elements like:
_id:Array
0:2020
1:3
2:2
temp_avg:6.12
temp_min:0.7
temp_max:9.6
But now starts my problem: How to count the days for (e.g. 2020 month 3) with temperature <0° and the other days?
Your grouping is working fine. You just need one more extra $group by month to sum conditionally for each type of day. For example, you can do
{
$group: {
_id: {
"year": "$_id.year",
"month": "$_id.month",
},
winter_days_count: {
$sum: {
"$cond": {
"if": {
$lt: [
"$temp_min",
0
]
},
"then": 1,
"else": 0
}
}
}
...
to count the winter days in the month.
Here is the Mongo playground for your reference.

Can Azure Cosmos DB do this kind of query?

I have a JSON object stored in Azure Cosmos DB, and I'm seeing if there's a way to write workable queries doing basic things like Order By.
The structure looks something like :
[
{
"id":"id1",
"title":"test title",
"dataRecord":{
"version":1,
"dataRecordItems":[
{
"itemTitle":"item title 1",
"type":"string",
"value":"My First Title"
},
{
"itemTitle":"item number",
"type":"number",
"value":1
},
{
"itemTitle":"date",
"type":"date",
"value":"21/11/2019 00:00:00"
}
]
}
},
{
"id":"id2",
"title":"test title again",
"dataRecord":{
"version":1,
"dataRecordItems":[
{
"itemTitle":"item title 2",
"type":"string",
"value":"My Second Title"
},
{
"itemTitle":"item number",
"type":"number",
"value":2
},
{
"itemTitle":"date",
"type":"date",
"value":"20/11/2019 00:00:00"
}
]
}
]
I can use ARRAY_CONTAINS to find objects with a particular value, but I run into all kinds of issues if I try to sort by an the value of an object which has the title of "date".
So, as an example, I'd like to be able to say something like (pseudoish code here):
SELECT * FROM c WHERE
ARRAY_CONTAINS(c.dataRecord.dataRecordItems,
{"itemTitle":"item title 2", "value" : "My Second Title"}, true)
AND
ARRAY_CONTAINS(c.dataRecord.dataRecordItems,{"itemTitle":"item number", "value" : 2}, true)
ORDER BY < *** SOMEHOW GET THE DATE HERE from itemTitle = date ***
Then, in this simple case, I would everything returned, but ordered by date.
Obviously in the future I would be pulling out individual fields, but it's all kind of moot if I can't do the first part.
Just wondering if anyone has any great ideas.
Cheers!
You need to store the date in ISO 8601 format:
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
https://www.w3.org/TR/NOTE-datetime

Data which relates to multiple Schemas in MongoDB?

I have a front-end app which sometimes requests all the days of a month, sometimes the weeks of the year (more precisely: the days these weeks contain);
I thought of saving everything in mongodb like this:
{
month: 'Feb',
weeks: [
[{ day: 'Mo', etc. }, { day: 'Tu', etc. } ],
[ week2 here etc.] ], //etc.
]
},
{
month: 'Mar',
weeks: [
[{ day: 'Mo', etc. }, { day: 'Tu', etc. } ],
[ week2 here etc.] ], //etc.
]
}
... but this won't work for obvious reasons. Not every month has 4 weeks starting from Monday etc.
How would I realise something like this in MongoDB (roughly), so that my front-end app can request months (=an array of all the days of a month), but also weeks (= an array of 7 day-objects, starting from Monday)? I would really appreciate some hints here, as I am a novice.

Elasticsearch how to quickly find the result when search array fields

My JSON data:
{
"date": 1484219926,
"uid": "1234567",
"interest": [
"2000001",
"2000002",
"....",
"2000xxx"
],
"other": "xxxx"
}
The search result as the following SQL:
select count(*)
from xxxxx
where date > time1 and
date < time2 and
interest="20000xxx"
The filed interest may have 500 itmes. I want to quickly get the search result, what shou I do? The total data may be 2 billion.