How to remove a relation in surrealdb - surrealdb

RELATE (
SELECT * from user where username = 'user_male'
)->interested_in->gender:female;
I used the above RELATE statement to specify that the user_male user is interested in women.
I can't figure out how to UNRELATE this user to that gender; or for example to SELECT all users that are interested in women

The edge interested_in becomes its own table from which you can delete rows with the DELETE statement.
To "unrelate" the relation, we only need to delete every entry from interested_in where in.username is 'user_male and where out is gender:female.
DELETE interested_in WHERE in.username = 'user_male' AND out = 'gender:female';

Related

Laravel 8 - count clicks but user can't spam the function

In my website I track clicks on a specific link and save the count on database with this function:
public function redirect($url)
{
$data = Class::where('url', $url)->first('clicks');
$cnt = $data->clicks;
$cnt++;
$sql = Class::where('url', $url)->update([
'clicks' => $cnt,
]);
if (!$sql) {
abort(500);
}
return redirect('https://website.com/'.$url);
}
the problem is if someone spam the link , the count goes up. I want stop count if user clicked on link 5mins ago.
he will redirected to the link but count doesn't go up.
I'm new so it's so good if you explain it with details. Thanks
I would create a new table, lets call it link_clicks. You will need 3 columns, one to identify the person, one to get the time and one to identify the link (I dont exactly know how you store the links you want to observe).
So more or less you will have the following table:
link_clicks
user_id
link_id
created_at
If the users are always logged in, I would store the user_id, if they are not logged in, I would store the IP-address. So instead of user_id make a column called ip or ip-address.
Afterwards you can easily get the last click and its time.
--Example
Your new table contains following entry:
user_id = 1, link_id = 1 and created_at = 2021-04-21 19:00:00
Now in your controller you get the current date_time date("Y-m-d H:i:s") and the user id like that: auth()->id(). You can also define your time treshold in minutes e.g max_dif = 5.
Afterwards you can query the table for all clicks for the user. You can either make the time comparision in your where() statement or you can make some comaprison in your php code to check if the time treshhold is reached.

DAX/POWERBI : count tickets attached to an item and all its child items

