Linq to Entities 3.5 where most recent date - asp.net

I have a table and for the sake of the example lets say it has three columns: Name, DateAdded, Note. In this table there will be multiple records with the same name. I need a linq where clause that will get me a result set with the most recent record for each name.
So if I had the following information:
Name, DateAdded, Note
Alpha, 1/1/2010, note one
Alpha, 1/2/2010, note two
Alpha, 1/3/2010, note three
Beta, 1/4/2010, note four
Beta, 1/5/2010, note five
I would expect to get the following results:
Name, DateAdded, Note
Alpha, 1/3/2010, note three
Beta, 1/5/2010, note five
In real use this is going to be a search option so I need it to be a .Where clause I can append to an IQueryable collection if possible.

from row in rows
group row by row.Name into grouping
from g in grouping
where g.DateAdded == grouping.Max(x => x.DateAdded)
select g;
If there's more than one row with the maximum DateAdded in some group, this returns all such rows.
You don't need just one Where clause. If rows is a IQueryable, then the result of this query is too, so you can use it as a part of a chain. But I have no idea how well does this query translate to SQL.

Related

Clickhouse topK query on several columns

In ClickHouse, is there any way use the topK query on more than the column ,
for example:
select topK(10)(AGE,COUNTRY) ...
meaning I want the top10 combinations of AGE+COUNTRY,
I only found a workaround using concat on fields and topK on them, wondered if there is any other way.
You can pass array (or tuple) of columns to topK:
SELECT topK(10)([Age, Country])
FROM table
Or use the straightforward calculation (it is much slower but provides the exact result):
SELECT
Age,
Country
FROM table
GROUP BY
Age,
Country
ORDER BY count() DESC
LIMIT 10

Express conditions on two consecutive variable length relationships?

How to express a conditions for two consecutive variable length relationships?
Consider this partial query
MATCH(t1:Type{myID: 1})-[r:relType]->(:Type)-[rels:relType*0..]-(t2:Type{myID:100})
WHERE r.attr1>10
Basically I am trying to saying that there could be one or more relations from t1 to t2. The first relation r should satisfy a given condition on its attribute.
If this is the only relation between the two nodes then it's ok.
It at least another relation exist I want to add another condition such as:
WHERE r.attr1>10 AND r_next.attr2> r_prev.attr2+r_prev.attr1
where r_next and r_prev are consecutive relations: ()-[r_prev]->()-[r_next]-(). Note that at the first step r_prev is the first relation r.
I know rels is a collection but I do not know how to express such a condition.
Consecutive comparison like this isn't easy at this time, and it can't currently be evaluated during expansion.
You can do some filtering on this after, but it will be ugly.
We'll make use of the APOC Procedures for apoc.coll.pairsMin(), which takes a collection and returns a list of adjacent pairs.
MATCH (t1:Type{myID: 1}), (t2:Type{myID:100})
MATCH (t1)-[r:relType]->(:Type)-[rels:relType*0..]-(t2)
WHERE r.attr1>10
WITH t1, t2, apoc.coll.pairsMin(rels) as pairs
WHERE all(pair in pairs WHERE pair[0].attr1 + pair[0].attr2 < pair[1].attr2)
RETURN t1, t2 //or whatever you want to return from this

Neo4j MATCH then MERGE too many DB hits

