How to get exact node based on some property - cypher

I am creating some node and relationship as below, please ignore variables name convention:-
create e1={name:'sayeed', eId:1},
p1={pId:101, is_flag1:'Y', is_flag2:'N', is_flag3:'Y'},
p2={pId:102, is_flag1:'N', is_flag2:'N', is_flag3:'N'},
p3={pId:103, is_flag1:'N', is_flag2:'Y', is_flag3:'N'},
p1<-[:HAS_REL]-e1, p2<-[:HAS_REL]-e1, p3<-[:HAS_REL]-e1;
and searching node from this query:
start e = node:node_auto_index('name:sayeed')
match e-[:HOLD]-p
where p.is_flag2='N'
return distinct e;
The above query gets the entity "e" but in my case it should not get. Below is the detailed description.
I want to get entity which has ":HAS_REL" relationship and all "is_flag2" value should be "N"
if any one of "is_flag2" is "Y" I should not get the entity.Please Let me know
how can I write the cypher query.

You should use this query:
START e = node:node_auto_index('name:sayeed')
MATCH e-[:HAS_REL]-p
WHERE p.is_flag2='N'
RETURN DISTINCT e;
You were using the incorrect relationship lookup in your match clause. I hope this helps.

Related

request where there are at least 2 records in other table

I have this doc xml(short version) :
<Artiste>
<artiste a_id="A62" a_p_id="UK" a_date_nais="07/06/1952" a_sexe="M">
<a_prenom>Liam</a_prenom>
<a_nom>Neeson</a_nom>
</artiste>
<artiste a_id="A66" a_p_id="UK" a_date_nais="08/09/1971" a_sexe="M">
<a_prenom>Martin</a_prenom>
<a_nom>Freeman</a_nom>
</artiste>
<Film>
<film f_id="F1" f_p_id="FR" f_r_id="A61">
<f_genre>P</f_genre>
<f_titre>Banlieue 13</f_titre>
<f_date_sortie>10/11/2004</f_date_sortie>
<f_resume>fiction française</f_resume>
<f_role ro_nom="Leïto" ro_a_id="A63"/>
<f_role ro_nom="Lola" ro_a_id="A64"/>
</film>
<film f_id="F2" f_p_id="NZ" f_r_id="A59">
<f_genre>A</f_genre>
<f_titre>Les seigneurs des anneaux</f_titre>
<f_date_sortie>19/12/2005</f_date_sortie>
<f_resume>fiction américaine</f_resume>
<f_role ro_nom="Pêcheur" ro_a_id="A25"/>
<f_role ro_nom="Sirène" ro_a_id="A2"/>
</film>
</Film>
An artist play in a film(movie), an artist has a 'a_id" field in Artiste which is the same then in ro_a_id in Film
I want to select the name and first name (a_prenom, a_nom) of every artists that have played in at least 2 movies (film)
This is what I've done :
for $artiste in doc('S:/path/file.xml')//Artiste/artiste
(: retrieve film $artiste is working in :)
let $film := ('S:/path/file.xml')//Film/film[#ro_a_id=$artiste/#a_id]
where count(#ro_a_id)>=2
order by $artiste/#a_id
return $x/a_nom, $x/a_prenom
So I don't know how to join and make the request, and I also don't know how to return 2 fields (I know that $x/a_nom, $x/a_prenom line generates an error)
You're very close, but your query has a couple of problems:
The return clause references an undefined variable $x. This should be changed to $artiste
Once that is fixed, you can return each actor's two name elements by constructing a sequence—by wrapping the items in parentheses: ($artiste/a_nom, $artiste/a_prenom). Alternatively you could return a single item, e.g., a string created by concatenating the two name parts, with concat($artiste/a_nom, " ", $artiste/a_prenom).
Your where clause should reference the $film variable—specifically, $film/f_role/#ro_a_id.
Your sample data here doesn't contain any artists who appear in more than two of the films listed. So the where clause, even if fixed, will result in 0 hits.
I've posted a revised query to http://xqueryfiddle.liberty-development.net/nbUY4kp/1 showing these suggested changes. You'll see that I commented out the where clause so that we get some results.
The easiest way to make your query work is as follows:
for $artiste in doc('S:/path/file.xml')//Artiste/artiste
(: retrieve film $artiste is working in :)
let $film := doc('S:/path/file.xml')//Film/film[f_role/#ro_a_id=$artiste/#a_id]
where count($film)>=2
order by $artiste/#a_id
return ($artiste/a_nom, $artiste/a_prenom)
Here are the things I changed:
The expression ('S:/path/file.xml') in line 3 should probably be doc('S:/path/file.xml').
#ro_a_id is an attribute of f_role, not film.
You have to count the film elements, #ro_a_id is not in scope on line 4.
$x is never declared, you probably mean $artiste.
The final problem in the last row is that the FLWOR expression ends after the comma, so $artiste/a_prenom is not part of it. You can solve that by surrounding both parts with parentheses.

Hibernate return empty result if using LIKE condition with univarchar columns (Sybase)

Let's suppose I have a table: Person(Name: univarchar) and this table contains a row "abc".
I search Person by using Hibernate (Criteria API and HQL):
Criteria API:
Criteria c = session.createCriteria(Person.class);
c.add(Restrictions.ilike(name,"abc"));
return c.list();
HQL:
String query = "from Person where lower(name) like :name";
Query q = session.createQuery(query);
query.setString("name","abc");
return query.list();
It return empty result. However, when I use Interactive SQL of Sybase to execute SQL statement that is generated by Hibernate, it return a row "abc".
I found a solution for HQL case. This is to use rtrim function:
String query = "from Person where lower(rtrim(name)) like :name";
...
But my problem is I want to use Criteria API and I cannot find any ways to trim name column by using Criteria API.
Thanks and sorry for my poor English.
Have you tried with this code :
Criteria c = session.createCriteria(Person.class);
c.add(Restrictions.ilike(name,"abc",MatchMode.ANYWHERE));
return c.list();
If this will not work then Read this. Its similar Just he wants to trim while Order you want to trim while Restrictions.

What to do with the error "Ambiguous Columns Defined"?

This is my sql command:
select INCOME_TYPE_ID,
REGION_CODE,
FIN_YEAR_CODE,
PORTION_AMOUNT
from INCOME.INCOME_TYPE,
COMMON.REGION,
INCOME.RECEIVE_DOC_PORTION,
INCOME.ASSESS_ORDER_ITEM,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC,
ACCOUNTING.VOUCHER_ITEM,
ACCOUNTING.VOUCHER,
ACCOUNTING.FIN_YEAR
where INCOME.RECEIVE_DOC_PORTION.ASSESS_ORDER_ITEM_ID = INCOME.ASSESS_ORDER_ITEM.ASSESS_ORDER_ITEM_ID
and INCOME.ASSESS_ORDER_ITEM.INCOME_TYPE_ID=INCOME.INCOME_TYPE.INCOME_TYPE_ID
and INCOME.RECEIVE_DOC_PORTION.RECEIVE_DOC_PORTION_ID = ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.RECEIVE_DOC_PORTION_ID
and ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.VOUCHER_ITEM_ID = ACCOUNTING.VOUCHER_ITEM.VOUCHER_ITEM_ID
and ACCOUNTING.VOUCHER_ITEM.VOUCHER_ID = ACCOUNTING.VOUCHER.VOUCHER_ID
and ACCOUNTING.VOUCHER.REGION_CODE = COMMON.REGION.REGION_CODE
and ACCOUNTING.VOUCHER.FIN_YEAR_CODE = ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE
and I got this error:
Ambiguous Columns Defined
I'm Using SQL Developer as Oracle client.
Apparently one (or more) column names in your select list exists in more than one table of the FROM list.
You need to prefix every column in the SELECT list with the table it's coming from (it's also a good practice to always do that, regardless of the fact if they are ambigous)
Mention name of table before every column in select query.
Ambiguous column means that you have more than one column with the same name in one of the SELECT statements.
Try this instead, prefgixing all selected columns with their fully qualified names (as you have done elsewhere in your SELECT):
select INCOME.INCOME_TYPE.INCOME_TYPE_ID,
COMMON.REGION.REGION_CODE,
ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.PORTION_AMOUNT
from INCOME.INCOME_TYPE,
COMMON.REGION,
INCOME.RECEIVE_DOC_PORTION,
INCOME.ASSESS_ORDER_ITEM,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC,
ACCOUNTING.VOUCHER_ITEM,
ACCOUNTING.VOUCHER,
ACCOUNTING.FIN_YEAR
where INCOME.RECEIVE_DOC_PORTION.ASSESS_ORDER_ITEM_ID = INCOME.ASSESS_ORDER_ITEM.ASSESS_ORDER_ITEM_ID
and INCOME.ASSESS_ORDER_ITEM.INCOME_TYPE_ID = INCOME.INCOME_TYPE.INCOME_TYPE_ID
and INCOME.RECEIVE_DOC_PORTION.RECEIVE_DOC_PORTION_ID = ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.RECEIVE_DOC_PORTION_ID
and ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.VOUCHER_ITEM_ID = ACCOUNTING.VOUCHER_ITEM.VOUCHER_ITEM_ID
and ACCOUNTING.VOUCHER_ITEM.VOUCHER_ID = ACCOUNTING.VOUCHER.VOUCHER_ID
and ACCOUNTING.VOUCHER.REGION_CODE = COMMON.REGION.REGION_CODE
and ACCOUNTING.VOUCHER.FIN_YEAR_CODE = ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE
I had to guess the filly qualified name for
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.PORTION_AMOUNT
It might be
INCOME.RECEIVE_DOC_PORTION.PORTION_AMOUNT
But you should be able to resolve that easily.
Hope it helps...

