SQLite3 Match on VIEW? - view

So I am attaching multiple databases, then creating a TEMP VIEW which combines all the virtualTables from the various databases as such.
theDatabase.execSQL("CREATE TEMP VIEW virtualView AS SELECT * FROM Virtual_Sites UNION SELECT * FROM db1.Virtual_Sites UNION SELECT * FROM db2.Virtual_Sites");
Is it possible to use the MATCH query on this VIEW?
theDatabase.rawQuery(SELECT * FROM virtualView WHERE all_text MATCH 21033, null)
I am currently getting this error.
sqlite returned: error code = 1, msg = statement aborts at 46: [SELECT * FROM virtualView WHERE all_text MATCH 21033] unable to use function MATCH in the requested context, db=xxx
exception: SQL logic error or missing database; query: SELECT * FROM virtualView WHERE all_text MATCH 21033
I have read that creating VIEW's in this way will not inherit the underlying tables indexes, is this why I am unable to do a MATCH? If so any work arounds?
Thanks

MATCH works only directly on virtual tables that implement this operator, not on views.
You will have to rewrite all your queries to use MATCH on the individual tables, and to combine those results with UNION ALL.
Alternatively, copy the data of all your tables into one single temporary table.

This is what I found. VIEWS are in-efficient when combining multiple databases/tables as they loose their underlining indexes once they contain more than 2 tables.
ATTACH
String newDbPath = DB_PATH + DB_NAME;
theDatabase.execSQL("ATTACH DATABASE '" + newDbPath + "' as db1;");
The in-efficient way
Create VIEW
theDatabase.execSQL("CREATE TEMP VIEW view1 AS SELECT * FROM Sites_hsf UNION SELECT * FROM db1.Sites_hsf UNION SELECT * FROM db2.Sites_hsf;");
Query VIEW
sqlStatement = "SELECT * FROM view1 s INNER JOIN view2 con ON s.site_uid = con.site_uid AND con.ap_active = 1 WHERE s.site_uid = 1114331";
Execution Time
16281 Milliseconds
The efficient way
sqlStatement = "SELECT * FROM Sites_hsf s INNER JOIN Connections_hsf con ON s.site_uid = con.site_uid AND con.ap_active = 1 WHERE s.site_uid = 1114331 " +
"UNION ALL " +
"SELECT * FROM db1.Sites_hsf s1 INNER JOIN db1.Connections_hsf con1 ON s1.site_uid = con1.site_uid AND con1.ap_active = 1 WHERE s1.site_uid = 1114331 " +
"UNION ALL " +
"SELECT * FROM db2.Sites_hsf s2 INNER JOIN db2.Connections_hsf con2 ON s2.site_uid = con2.site_uid AND con2.ap_active = 1 WHERE s2.site_uid = 1114331";
Execution Time
4 Milliseconds
MATCH using CL.'s answer of UNION ALL
sqlStatement = "SELECT * FROM Virtual_Sites_hsf WHERE all_text MATCH '40227' " +
"UNION ALL SELECT * FROM db1.Virtual_Sites_hsf WHERE all_text MATCH '40227'" +
"UNION ALL SELECT * FROM db2.Virtual_Sites_hsf WHERE all_text MATCH '40227'"
Hopefully this helps someone.

Related

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in on line 285

$get_list_pro = "select * from products where product_list='$list_id'";
$run_list_pro = mysqli_query($con, $get_list_pro);
$count_lists = mysqli_num_rows($run_list_pro);*
That error occurs when your select query is wrong. The item that you are selecting using select * from products where product_list='$list_id. Does not exists in table product.

SQL query multiple joins not working

I am using oracle database and trying to run the following query but it gives the error:
"ERROR at line 17: ORA-00904: "FRH"."NS": invalid identifier"
What is the problem with it?
Following is the query:
SELECT *
FROM
(SELECT *
FROM ROOMS R
WHERE R.Prix<'50') FRM
JOIN
(SELECT *
FROM
(SELECT *
FROM HOTELS H
WHERE H.CatH=2) FH
JOIN
(SELECT *
FROM RESORTS R
WHERE TypeS='montagne') FR
ON FH.NS=FR.NS) FRH
ON (FRH.NS=FRM.NS AND FRH.NH=FRM.NH);
Thanks in advance
You have way too many nested selects here. Your query can be simplified to:
SELECT *
FROM rooms rm
JOIN hotels ht ON ht.ns = rm.ns AND ht.nh = rm.nh
JOIN resorts rs ON rs.ns = ht.ns
WHERE rm.prix < 50
AND ht.cath = 2
AND ss.types = 'montagne';
I am not entirely sure which tables need to be joined using just the ns column and which need both the ns and nh column because you have obfuscated your query so much and did not show us the table definitions.
Alternatively you can move the restrictions on the joined tables into the join condition. This isn't necessary for the inner joins you are using, but could be needed if you ever want to change that to an outer join:
SELECT *
FROM rooms rm
JOIN hotels ht ON ht.ns = rm.ns AND ht.nh = rm.nh AND ht.cath = 2
JOIN resorts rs ON rs.ns = ht.ns AND rs.types = 'montagne'
WHERE rm.prix < 50;
You should also not compare numbers and strings. Assuming rooms.prix is a number column, the condition R.Prix<'50' is wrong. You need to compare the number to a number r.prix < 50