This is the query:
MATCH (n:Client{curp:'SOME_VALUE'})
WITH n
MATCH (n)-[:HIZO]-()-[r:FB]-()-[:HIZO]-(m:Client)
WHERE ID(n)<>ID(m)
AND NOT (m)-[:FB]->(n)
MERGE (n)-[:FB]->(m) RETURN m.curp
Why is the Merge stage getting so many DB hits if the query already narrowed down
n, m pairs to 6,781 rows?
Details of that stage shows this:
n, m, r
(n)-[ UNNAMED155:FB]->(m)
Keep in mind that queries build up rows, and operations in your query get run on every row that is built up.
Because the pattern in your match may find multiple paths to the same :Client, it will build up multiple rows with the same n and m (but possibly different r, but as you aren't using r anywhere else in your query, I encourage you to remove the variable).
This means that even though you mean to MERGE a single relationship between n and a distinct m, this MERGE operation will actually be run for every single duplicate row of n and m. One of those MERGEs will create the relationship, the others will be wasting cycles matching on the relationship that was created without doing anything more.
That's why we should be able to lower our db hits by only considering distinct pairs of n and m before doing the MERGE.
Also, since your query made sure we're only considering n and m where the relationship doesn't exist, we can safely use CREATE instead of MERGE, and it should save us some db hits because MERGE always attempts a MATCH first, which isn't necessary.
An improved query might look like this:
MATCH (n:Client{curp:'SOME_VALUE'})
WITH n
MATCH (n)-[:HIZO]-()-[:FB]-()-[:HIZO]-(m:Client)
WHERE n <> m
AND NOT (m)-[:FB]->(n)
WITH DISTINCT n, m
MERGE (n)-[:FB]->(m)
RETURN m.curp
EDIT
Returning the query to use MERGE for the :FB relationship, as attempts to use CREATE instead ended up not being as performant.

Aggregate SQLite function - unexpected result

This sqlite query returns zero rows as expected:
SELECT 1 FROM tbl WHERE 0;
This query, on the other hand, returns one row containing a null column:
SELECT MAX(1) FROM tbl WHERE 0;
Why does the second query return one row, rather than zero rows?
'Normal' queries return one result for each (filtered) record of the table in the FROM clause.
However, when you are using some aggregate function, the result has one record for each group in the source table. And if there is no GROUP BY clause, the entire table is one group.
With GROUP BY, there are no results for empty groups.
However, without GROUP BY, you always have exactly one group and get one result record, even if that group ends up being empty.
Your query would get zero rows with MAX if it were also using GROUP BY (with a constant value, which would put all records, if there were any, into a single group):
SELECT MAX(1) FROM tbl WHERE 0 GROUP BY NULL;

How to create a view that returns a 2x2 (or NxN) matrix of results

So I know enough SQL just to be really dangerous (I don't normally work the back-end) but cannot get the following view to be created successfully ;) The result set I'm after is a data set that has rows assigned as a column alias from multiple tables (instead of a 1xN flat of all columns). There is a many-to-one relationship when looking at the main table, based on foreign keys associated to the row id of the appropriate related table.
Ideally I'd like a data set that looks like this in the return:
dataset.transaction_row[n]: col1, col2, col3, coln... (columns from the transaction table)
dataset.category_row[n]: col1, co2, col3, coln... (columns from the category table)
and so on...
I get the following error:
Query Error: near "AS": syntax error Unable to execute statement
From:
CREATE VIEW view_unreconciled_transactions
AS SELECT account_transaction.* AS transaction_row,
category.* AS category_row,
memorized.name_rule_replace OR account_transaction.name AS payee
FROM account_transaction
LEFT JOIN memorized ON account_transaction.memorized_key = memorized.id
LEFT JOIN category ON account_transaction.category_key = category.id
WHERE status != 2
ORDER BY account_transaction.dt_posted DESC
It seems easy enough since the result-column selector is repeatable which includes expressions (referencing sqlite's syntax diagrams). In reference to the error, I'm assuming it's complaining about the 2nd 'AS' where I'm trying to get table.* assigned as an alias. Any help in the right direction is appreciated. If I had to, I suppose I could explicitly state all columns but that feels like a kludge.
The AS modifier can only be applied to a single column, not to a collection such as the * you used. You will have to break them out into specific names, (which is best practice IMHO anyway)
It looks like you want to make a "pivot table". They can be tricky to make in a database. I can say that if you a data result, where each row comes from a different table source, and the columns form each table are IDENTICAL, then you could try using a UNION statement to join the different results together like they are just one dataset.
NOTE that the columns all take their naming cue from the first dataset in a UNION and the datatype all need to be the same.

Resources