checking if all the items in list occur in another list using linq

I am stuck with a problem here. I am trying to compare items in a list to another list with much more items using linq.
For example:
list 1: 10,15,20
list 2: 10,13,14,15,20,30,45,54,67,87
I should get TRUE if all the items in list 1 occur in list 2. So the example above should return TRUE
Like you can see I can't use sequenceEquals
Any ideas?
EDIT:
list2 is actually not a list it is a column in sql thas has following values:
<id>673</id><id>698</id><id>735</id><id>1118</id><id>1120</id><id>25353</id>.
in linq I did the following queries thanks to Jon Skeets help:
var query = from e in db
where e.taxonomy_parent_id == 722
select e.taxonomy_item_id;
query is IQueryable of longs at this moment
var query2 = from e in db
where query.Contains(e.taxonomy_item_id)
where !lsTaxIDstring.Except(e.taxonomy_ids.Replace("<id>", "")
.Replace("</id>", "")
.Split(',').ToList())
.Any()
select e.taxonomy_item_id;
But now I am getting the error Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.
How about:
if (!list1.Except(list2).Any())
That's about the simplest approach I can think of. You could explicitly create sets etc if you want:
HashSet<int> set2 = new HashSet<int>(list2);
if (!list1.Any(x => set2.Contains(x)))
but I'd expect that to pretty much be the implementation of Except anyway.
This should be what you want:
!list1.Except(list2).Any()
var result = list1.All(i => list2.Any(i2 => i2 == i));

