SQLITE: INTERSECT / UNION Select Result & Normal Array - ios

I use the solution below at the moment, the result is what I expected now.
myIDArray = #[#1, #2, #3];
...
executeQuery:[NSString stringWithFormat:#""
"SELECT name"
" FROM TABLE_A"
" WHERE a_id IN ("
" SELECT b_id"
" FROM TABLE_B"
" )"
" AND a_id IN %#", myIDArray];
But I wonder is there any way to INTERSECT two id lists inner IN (?)? Like
executeQuery:[NSString stringWithFormat:#""
"SELECT name"
" FROM TABLE_A"
" WHERE a_id IN ("
" SELECT b_id"
" FROM TABLE_B"
" INTERSECT"
" %#"// how to put my id array here appropriately?
" )", myIDArray];
Of course, this does not work, will throw syntax error.
I've also tried to use -componentsJoinedByString: to convert the array to string, w/ or w/o () for the string. No luck.

INTERSECT would require a query, so you would need a query that returns all these values:
... IN (SELECT b_id FROM TableB
INTERSECT
SELECT id FROM (SELECT 1 AS id UNION ALL
SELECT 2 UNION ALL
...
SELECT 42));
This is more verbose and less efficient than using two INs.

Related

Query with date Pivot and date order

I have this simple table and query:
https://docs.google.com/spreadsheets/d/1MwdP08WWmkG13fGmKjB5hpL-D_uqgAu0lsXrO8WcE1M
the problem is, I cannot get the columns to be ordered by date. I've tried transpose+sort+transpose, but it doesn't work as it puts "2019-10-1" before "2019-9-1" (because it interprets date as text). Is there any way to get this sorted?
you can do:
=ARRAYFORMULA(QUERY(QUERY(A:D;
"select B,sum(C),sum(D)
where B is not null
group by B
pivot A");
"select Col1,"&TEXTJOIN(","; 1;
{"Col"&ROW(INDIRECT("A2:A"&COUNTUNIQUE(A2:A)+1))\
"Col"&ROW(INDIRECT("A"&2+COUNTUNIQUE(A2:A)&":A"&COUNTUNIQUE(A2:A)*2+1))})))
and if you want to remove that sum:
=ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY(QUERY(A:D;
"select B,sum(C),sum(D)
where B is not null
group by B
pivot A ");
"select Col1,"&TEXTJOIN(","; 1;
{"Col"&ROW(INDIRECT("A2:A"&COUNTUNIQUE(A2:A)+1))\
"Col"&ROW(INDIRECT("A"&2+COUNTUNIQUE(A2:A)&":A"&COUNTUNIQUE(A2:A)*2+1))})));
" sum "; " "))
where can I add a date filter where A >= date '2019-07-01'. I've tried adding it in the first block, but I keep getting the error: "Error Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: ColXX"
=ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY(QUERY(A:D;
"select B,sum(C),sum(D)
where B is not null
and A >= date '2019-7-1'
group by B
pivot A ");
"select Col1,"&TEXTJOIN(","; 1;
{"Col"&ROW(INDIRECT("A2:A"&COUNTUNIQUEIFS(A2:A; A2:A; ">="&DATE(2019;7;1))+1))\
"Col"&ROW(INDIRECT("A"&2 +COUNTUNIQUEIFS(A2:A; A2:A; ">="&DATE(2019;7;1))&":A"&
COUNTUNIQUEIFS(A2:A; A2:A; ">="&DATE(2019;7;1))*2+1))})));
" sum "; " "))

Creating relationships based on nested list

