Say I have a nodes person and movie with a relation of [likes]. I am trying to be able to limit the amount of persons that like the same movie without limiting my results to only include 1 movie.
For example:
MATCH (p:Person)-[LIKES]->(m:Movie)
WHERE
p.age < 30
p.gender = "Male"
RETURN p,m
So in the query above I would like to get all the results but filter them so that only 2 Persons will like the same movie.
Is this possible?
This knowledge base article goes over different ways to limit match results per row.
For a non-APOC approach you can get the slice of the collection of people who liked the movie:
MATCH (p:Person)-[:LIKES]->(m:Movie)
WHERE
p.age < 30
p.gender = "Male"
RETURN m, collect(p)[..2] as peopleWhoLiked
If you want a separate row per person, then UNWIND the peopleWhoLiked list before the return.
For the second approach, you'll need APOC Procedures.
In order to use LIMIT, you'll need to first match on all movies, then perform the limited match to :Person nodes using apoc.cypher.run().
To get all movies that have exactly two (under-30 male) likers:
MATCH (p:Person)-[:LIKES]->(m:Movie)
WHERE
p.age < 30 AND
p.gender = "Male"
WITH m, COLLECT(p) AS likers
WHERE SIZE(likers) = 2
RETURN m, likers;
Related
I have docs with following structure:
{
id: 1
type: 1
prop1: "any value"
prop2: "any value"
...
...
}
type can be 1 or 2
Now I would like to create a query which returns all of type 1 and limited (LIMIT = 100) results of type 2 with filtering props and ordering by score.
My try so far is as follow, which isn't correct, resp. sorting by score isn't correct:
I combine two queries:
prepare a first query for using in the mainquery : type:2 AND commonfilters, size=LIMIT, sort by score, ID -> returns a list of id's
main query : (type:1 AND commonfilters) OR (id:[ids from first query]), sort by score, ID
The order isn't correct (sort by score), because it was sorted for two different independent sets of data and not sorted over all id's in combination.
What I need is something like the following SQL Query:
select * from data where commonfilters order by score, id MINUS (select * from data where rowcount > LIMIT)
Does anyone know how to achieve correct ordering for this case?
I wasn't really sure what to title this but this is what I'm trying to do:
I have a table of teams:
teamid | city | name
and I have a table of games played:
gameid | teamid | points
What I'm trying to do is tally the total points scored in all games played by the 'Raptors'.
I've tried it this way:
SELECT COUNT(points) AS "Raptors Points" FROM TEAMS, GAMES
WHERE name = 'Raptors' AND TEAMS.teamid = GAMES.teamid;
and this way:
SELECT COUNT(points) AS "Raptors Points" FROM (SELECT * FROM
TEAMS JOIN GAMES ON TEAMS.teamid = GAMES.teamid WHERE
name = 'Raptors') AS foo;
Both result in a table displaying only points from one of the games (the first one) instead of a total count from all games.
I'm really stumped as to what I'm doing wrong. Any suggestions?
Thanks
Most likely you are seeing the wrong value and thinking it's a proper value. You need to change COUNT(points) with SUM(points)
Count will give you the total number of records, SUM will give you the total number of points
BTW your first query should work if you fix that
I have attached the screenshot. Most probably that the image itself will explain my logic. Let me paste the code I have used in the fields. Product Info1 field which hold Y & N.
RemainingCosu2=If(Invoice Line Items::Product Info1 = GetAsText ("N"); Sum(Cost Total) - Invoice Line Items::VendPaid_total;0)
RemainingCosu1=If(Vendor Status="Partly Paid"; RemainingCosu2; 0)
What should I do to fix this issue?. Please check the screenshot:
Filemaker has no SumIf() function. You need to create a calculation field in the LineItems table, along the lines of:
If ( Paid = "N" ; Cost )
then sum this field at the invoice level (and/or summarize it at the LineItems table itself), instead of the Cost field.
--
BTW, it is much more convenient to define Boolean (yes/no) fields as Number and use the values of 1 for True, 0 (or empty) for False. Then the calculation can be simply:
If ( not Paid ; Cost )
I have a stored procedure (I cannot edit) that I am calling via linq.
The stored procedure returns values (more complex but important data below):
Customer Stock Item Date Price Priority Qty
--------------------------------------------------------
CUST1 TAP 01-04-2012 £30 30 1 - 30
CUST1 TAP 05-04-2012 £33 30 1 - 30
CUST1 TAP 01-04-2012 £29 20 31 - 99
CUST1 TAP 01-04-2012 £28 10 1 - 30
I am trying to limit this list to rows which have unique Dates and unique quantities in LINQ.
I want to remove items with the HIGHER priority leaving rows with unique dates and qty's.
I have tried several group by's using Max and order by's but have not been able to get a result.
Is there any way to do this via linq?
EDIT:
Managed to convert brad-rem's answer into VB.net.
Syntax below if anyone needs it:
returnlist = (From p In returnlist
Order By p.Qty Ascending, p.Priority
Group By AllGrp = p.Date, p.Qty Into g = Group
Select g.First).ToList
How about the following. It groups by Date and Qty and orders it so that the lower priorities come first. Then, it just selects the first item from each group, which are all the lower priority items:
var result = from d in dbData
orderby d.Priority
group d by new
{
d.Date,
d.Qty
} into group1
select group1.First();
I have a project where I must make the following;
You have a small business and you sell 6 different products. Choose your products
and their prices within the range of 20p to £25.00 (these could be completely fictitious). Your
shop has 4 employees, one of whom will be at the till at the time of purchase.
Your task is to write MATLAB code to prepare a receipt for a fictitious transaction as explained
below.
There is a customer at the till. They want to purchase 3 random products with specific
quantities for each. For example, the customer wants 2 cappuccinos, 1 croissant and 6 raspberry
muffins.
(1) Select randomly 3 products from your list. For each product choose a random quantity
between 1 and 9.
(2) Calculate the total cost.
(3) Choose randomly the staff member to complete the transaction.
(4) Suppose that the price includes 20% VAT. Calculate the amount of VAT included in the price.
(6) Prepare the receipt as text in the MATLAB command window. Use the current date and time
(check datestr(now,0)).
Your code should output the receipt in the format shown in the picture. There should be
60 symbols across. Choose our own shop name.
My code so far is the following:
clear all
clc
close all
items = {'apples ','carrots ','tomatoes','lemons ','potatoes','kiwis '};% products
price = {3.10, 1.70, 4.00, 1.65, 9.32, 5.28};% item prices. I set spaces for each entry in order to maintain the border format.
employee = {'James','Karina','George','Stacey'};%the employees array
disp(sprintf('+-----------------------------------------------+'));
disp(sprintf('|\t%s \t\t\tAlex''s Shop |\n|\t\t\t\t\t\t\t\t\t\t\t\t|', datestr(now,0)));
totalPrice = 0;
for i = 1:3
randItems = items {ceil(rand*6)};
randprice = price {ceil(rand*6)};
randQuantity = ceil(rand*9);% random quantity from 1 to 9 pieces
randEmployee = employee{ceil(rand*4)};
itemTotal = randprice * randQuantity;%total price of individual item
totalPrice = totalPrice + itemTotal;
disp(sprintf('|\t%s\t (%d) x %.2f = £ %.2f \t\t\t|', randItems, randQuantity, randprice, itemTotal))
end
disp(sprintf('|\t\t\t\t-----------------------------\t|'));
disp(sprintf('|\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t Total to pay \t £ %.2f\t\t\t\t|',totalPrice));
disp(sprintf('|\t VAT \t\t\t\t £ %.2f\t\t\t\t| \n|\t\t\t\t\t\t\t\t\t\t\t\t|', totalPrice*0.2));
disp(sprintf('|\tThank you! You have been served by %s\t|\t', randEmployee));
disp(sprintf('+-----------------------------------------------+'));
My issue of course is the following. Upon choosing a random item from the items list, I then choose a random price to assign as well. I don't want this though. I would like to find a way to assign a preset price to each item to be printed automatically when generating a random item to be added to the basket. I hope this explanation is sufficient for you, if you have any questions feel free to ask. Thank you in advance.
When you write
randItems = items {ceil(rand*6)};
randprice = price {ceil(rand*6)};
you calculate a random index into the array items, and then you calculate a random index into the array price. If you instead assign the index you calculate via ceil(rand*6) to a separate variable, called e.g. index, you can re-use it to pick, say, item #3 from both items and price. Thus, the ith item will always show up with the ith price.