(drupal)a difficulty code to understand,get the same article's title under the same term

if ($node->taxonomy) {
$query = 'SELECT DISTINCT(t.nid), n.nid, n.title FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid WHERE n.nid != %d AND (';
$args = array($node->nid);
$tids = array();
foreach ($node->taxonomy as $term) {
$tids[] = 't.tid = %d';
$args[] = $term->tid;
}
$query .= implode(' OR ', $tids) . ')';
$result = db_query_range($query, $args, 0, 10);
while ($o = db_fetch_object($result)) {
echo l($o->title, 'node/' . $o->nid);
}
}
the code is from a drupal guru. . used to get the article's title under the same term in node.tpl.php, i have researched it two days, although know some part of it. the principle of the code i still don't know. expect someone can explain more details about it for me .many thanks.
Short version:
It gets the array of tags of the node, retrieves the first 10 nodes that use at least one of these tags and outputs a link for each of these 10 results.
Detailed version:
First of all, the variable "$node" is an object that contains the data about a specific node (e.g. a Page or Story node).
For example, "$node->title" would be the title of that node.
"$node->taxonomy" tests is that node is tagged (because if it has no tags, it cannot retrieve the other nodes using the same tag(s).
When there is one or several tags associated with that node/page/story, $node->taxonomy is an array .
Now about the SQL query:
"node" is the database table that stores the base fields (non-CCK) of every node.
"term_node" is the database table that contains the combination of tag (which is called a "taxonomy term") and node.
In both tables, "nid" is the "unique Node ID" (which is an internal autoincremented number). Because this column is in both tables, this is how the tables are joined together.
In "term_node", "tid" is the "unique Term ID" (which is also an internal autoincremented number).
The "node" table is aliased "n", therefore "n.nid" means "the Node ID stored in table node".
The "term_node" table is aliased "t", therefore "t.tid" means "the Term ID stored in table term_node".
The "foreach" loop goes thru the array of tags to extract the TermID of each tag used by the node in order to add it in the SQL query, and implode converts to a string.
The loop stores a piece of SQL query for each tag in variable $tids and stores the actual value in variable $args because Drupal database calls are safer when the arguments are passed separately from the SQL query: "%d" means "integer number".
"db_query_range" is a function that selects multiple rows in the database: here, "0 10" means "retrieve the first 10 results".
"db_fetch_object" in the "while" loop retrieves each result and stores it in the variable "$o", which is an object.
Therefore "$o->title" contains the value of the column "title" retrieved by the SQL query.
The function "l" is the drupal functin that creates the code for an HTML link: the first argument is the name of the link, the second argument is the drupal path: in Drupal, any node can be accessed by default using "www.yoursite.com/node/NodeID",
which is why it gives the path "node/123" (where 123 is the "Node ID").
This function is useful because it transparently handles custom paths, so if your node has a custom path to access it using "www.yoursite.com/my-great-page" instead, it will create a link to that page instead of "www.yoursite.com/node/123" automatically.
I wouldn't exactly call the guy who wrote this a guru, you could do this a lot prettier. Anyways what he does it create a query that looks like this:
SELECT DISTINCT(t.nid), n.nid, n.title FROM {node} n
INNER JOIN {term_node} t ON n.nid = t.nid
WHERE n.nid != %d
AND (t.tid = %d OR t.tid = %d OR ... t.tid = %d);
The end result is that he selects all the node ids and titles (only once) that share at least one term with the selected node, but isn't the node itself.