I'm building a graph based and some of the relationships are based on information in nested lists. The relevant nodes are (b:Bundle) and (o:Object); the bundles require certain objects with different quantities and qualities. The nested list that contains these requirements has the format [ [object1, quantity1, quality1], [object2, quantity2, quality2], ... ]
but in the .csv file that I'm using the field has the format
o1,qn1,ql1|o2,qn2,ql2|... The relationship I want to create is
(b)-[r:REQUIRES {quantity, quality}]->(o).
I've tried using various combinations of SPLIT, UNWIND, and FOREACH. A minimal example from my data set:
id: 1
requirements: 24,1,0|188,1,0|190,1,0|192,1,0
That is to say, (b:Bundle {id:1}) -[r:REQUIRES {quantity:1, quality:0}]-> (o:Object {id:24}) and so on.
LOAD CSV WITH HEADERS FROM 'file:///bundles.csv' AS line
WITH SPLIT( UNWIND SPLIT ( line.requirements, '|' ), ',') as reqList
MATCH ( o:Object { id:TOINTEGER(reqList[0]) } )
MATCH ( b:Bundle { id:TOINTEGER(line.id) } )
MERGE (b) -[r:REQUIRES]-> (o)
ON CREATE SET r.quantity = TOINTEGER(reqList[1]),
r.quality = TOINTEGER(reqList[2]);
The error this query gives is
Neo.ClientError.Statement.SyntaxError: Invalid input 'P': expected 't/T' (line 2, column 22 (offset: 78))
" WITH SPLIT( UNWIND SPLIT ( line.requirements, '|' ), ',') as reqList"
^
Assuming your CSV file actually looks like this:
id requirements
1 24,1,0|188,1,0|190,1,0|192,1,0
then this query should work:
LOAD CSV WITH HEADERS FROM 'file:///bundles.csv' AS line FIELDTERMINATOR ' '
WITH line.id AS id, SPLIT(line.requirements, '|' ) AS reqsList
UNWIND reqsList AS reqsString
WITH id, SPLIT(reqsString, ',') AS reqs
MATCH ( o:Object { id:TOINTEGER(reqs[0]) } )
MATCH ( b:Bundle { id:TOINTEGER(id) } )
MERGE (b) -[r:REQUIRES]-> (o)
ON CREATE SET r.quantity = TOINTEGER(reqs[1]),
r.quality = TOINTEGER(reqs[2]);

Write SQL query to Entity Framework

select *
from [InterViewerComment]
where commentID in (select max(commentID) as commentID
from [InterViewerComment]
where jobID = 45
group by qenID)
This query is correct in SQL, but I want to rewrite it in Entity Framework.
Basically, I want the latest comment for each qenID based on job ID.
Other way to do the same
var query = " select qendidateList.qenName,InterViewerComment.*, candidate_status.status, 0 as ExamMarks, 0 as Skills from [InterViewerComment] " +
" left outer join qendidateList on InterViewerComment.qenID = qendidateList.qenID " +
" left outer join candidate_status on InterViewerComment.candidate_status = candidate_status.Candidate_status " +
" where commentID in( select max(commentID) as commentID from[InterViewerComment] where jobID = 45 group by qenID)";
var CandidateComm_ = db.Database.SqlQuery<interViewerComment>(query).ToList();

FireDAC Query connect to DBGrid

I have FDQuery and DataSource and DBGrid. Now I write this code in Button
FDQuery2->Active = false;
FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
" FROM Checkup C "
" INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
" WHERE C.today = " + MaskEdit1->Text +
" ORDER BY C.check_id ");
FDQuery2->Active = true;
and i connect FDQuery to DataSource and tDataSource to DBGrid, but when I click the Button it doesn't show rows. and i am sure that SQL code is work, because when i write inside the SQL String the rows have been shown.
any ideas.
You're missing the ' around your value when concatenating the text. Change your WHERE clause:
FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
" FROM Checkup C "
" INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
" WHERE C.today = '" + MaskEdit1->Text + "'" +
" ORDER BY C.check_id ");
You really should learn to use parameterized queries instead, though. It allows the database driver to handle things like properly quoting text or formatting dates for you, and it also (importantly) prevents SQL injection.
FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
" FROM Checkup C "
" INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
" WHERE C.today = :today" +
" ORDER BY C.check_id ");
FDQuery2->ParamByName("today")->AsString = MaskEdit1.Text;
FDQuery2->Active = true;

Esper How To Join Tables

I m sure that this statement works in Esper:
/* query from table TableA when receive event PriceEvent */
ON PriceEvent p
SELECT a.SymbolA, p.price
FROM TableA a
WHERE a.Symbol = p.Symbol
But this statement throws error:
/* join table TableA with TableB */
ON PriceEvent p
SELECT a.SymbolA, p.price, b.SymbolB
FROM TableA a, TableB b
WHERE a.Key = b.Key and a.Symbol = p.Symbol
Error Message:
com.espertech.esper.client.EPStatementSyntaxException: Incorrect syntax near ',' expecting end-of-input but found a comma ','
Then I use JOIN but it still doesn't work:
/* join table TableA with TableB */
ON PriceEvent p
SELECT a.SymbolA, p.price, b.SymbolB
FROM TableA a inner join TableB b
ON a.Key = b.Key
WHERE a.Symbol = p.Symbol
Error Message:
Incorrect syntax near 'join' (a reserved keyword) expecting end-of-input but found 'join'
How to join two tables in Esper?
The on-select only allows only a single named window or table and not multiple. Joins are just "select * from A, B, C...." and you can look into using "unidirectional".

Resources