Calculation in query based on the condition

I have a package in oracle contains several procedures, in one of those procedures I need to calculate some fee based on specific condition like this:
if X_flag = 1
then
fee = (.5 * orders.total_count) + (.3 * orders.total_amount)
else
fee = (.7 * orders.total_count) + (.4 * orders.total_amount)
so, what is the better way to do that?
The procedure that I need to add this calculation on it :
procedure informatonRPT (
p_customerID in number,
p_orderID in number)
as
begin
select
cusromers.costomerId,
cusromers.costomerName,
cusromers.coustomerPhone,
orders.price
from cusromers
inner join orders
on cusromers.orderId = orders.orderID
when
p_customerID is null or p_customerID = cusromers.costomerId
and p_orderID is null or p_orderID = orders.orderID ;
end informatonRPT;
customers table columns:
customerID
custumerName
customerPhone
ordedID
order table columns:
orderID
price
total_Amount
total_count
Note that:
Fee column doesn't exist in the data base, I have to calculate it in query
Again Im not sure because you didnt provide enought information, but you need use a CASE expresion
SELECT C.costomerId,
C.costomerName,
C.coustomerPhone,
O.price,
CASE X_flag
WHEN 1 THEN (.5 * O.total_count) + (.3 * O.total_amount)
ELSE 7 * O.total_count) + (.4 * O.total_amount)
END as fee
FROM cusromers C
join orders O
on C.orderId = O.orderID
oracle CASE documentation

How to use SQL query to define table in dbtable?

In JDBC To Other Databases I found the following explanation of dbtable parameter:
The JDBC table that should be read. Note that anything that is valid in a FROM clause of a SQL query can be used. For example, instead of a full table you could also use a subquery in parentheses.
When I use the code:
CREATE TEMPORARY TABLE jdbcTable
USING org.apache.spark.sql.jdbc
OPTIONS (
url "jdbc:postgresql:dbserver",
dbtable "mytable"
)
everything works great, but the following:
dbtable "SELECT * FROM mytable"
leads to the error:
What is wrong?
Since dbtable is used as a source for the SELECT statement it has be in a form which would be valid for normal SQL query. If you want to use subquery you should pass a query in parentheses and provide an alias:
CREATE TEMPORARY TABLE jdbcTable
USING org.apache.spark.sql.jdbc
OPTIONS (
url "jdbc:postgresql:dbserver",
dbtable "(SELECT * FROM mytable) tmp"
);
It will be passed to the database as:
SELECT * FROM (SELECT * FROM mytable) tmp WHERE 1=0
Code In Scala
val checkQuery = "(SELECT * FROM " + inputTableName + " ORDER BY " + columnName + " DESC LIMIT 1) AS timetable"
val timeStampDf = spark.read.format("jdbc").option("url", url).option("dbtable", checkQuery).load()
Adding an alias is also necessary after the query in parenthesis.

simple join syntax not properly working

I'd like to show the number of records in the history table grouped by name of service
service(code,name)
history(id, code,....)
Please note that there is no relationship between the two table history and service, history stores the activity independently from the other tables
I have tested this sql query and it returns the expected result:
select s.name, count(*) from history c
join service s
on c.code=s.code
where c.state='INITIALE'
group by s.name
Actually, I'd like to write it in jpql, I did alike
Query query =entityManager.createQuery(" select s.name, count(*) from ServiceEntity s join"
+ " HistoryEntity c "
+ " where c.code=s.code and c.state='INITIALE'"
+ " group by c.name order by c.name"
);
I got this error : Path expected for join!....
Invalid path: 'c.code'....right-hand operand of a binary operator was null....unexpected end of subtree
Try this
Query query = entityManager.createQuery("select s.name, count(s) from ServiceEntity s, HistoryEntity c "
+ " where c.code = s.code and c.state = 'INITIALE'"
+ " group by s.name order by s.name"
);

Resources