Refining a query by Edge metadata - sql

I currently have two Vertex classes, VersionSet and Version, with one non-lightweight Edge class, VersionSetToVersion. The VersionSetToVersion edge class also has a property called status which can have the value 'latest'.
If I have some #rid of a VersionSet vertex (i.e. #14:1), how would I construct an orient-db style SQL query to retrieve only the Version vertex that has a VersionSetToVersion EDGE with a status of 'latest'?
Here's a query that will return all Versions related to the VersionSet with #rid #14:1 regardless of the status property
SELECT out('VersionSetToVersion') FROM #14:1
This returns two VersionSet objects: #15:1, and #15:2, but only the edge to #15:2 has the status of 'latest'.
How can I refine this query by the status property on the EDGE so only #15:2 is returned in the results?

Try this:
SELECT outE('VersionSetToVersion')[status = 'latest'].inV() FROM #14:1

select out_VersionSetToVersion[status = 'latest'] from 14:1

Related

How validate two select list in oracle apex

I'm using Oracle apex, and i have 2 select list components that get the elements from the same table. i want build a currency converter, and the list of currency are "divisas"
My problem is: i want validate that when on one select component, one value is selected, the other component doesn't contain that element from select list 1. And vice verse
Also when on select 1 is null on the select 2 must show all result from the table, and vice verse.
I start with this query, but i can't do it works
select *
from divisas
where EN_APP = 'S'
and (
case when :P12_DIVISA is not null then (cod_divi <> :P12_DIVISA) else EN_APP = 'S' end
)
;
can somebody help me?
There's the Cascading List of Values property for Select List items. However, as you want to handle both items at the same time, you'll enter circular reference & dead loop so it won't just work.
Here's an option which does what you wanted; see if it helps:
create two items, e.g. P59_CURR_1 and P59_CURR_2 (for two currencies)
their type is Select List item
Page action on selection = "Redirect and set value"
SQL query looks like this (for P59_CURR_1)
select cod_divi d, cod_divi r
from divisas
where cod_divi <> nvl(:P59_CURR_2, 'X') -- would be `:P59_CURR_1` for another item
order by cod_divi
don't set cascading list of values parent item!
That's it; run the page and see how it behaves. Looks OK to me on apex.oracle.com's Apex 20.1 version.

Core Data SUBQUERY with key-path collection causes SQL error

I'm experiencing a SQL error from a Core Data-generated query. Here's the data model:
I want to get all of the drivers for whose busses have active passengers. This is simply described by the following fetch request and predicate, which works:
NSFetchRequest(entityName: "Driver")
NSPredicate(format: "ANY bus.passengers.active == YES")
However, let's say I want to do a more complex query on the passenger, like they are both active and a balance above 100. To do such a query, I'd have to use a SUBQUERY, like so:
NSPredicate(format: "SUBQUERY(bus.passengers, $passenger, $passenger.active == YES
AND $passenger.balance > 100).#count > 0")
I would expect SUBQUERY to accept a keypathed collection as its first argument. It generates the following SQL query:
SELECT 0,
t0.z_pk,
t0.z_opt,
t0.zbus
FROM zdriver t0
WHERE (SELECT Count(t2.z_3passengers)
FROM zbus t1
JOIN z_1passengers t2
ON t1.z_pk = t2.z_1busses
WHERE ( t0.zbus = t1.z_pk
AND (( t2.zactive = ?
AND t2.zbalance > ? )) )) > ?
And the error: 'no such column: t2.ZACTIVE'. It looks like it's missing a JOIN on the ZPASSENGER table.
Is this a Core Data bug or is there a different way to write this predicate?
Update: Here's a sample project that reproduces this issue.
Try fetching the Bus entity, avoiding the key path in the first argument in the SUBQUERY.
As was pointed out, this can be viewed as a limitation or a bug.
Update:
Further research suggests that your subquery predicate should actually work. I would recommend to check your NSManagedObject subclasses and make sure passengers returns a collection and active is properly listed as an attribute.

SQL Join based on a pattern

