Sub query in where condition not working - N1QL - subquery

Below with sub query is not working. When I execute the sub query separately, I can see it returns results. But when this sub query is added to where condition, I get "Results" : []
Select T1.* FROM `masterdata` AS T1
WHERE T1.doc="Calendar"
AND T1.Id=(SELECT SL.DocId FROM `masterdata` AS T
UNNEST T.Sllist AS SL
WHERE T.doc="Cuslist"
AND GtId IN ["1234567"])
Sub query returning the below result
[
sls::76543_77
]

Subquery returns ARRAY of objects, The documents inside subquery are objects. Remove Object use RAW inside subquery (must be projected single element)
You have two options
Option 1: Assume subquery returns single document, if not then query might not give results because your value might be other elements of ARRAY.
Select T1.* FROM `masterdata` AS T1
WHERE T1.doc="Calendar"
AND T1.Id= ((SELECT RAW SL.DocId FROM `masterdata` AS T
UNNEST T.Sllist AS SL
WHERE T.doc="Cuslist"
AND GtId IN ["1234567"])[0])
Option 2: Use IN clause
Select T1.* FROM `masterdata` AS T1
WHERE T1.doc="Calendar"
AND T1.Id IN (SELECT RAW SL.DocId FROM `masterdata` AS T
UNNEST T.Sllist AS SL
WHERE T.doc="Cuslist"
AND GtId IN ["1234567"])
Also checkout Subquery handling and potential query rewrite https://blog.couchbase.com/in-list-handling-improvements-in-couchbase-server-6-5/

Related

Cosmos DB Left Join

All of the documentation for Cosmos DB and it looks like it only supports the JOINkeyword, which seems to be a sort of INNER JOIN.
I have the following query:
SELECT * FROM
(
SELECT
DISTINCT(c.id),
c.OtherCollection,
FROM c
JOIN s IN c.OtherCollection
)
AS c order by c.id
This works fine and returns the data of documents that have OtherCollection populated. But It obviously does not return any documents that do not have it populated.
The reason for the join is that sometimes I execute the following query (queries are built up from user input)
SELECT * FROM
(
SELECT
DISTINCT(c.id),
c.OtherCollection,
FROM c
JOIN s IN c.OtherCollection
WHERE s.PropertyName = 'SomeValue'
)
AS c order by c.id
The question is how can I have a sort of LEFT JOIN operator in this scenario?
CosmosDB JOIN operation is limited to the scope of a single document. What possible is you can join parent object with child objects under same document.
It is totally different from SQL Join query which supports across two/many tables.
You can simulate LEFT JOIN with the EXISTS sentence.
Eg:
SELECT VALUE c
FROM c
WHERE (
(c.OtherCollection = null) OR EXISTS (--Like a "Left Join"
SELECT null
FROM s IN c.OtherCollection
WHERE s.PropertyName = 'SomeValue'
)
)
--AND/OR Some other c Node conditions
order by c.id

Is it possible to chain subsequent queries's where clauses in Dapper based on the results of a previous query in the same connection?

Is it possible to use .QueryMultiple (or some other method) in Dapper, and use the results of each former query to be used in the where clause of the next query, without having to do each query individually, get the id, and then .Query again, get the id and so on.
For example,
string sqlString = #"select tableA_id from tableA where tableA_lastname = #lastname;
select tableB_id from tableB WHERE tableB_id = tableA_id";
db.QueryMultiple.(sqlString, new {lastname = "smith"});
Is something like this possible with Dapper or do I need a view or stored procedure to accomplish this? I can use multiple joins for one SQL statement, but in my real query there are 7 joins, and I didn't think I should return 7 objects.
Right now I'm just using object.
You can store every previous query in table parameter and then first perform select from the parameter and query for next, for example:
DECLARE #TableA AS Table(
tableA_id INT
-- ... all other columns you need..
)
INSERT #TableA
SELECT tableA_id
FROM tableA
WHERE tableA_lastname = #lastname
SELECT *
FROM #TableA
SELECT tableB_id
FROM tableB
JOIN tableA ON tableB_id = tableA_id

Maximo Conditional expression manager/Sql Query Builder

Can somebody help me in converting below mentioned query in to Maximo's where clause:
select distinct workorder.wonum from workorder inner join [assignment]
On workorder.wonum=[assignment].wonum
inner join amcrew
On amcrew.amcrew=[assignment].amcrew
inner join amcrewlabor
On amcrewlabor.amcrew=amcrew.amcrew
inner join labor
On amcrewlabor.laborcode=labor.laborcode
inner join person
on labor.laborcode=person.personid where amcrewlabor.laborcode='KELLYB'
KELLYB is PERSONID used here for just reference.
If you are using a custom search query in Maximo, you can try prepending your with in (your query)
For example, if you're in Maximo's work order tracking module, the application uses select * from workorder by default. Any time you add a search filter such as work order number (wonum), then the query appends to run a query as select * from workorder where wonum = '123' if 123 is the work order number you entered.
Your where clause might look something like this:
wonum in (
select distinct workorder.wonum
from workorder
join assignment on workorder.wonum=assignment.wonum
join amcrew on amcrew.amcrew=assignment.amcrew
join amcrewlabor on amcrewlabor.amcrew=amcrew.amcrew
join labor on amcrewlabor.laborcode=labor.laborcode
join person on labor.laborcode=person.personid
where amcrewlabor.laborcode='KELLYB'
)
The SQL that is generated in Microsoft Access will not necessarily work in Maximo without some modification.

n1ql multiple unnest in select statement with multiple where condition

Following is one of the sample document in couchbase.
{
"name":"abc",
"friends":["a","b","c"],
"bestfriends":["x","y","z"]
}
I want to display "name" based on certain condition on "friends" and "bestfriends".
n1ql Query
select s.name from userdetails s
unnest s.friends as f
unnest s.bestfriends as bf
where f="a" or bf="a"
The above query works fine, if both the array ( friends, bestfriends) are not empty array.
but, even if any one of the array is empty array (ie. "bestfriends":[]), the result is null. How to overcome this?
Use LEFT OUTER UNNEST in both cases.

How to sort by sum of field of a relation on sails.js?

I searched a lot about sorting elements by sum of votes (in another model), like I do in SQL here :
SELECT item.* FROM item
LEFT JOIN (
SELECT
vote.item,
SUM(vote.value) AS vote.rating
FROM vote
GROUP BY vote.item
) AS res ON item.id = vote.item
ORDER BY res.rating DESC
Is there a way to do it via waterline methods ?
I think you can't do the left join with simple waterline methods, but you can use the .query method to execute your raw SQL syntax.
Sails MySQL adapter makes sum('field') conflict with sort('field'). It will generate SQL query like:
SELECT SUM(table.field) AS field FROM table ORDER BY table.field;
But I want:
SELECT SUM(table.field) AS field FROM table ORDER BY field;
It same as:
SELECT SUM(table.field) AS f FROM table ORDER BY f;
My solution is using lodash.sortBy() to process results. https://lodash.com/docs/4.16.4#sortBy

Resources