Is it possible to do union of two queries (from the same entity) in core data? In SQL speak, if entity is called t, then consider that T has following data:
+------+------+------+
| x | y | z |
+------+------+------+
| 1 | 11 | 2 |
| 1 | 12 | 3 |
| 2 | 11 | 1 |
| 3 | 12 | 3 |
Then I am trying to run the following query (using core data - not SQLite)
select x, y, sum(z)
from t
group by 1, 2
union
select x, 1 as y, sum(z)
from t
group by 1, 2
order by x, y, 1
;
+------+------+--------+
| x | y | sum(z) |
+------+------+--------+
| 1 | 1 | 5 |
| 1 | 11 | 2 |
| 1 | 12 | 3 |
| 2 | 1 | 1 |
| 2 | 11 | 1 |
| 3 | 1 | 3 |
| 3 | 12 | 3 |
+------+------+--------+
7 rows in set (0.00 sec)
Is it possible?
Thanks!
Related
I have a Cypher query that shows the following output:
+----------------
| usid | count |
+----------------
| "000" | 1 |
| "000" | 0 |
| "000" | 0 |
| "001" | 1 |
| "001" | 1 |
| "001" | 0 |
| "002" | 2 |
| "002" | 2 |
| "002" | 0 |
| "003" | 4 |
| "003" | 2 |
| "003" | 2 |
| "004" | 4 |
| "004" | 4 |
| "004" | 4 |
+----------------
How can I get the below result with the condition SUM(count) <= 9.
+----------------
| usid | count |
+----------------
| "000" | 1 |
| "001" | 2 |
| "002" | 4 |
| "003" | 8 |
+----------------
Note: I have used the below query to get the 1st table data.
MATCH (us:USER)
WITH us
WHERE us.count <= 4
RETURN us.id as usid, us.count as count;
I don't know how you get your original data, so I will just use a WITH clause and assume the data is there:
// original data
WITH usid, count
// aggregate and filter
WITH usid, sum(count) as new_count
WHERE new_count <= 9
RETURN usid, new_count
Based on the updated question, the new query would look like:
MATCH (us:USER)
WHERE us.count <= 4
WITH us.id as usid, sum(us.count) as count
WHERE new_count <= 9
RETURN usid, count
˙˙˙
How to join left next row in Cloud Spanner.
I want to calculate how many kilometers driven each driver.
My table looks like:
vehicle_id | driver_id | odometer
1 | 1 | 10
1 | 1 | 20
1 | 2 | 20
1 | 2 | 40
1 | 1 | 40
1 | 1 | 50
2 | 1 | 10
2 | 1 | 20
2 | 2 | 20
2 | 2 | 30
2 | 1 | 30
2 | 1 | 80
2 | 2 | 80
2 | 2 | 120
Results should be:
driver_id | total_mileage
1 | 80
2 | 70
My solution is:
SUM (mileage)
FROM (SELECT (odometer2-odometer) AS mileage
FROM (SELECT vehicle_id , odometer ,driver_id ,
NEXT.driver_id AS driver_id 2, NEXT.odometer AS odometer2 FROM Table
**JOIN NEXT ROW** AS NEXT
)
WHERE driver_id=driver_id2
)
GROUP BY driver_id
vehicle_id | driver_id | odometer | driver_id2 | odometer2 |mileage
1 | 1 | 10 | 1 | 20 | 10
1 | 1 | 20 | 2 | 20 | -
1 | 2 | 20 | 2 | 40 | 20
1 | 2 | 40 | 1 | 40 | -
1 | 1 | 40 | 1 | 50 | 10
1 | 1 | 50 | - | - | -
2 | 1 | 10 | 1 | 20 | 10
2 | 1 | 20 | 2 | 20 | -
2 | 2 | 20 | 2 | 30 | 10
2 | 2 | 30 | 1 | 30 | -
2 | 1 | 30 | 1 | 80 | 50
2 | 1 | 80 | 2 | 80 | -
2 | 2 | 80 | 2 | 120 | 40
2 | 2 | 120 | - | - | -
In Cloud Spanner function Row_number, OVER, LAG do not exist.
My question is how to join left next row in Cloud Spanner?
You're looking to PIVOT the data. This is not something that is doable directly in Cloud Spanner SQL - you'll need to post-process the data to affect the pivot.
My problem on getting one row shown with two null values.
FROM tuotemerkki;
tmtunnus | tmnimi | maa
----------+----------+-------------
1 | McCee | Yhdysvallat
2 | KooTek | Italia
3 | Giardino | Italia
(3 rows)
FROM tuote;
ttunnus | tnimi | kuvaus | suositushinta | tmtunnus
---------+-----------------------+--------------------+---------------+----------
111 | Trimmeri TRCee | tehokas 4-tahtinen | 179.00 | 1
112 | Trimmerisiima Cee | laadukas siima | 6.99 | 1
113 | Moottorisaha MSCee RR | robusti ja raskas | 559.00 | 1
114 | Trimmerisiima Y | yleissiima | 3.99 | 2
115 | Lapio L | kevyt yleislapio | 23.95 | 2
(5 rows)
I need to get this selected with the NULL VALUE on giardino from tmnimi.
tmnimi | tnimi | kuvaus
----------+-----------------------+--------------------
McCee | Trimmeri TRCee | tehokas 4-tahtinen
McCee | Trimmerisiima Cee | laadukas siima
McCee | Moottorisaha MSCee RR | robusti ja raskas
KooTek | Trimmerisiima Y | yleissiima
KooTek | Lapio L | kevyt yleislapio
Giardino | |
(6 rows)
I get only this selected
SELECT tmnimi, tnimi, kuvaus
FROM tuote
CROSS JOIN tuotemerkki
WHERE tuote.tmtunnus = tuotemerkki.tmtunnus;
tmnimi | tnimi | kuvaus
--------+-----------------------+--------------------
McCee | Trimmeri TRCee | tehokas 4-tahtinen
McCee | Trimmerisiima Cee | laadukas siima
McCee | Moottorisaha MSCee RR | robusti ja raskas
KooTek | Trimmerisiima Y | yleissiima
KooTek | Lapio L | kevyt yleislapio
(5 rows)
Use a left join between the two tables:
SELECT
t1.tmnimi,
t2.tnimi,
t2.kuvaus
FROM tuotemerkki t1
LEFT JOIN tuote t2
ON t1.tmtunnus = t2.tmtunnus
When you left join from tuotemerkki to tuote, then every record in the former table is guaranteed to appear in the result set. Since the Giardino record does not match to anything in the tuote table, all the columns from that table would have a NULL value for the Giardino record.
I have a tree structure like node(1)->node(2)->node(3). I have name as an property used to retrieve a node.
Given a node say node(3), i wanna retrieve node(1).
Query tried :
MATCH (p:Node)-[:HAS*]->(c:Node) WHERE c.name = "node 3" RETURN p LIMIT 5
But, not able to get node 1.
Your query will not only return "node 1", but it should at least include one path containing it. It's possible to filter the paths to only get the one traversing all the way to the root, however:
MATCH (c:Node {name: "node 3"})<-[:HAS*0..]-(p:Node)
// The root does not have any incoming relationship
WHERE NOT (p)<-[:HAS]-()
RETURN p
Note the use of the 0 length, which matches all cases, including the one where the start node is the root.
Fun fact: even if you have an index on Node:name, it won't be used (unless you're using Neo4j 3.1, where it seems to be fixed since 3.1 Beta2 at least) and you have to explicitly specify it.
MATCH (c:Node {name: "node 3"})<-[:HAS*0..]-(p:Node)
USING INDEX c:Node(name)
WHERE NOT (p)<-[:HAS]-()
RETURN p
Using PROFILE on the first query (with a numerical id property instead of name):
+-----------------------+----------------+------+---------+-------------------------+----------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------------+----------------+------+---------+-------------------------+----------------------+
| +ProduceResults | 0 | 1 | 0 | p | p |
| | +----------------+------+---------+-------------------------+----------------------+
| +AntiSemiApply | 0 | 1 | 0 | anon[23], c -- p | |
| |\ +----------------+------+---------+-------------------------+----------------------+
| | +Expand(All) | 1 | 0 | 3 | anon[58], anon[67] -- p | (p)<-[:HAS]-() |
| | | +----------------+------+---------+-------------------------+----------------------+
| | +Argument | 1 | 3 | 0 | p | |
| | +----------------+------+---------+-------------------------+----------------------+
| +Filter | 1 | 3 | 3 | anon[23], c, p | p:Node |
| | +----------------+------+---------+-------------------------+----------------------+
| +VarLengthExpand(All) | 1 | 3 | 5 | anon[23], p -- c | (c)<-[:HAS*]-(p) |
| | +----------------+------+---------+-------------------------+----------------------+
| +Filter | 1 | 1 | 3 | c | c.id == { AUTOINT0} |
| | +----------------+------+---------+-------------------------+----------------------+
| +NodeByLabelScan | 3 | 3 | 4 | c | :Node |
+-----------------------+----------------+------+---------+-------------------------+----------------------+
Total database accesses: 18
and on the second one:
+-----------------------+----------------+------+---------+-------------------------+------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------------+----------------+------+---------+-------------------------+------------------+
| +ProduceResults | 0 | 1 | 0 | p | p |
| | +----------------+------+---------+-------------------------+------------------+
| +AntiSemiApply | 0 | 1 | 0 | anon[23], c -- p | |
| |\ +----------------+------+---------+-------------------------+------------------+
| | +Expand(All) | 1 | 0 | 3 | anon[81], anon[90] -- p | (p)<-[:HAS]-() |
| | | +----------------+------+---------+-------------------------+------------------+
| | +Argument | 1 | 3 | 0 | p | |
| | +----------------+------+---------+-------------------------+------------------+
| +Filter | 1 | 3 | 3 | anon[23], c, p | p:Node |
| | +----------------+------+---------+-------------------------+------------------+
| +VarLengthExpand(All) | 1 | 3 | 5 | anon[23], p -- c | (c)<-[:HAS*]-(p) |
| | +----------------+------+---------+-------------------------+------------------+
| +NodeUniqueIndexSeek | 1 | 1 | 2 | c | :Node(id) |
+-----------------------+----------------+------+---------+-------------------------+------------------+
Total database accesses: 13
what should one specify when creating a graphlab recommender model such that the item that a user already own is not recommended to him again? Can this be done directly by specifying certain parameters or do I need to write a recommender from scratch.? data would look something like this
| user_id | item_id | othercolumns |
|:-----------|------------:|:------------:|
| 1 | 21 | This |
| 2 | 22 | column |
| 1 | 23 | will |
| 3 | 24 | hold |
| 2 | 25 | other |
| 1 | 26 | values |
Since item 21,23 and 26 are already owned by user 1 this item should not be recommended to him.
This behaviour is controlled by the exclude_known parameter of the recommender.recommend method (doc).
exclude_known : bool, optional
By default, all user-item interactions previously seen in the training
data, or in any new data provided using new_observation_data.., are
excluded from the recommendations. Passing in exclude_known = False
overrides this behavior.
Example
>>> import graphlab as gl
>>> sf = gl.SFrame({'user_id':[1,2,1,3,2,1], 'item_id':[21,22,23,24,25,26]})
>>> print sf
+---------+---------+
| item_id | user_id |
+---------+---------+
| 21 | 1 |
| 22 | 2 |
| 23 | 1 |
| 24 | 3 |
| 25 | 2 |
| 26 | 1 |
+---------+---------+
[6 rows x 2 columns]
>>> rec_model = gl.recommender.create(sf)
>>> # we recommend items not owned by user
>>> rec_wo_own_item = rec_model.recommend(sf['user_id'].unique())
>>> rec_wo_own_item.sort('user_id').print_rows(100)
+---------+---------+----------------+------+
| user_id | item_id | score | rank |
+---------+---------+----------------+------+
| 1 | 22 | 0.0 | 1 |
| 1 | 24 | 0.0 | 2 |
| 1 | 25 | 0.0 | 3 |
| 2 | 21 | 0.0 | 1 |
| 2 | 23 | 0.0 | 2 |
| 2 | 24 | 0.0 | 3 |
| 2 | 26 | 0.0 | 4 |
| 3 | 21 | 0.333333333333 | 1 |
| 3 | 23 | 0.333333333333 | 2 |
| 3 | 26 | 0.333333333333 | 3 |
| 3 | 22 | 0.166666666667 | 4 |
| 3 | 25 | 0.166666666667 | 5 |
+---------+---------+----------------+------+
[12 rows x 4 columns]
>>> # we recommend items owned by user
>>> rec_w_own_item = rec_model.recommend(sf['user_id'].unique(), exclude_known=False)
>>> rec_w_own_item.sort('user_id').print_rows(100)
+---------+---------+----------------+------+
| user_id | item_id | score | rank |
+---------+---------+----------------+------+
| 1 | 21 | 0.666666666667 | 1 |
| 1 | 23 | 0.666666666667 | 2 |
| 1 | 26 | 0.666666666667 | 3 |
| 1 | 22 | 0.0 | 4 |
| 1 | 24 | 0.0 | 5 |
| 1 | 25 | 0.0 | 6 |
| 2 | 26 | 0.0 | 6 |
| 2 | 24 | 0.0 | 5 |
| 2 | 23 | 0.0 | 4 |
| 2 | 21 | 0.0 | 3 |
| 2 | 25 | 0.5 | 2 |
| 2 | 22 | 0.5 | 1 |
| 3 | 24 | 0.0 | 6 |
| 3 | 25 | 0.166666666667 | 5 |
| 3 | 22 | 0.166666666667 | 4 |
| 3 | 26 | 0.333333333333 | 3 |
| 3 | 23 | 0.333333333333 | 2 |
| 3 | 21 | 0.333333333333 | 1 |
+---------+---------+----------------+------+
[18 rows x 4 columns]
>>> # we add recommended items not owned by user to the original SFrame
>>> rec = rec_wo_own_item.groupby('user_id', {'reco':gl.aggregate.CONCAT('item_id')})
>>> sf = sf.join(rec, 'user_id', 'left')
>>> print sf
+---------+---------+----------------------+
| item_id | user_id | reco |
+---------+---------+----------------------+
| 21 | 1 | [24, 25, 22] |
| 22 | 2 | [24, 26, 23, 21] |
| 23 | 1 | [24, 25, 22] |
| 24 | 3 | [21, 23, 26, 25, 22] |
| 25 | 2 | [24, 26, 23, 21] |
| 26 | 1 | [24, 25, 22] |
+---------+---------+----------------------+
[6 rows x 3 columns]