I am new to DAX and PowerBI and have a problem to write DAX formulas for my case:
I have two tables: Assets and Tickets. Each have an Id, and the Assets have a ParentAssetId (can be 0 or None).
In a DAX expression: I would like to count (and list) all the tickets attached to an Asset and its children.
I tried this way but without success:
nbChildTickets =
VAR mykey =
SELECTEDVALUE ( Assets[AssetKey] )
VAR mypar =
SELECTEDVALUE ( Assets[ParentId] )
RETURN
CALCULATE(
COUNTX(Tickets, Tickets[TicketKey]),
FILTER(Tickets, RELATED(Assets[ParentId]) = mykey)
)
The Tables and the Canvas
It is the asset table which contains both the AssetKey and the ParentId colums.
Have any idea or tuto to do this ?
Thanks
A question, are these tables already related? In case they are, it seems that you wouldn't need a measure like that to get the count of # of tickets by asset and its parent. I would do it in the next two ways. Supposing you need it in a table:
Option 1.
Create a simple count measure to get the number of tickets in Tickets table, it could be something like Number of Tickets = COUNTROWS('Tickets')
Drag a table to the canvas.
Add to the table Assets and its children columns, finally add your new measure to the table
Option 2. In case each ticket has an ID.
Drag a table to the canvas.
Add to the table Assets and its children columns. Also add your tickets Id.
At Fields section (where you drag and drop your columns, right click and select the Count option.
Done
Remember, it is important that your both tables are already related to work. Otherwise Power BI will not know how to calculate and displays this combination of data for you.
First create two relationships between Assets and Ticket table. One relationship will be active (one to many) on column AssetKey Column.
Second relationship will be Inactive. Asset[AssetKey] = Ticket[ParentID]
Now use the below Measures -
Number Of Tickets = COUNT(Tickets[TicketKey])
Number of Child = CALCULATE(COUNT(Tickets[AssetKey]),USERELATIONSHIP(Asset[AssetKey],Tickets[ParentId]))
Relationship diagram is in below image :
Output is mentioned below:
The blank row can be eliminated from visual filters :

SQL Statement to delete only one row out of duplicates

So I am working in Ruby, and say I have 6 rows in a table of two columns that are exactly identical. In my case, my table "campaign_items" has two columns "campaign_name" and "item." I would like to delete only one row out of the 6 duplicates using a single query. I started with this:
db.exec("DELETE FROM products WHERE campaign_name = '#{camp_name}' AND product_type = 'fleecejacket' AND size = '#{size_array[index]}'")
Which of course deleted all items of that condition. So I found in another question an answer along these lines:
db.exec("DELETE FROM products a WHERE a.ctid <> (SELECT min(b.ctid) FROM products b WHERE a.key = b.key)")
However, this would delete all duplicates except for one. I have not found a way that only deletes a SINGLE row that has duplicates. Is there a delete top query that I am looking for? Thanks in advance.
Edit: I also have a column "id" which is a primary key.
So I definitely overthought this, but all that is needed is this:
x = db.exec("SELECT * FROM campaign_items WHERE campaign_name = '#{camp_name}' AND item = 'fleecejacket'")
id = x[0]['id']
db.exec("DELETE FROM campaign_items WHERE campaign_name = '#{camp_name}' AND item = 'fleecejacket' AND id = '#{id}'")
Get the unique id from the first duplicate (since it doesn't matter which one is deleted) and delete the row with that id.

Neo4j - return only one node that has multiple relations

I'm having a small issue finding out how to return one node, that has multiple outgoing relations.
So what I want is to display only node, even if it has more than one relation; this is my query:
MATCH total=(n:Employee)-[r:WorkedOn]->(p:Project)
RETURN toFloat(p.total_efficiency) / toFloat(count(p)) as score , n.first_name as name, n.last_name as surname, r.role as role, n.start_date_of_work as startDate, n.experience as experience,
n.email as email, n.age as age, collect(p.name) as projects ORDER BY score DESC LIMIT {l}
but this returns a table like this:
How do I solve the double 'Jari Van Melckebeke' records? I only want one.
I could also remove the 'role' property, but I need the Project object anyway to calculate the score...
Thanks in advance,
Jari Van Melckebeke
You have two options to collapse this into one row. Either, as you suggested, removing role from your return, or returning COLLECT(r.role) as roles.

Efficient algorithm that takes a Twitter user and finds top users by order of how many of his followers they follow

The title is very wordy. So I'll explain with an example.
We have a database of 10,000 twitter users with each following up to 2000 users. The algorithm takes as input one random never before seen user (including the people that follow him), and returns the twitter users from the database by order of how many of his followers they follow.
i.e.
We have:
User A follows 1,2,3,4
User B follows 3,4,5,6
User C follows 4,8,9
We enter user X who has users 3,4,5 following him.
The algorithm should return:
B: 3 matches (3,4,5)
A: 2 matches (3,4)
C: 1 match (4)
Store the data as a sparse integer matrix A of size 10^5x10^5 with ones at the appropriate places. Then, given a user i, compute A[i,] * A (matrix multiplication). Then sort.
Assuming you have a table structure similar to this:
Table Users
Id (PK, uniqueidentifier, not null)
Username (nvarchar(50), not null)
Table UserFollowers
UserId (FK, uniqueidentifier, not null)
FollowerId (uniqueidentifier, not null)
You can use the following query to get the common parents of followers of the followers of the user in query
SELECT Users_Inner.Username, COUNT(Users_Inner.Id) AS [Total Common Parents]
FROM Users INNER JOIN
UserFollowers ON Users.Id = UserFollowers.FollowerId INNER JOIN
UserFollowers AS UserFollowers_Inner ON UserFollowers.FollowerId = UserFollowers_Inner.UserId INNER JOIN
Users AS Users_Inner ON UserFollowers_Inner.FollowerId = Users_Computed.Id
WHERE (UserFollowers.UserId = 'BD34A1FF-FCF5-4D35-B8A3-EFFB1587A874')
GROUP BY Users_Inner.Username
ORDER BY COUNT(Users_Inner.Id) DESC
would something like this work?
for f in followers(x)
for ff in followers(f)
count[ff]++ // assume it is initially 0
sort the ff-s by their counts
Unlike the matrix solution, the complexity of this is proportional to the number of people involved rather than the number of users on twitter.

Resources