I have two tables tmpModels and tmp_product. Need to find matching models based on the model in tmpModel.
For example, If tmpmodel.updatedmodel ='amb3771' and tmp_product.updatedmodel could be 'amb3771bian' or '123abc3771xv' then we have a match.
Meaning that the tmp_product.updatedmodel should contain a matching pattern from tmpmodel anywhere in the updatedmodel column.
I have actually created a stored procedure but here is a snippet checking the behaviour for one particular value.
declare #model1 nvarchar(255)
set #model1 = 'amb3771'
select g.updatedmodel as GFK,p.updatedmodel Product from dbo.tmpModels g
join dbo.tmp_product p
on g.updatedmodel= substring(p.updatedmodel,PATINDEX('%#model1%',p.updatedmodel),LEN(#model1))
select #model1
I am expecting only two rows to be shown in the result. I don't understand why a different value is being assigned to #model1 even though I am forcing it to 'amb3771' and strangely there is no result matching 'amb3771'!! Please help me understand why this logic doesn't work and give the desired result. Below is the result -
GFK Product
w679 w679
t754cw t754cw
wa5714 wa5714silber
fl2823 fl2823
dc7230 dc7230
wda100 wda100
wa74sd wa74sd
ev9547 ev9547
wa5714 wa5714weiss
wa7439 wa7439
wda210 wda210wpm
wda210 wda210wpm
w194 w194
wda110 wda110wcs
wa5714 wa5714silber
wda100 wda100
First of all as DLeh mentioned #model should not be in quotes.
I don't understand why a different value is being assigned to #model1
even though I am forcing it
No different value is assigned to #model
If you execute query below you can see PATINDEX is returning 0 and then substring is getting full value of p.UpdateModel from 0th index to its length
select g.updatedmodel as GFK,p.updatedmodel Product, PATINDEX(#model1,p.updatedmodel)
from dbo.tmpModels g
join dbo.tmp_product p
on g.updatedmodel= substring(p.updatedmodel,PATINDEX(#model1,p.updatedmodel),LEN(#model1))
One solution I can suggest is to use WHERE condition instead of JOIN
set #model1 = '%wa5714%'
select DISTINCT g.updatedmodel as GFK,p.updatedmodel Product
from dbo.tmpModels g
, dbo.tmp_product p
WHERE PATINDEX(#model1,p.updatedmodel) > 0
AND PATINDEX(#model1,g.updatedmodel) > 0
I am assuming your model should display values of tmpModel and tmp_Product.
If you have different requirement the provide more details/example

Problem in HQL when try to get count of a property (that is entity) and its value

In my project, I have two entity, first PaperEntity contains several properties (consisting of value types and also reference types -reference to other entities-) and second is PaperStatusEntity.
PaperEntity has a property named Result of type PaperStatusEntity (and also a property named locked of type bool)
Imagine you have near 500 papers and just 8 paper status defined in database.
I want to find how much every status is used? for example status1 used 58 times and status2 used 130 times and so on.
I write below HQL
select paper.Result, Count(paper.Result) from PaperEntity paper group by paper.Result
this hql generates below error:
Column 'Conference_PaperStatusesTable.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
generated sql is:
select paperentit0_.Result as col_0_0_, count(paperentit0_.Result) as col_1_0_, paperstatu1_.Id as Id48_, paperstatu1_.Version as Version48_, paperstatu1_.CreationTime as Creation3_48_, paperstatu1_.Portal as Portal48_, paperstatu1_.TitleCodeName as TitleCod5_48_, paperstatu1_.Enabled as Enabled48_, paperstatu1_.RefereeChoice as RefereeC7_48_, paperstatu1_.OrderIndex as OrderIndex48_, paperstatu1_.ContactMessageTemplate as ContactM9_48_ from Conference_PapersTable paperentit0_ inner join Conference_PaperStatusesTable paperstatu1_ on paperentit0_.Result=paperstatu1_.Id, Conference_PaperStatusesTable paperstatu2_ where paperentit0_.Result=paperstatu2_.Id group by paperentit0_.Result
If I try to group data with a value type property like 'Locked' (that is bool), no problems and all things are ok
also If I use Criteria instead of HQL, works truly:
IList result = NHibernateSessionManager.Instance.CurrentSession.CreateCriteria(typeof(PaperEntity))
.SetProjection(Projections.ProjectionList().Add(Projections.RowCount()).Add(Projections.GroupProperty("Result"))).List();
foreach (var item in result) {
object[] value = item as object[];
yield return new Pair<PaperStatusEntity, int>(value[1] as PaperStatusEntity, (int)value[0]);
}
HQL group by is not smart enough to project all the properties of an entity when grouping by it. You have to either:
Specify all the properties
Select just those you need (id, description, whatever)
Use a subquery to get the count
The reason why it's working with Criteria is that it selects just the id when you use Projections.GroupProperty, creating uninitialized proxies.
This can create a SELECT N+1 problem.
You need to provide a simple data type for the database engine to group by. If paper.Result is another entity you'll probably need to join to the PaperStatusEntity table.
Something like:
select paper.Result, Count(paper.Result)
from PaperEntity as paper
join paper.Result as status
group by status.Id
If PaperStatus is mapped as a component you can also access Result.Id directly.
You can try:
SELECT paper.Result, Count(paper.Result)
FROM PaperEntity paper
JOIN paper.Result as status
GROUP BY paper.Result
Note that you would rather use a "FROM PaperStatusEntity" and join the papers
Because with the actual thing you are trying to do, you may not get a count for a PaperStatusEntity if no paper use this status (this means you don't get a count=0, you just get no count)
You can also use the #Formula annotation so that, for a given status, you can do paperStatusEntity.getPaperNumber()
Just add something like
#Formula("select count() from PaperEntity paper where paper.result_id = id");
public int getPaperNumber();
(should be adapted)
(I use Hibernate but it should be the same?)

NHibernate Return Values

I am currently working on a project using NHiberate as the DAL with .NET 2.0 and NHibernate 2.2.
Today I came to a point where I had to join a bunch of entities/collections to get what I want. That is fine.
What got me was that I do not want the query to return a list of objects of a certain entity type but rather the result would include various properties from different entities.
The following query is not what I am doing but it is kind of query that I am talking about here.
select order.id, sum(price.amount), count(item)
from Order as order
join order.lineItems as item
join item.product as product,
Catalog as catalog
join catalog.prices as price
where order.paid = false
and order.customer = :customer
and price.product = product
and catalog.effectiveDate < sysdate
and catalog.effectiveDate >= all (
select cat.effectiveDate
from Catalog as cat
where cat.effectiveDate < sysdate
)
group by order
having sum(price.amount) > :minAmount
order by sum(price.amount) desc
My question is, in this case what type result is supposed to be returned? It is certainly not of type Order, neither is of type LineItems.
Thanks for your help!
John
you can always use List of object[] for returning data and it will work fine.
This is called a projection, and it happens any time you specify an explicit select clause that contains rows from various tables (or even aggregate / summary data from a single table).
Using LINQ you can create anonymous objects to store these rows of data, like this:
var crunchies = (from foo in bar
where foo.baz == quux
select new { foo.corge, foo.grault }).ToList();
Then you can do crunchies[0].corge for example to pull out the rows & columns.
If you are using NHibernate.Linq this will "just work".
If you're using HQL or Criteria API, then what Fahad mentioned will work. You'll get a List<object[]> as a result, and the index of the array references the order of the columns that you returned in your select clause.