SQLITE3 selecting row matching multiple values from same column - sqlite

I use SQLITE3 for my DB,i have 3 table: Doc table (id,name,date),tag table (id,name,count) and the relation table (id_doc, id_tag) that connect doc and tag table (many to many relation). Now i'm stuck on a query: i want to select the id of document related to tag 1,2 and 3 but i can't figure out how to do this.
anyone can help me?

If I understand correctly, you can use aggregation and having. Something like this:
select id_doc
from relation r
where id_tag in (1, 2, 3)
group by id_doc
having count(*) = 3;

Related

if else statements for finding values from two different table

I want to match a number from two different table and if it's found in any table it will display that..
INPUT FROM VALUE("/mydirectory/test.csv").
REPEAT:
IMPORT c.
FOR EACH table1 NO-LOCK WHERE table1.ordernumber=c.
IF AVAILABLE table1 THEN
DISPLAY table1.ordernumber table1.orderdate.
ELSE IF NOT AVAILABLE table1 THEN
FIND FIRST table2 NO-LOCK WHERE table2.ordernumber=c.
IF AVAILABLE table2 THEN
DISPLAY table2.ordernumber table2.orderdate.
END.
END.
So far I have written this much but it's not giving correct results. Appreciate help on this regards. Thanks.
Using IF AVAILABLE table1 within the FOR EACH makes no sense, as the FOR EACH only iterates matching records in table1.
Try this:
INPUT FROM VALUE("/mydirectory/test.csv").
REPEAT:
IMPORT c.
FIND table1 WHERE table1.ordernumber=c NO-LOCK NO-ERROR.
IF AVAILABLE table1 THEN
DISPLAY table1.ordernumber table1.orderdate.
ELSE DO:
/*IF NOT AVAILABLE table1 THEN*/
FIND FIRST table2 WHERE table2.ordernumber=c NO-LOCK NO-ERROR.
IF AVAILABLE table2 THEN
DISPLAY table2.ordernumber table2.orderdate.
END.
END.
Without knowing your tables/indexes I can only guess if you need to do unique finds (current code) or add the FIRST option to your FIND statements.
If you want the display of table2.ordernumber and table2.orderdate in place of the fields from table1, you can change the second DISPLAY statement like this - using the place holder syntax:
DISPLAY table2.ordernumber # table1.ordernumber
table2.orderdate # table2.orderdate.

Search similar value in another table using IN (SQLite)

I'm using SQLite to deal with tons of data (like 100gb of data).
I need to seach the value of one column in other table in the fastest way possible.
For example, I need to find the following values of Table 1
[COD]
C62
K801
And then find them in Table 2:
[COD_2]
C60-C63
K80-K81
My desired result is something like:
[COD_1] [COD_2]
C62 C60-C63
K801 K80-K81
Since I have a lot of data, it is inefficient to do something like:
SELECT *
FROM TABLE_1, TABLE_2
WHERE COD_1 LIKE '%' || COD_2 || '%';
Instead, I was trying to do this:
SELECT *
FROM TABLE_1
WHERE COD_1 IN (SELECT COD_2 FROM TABLE_2);
Of course that this doesn't result because the codes are not exactly the sames. Is there a way to search for similar values of one column (something like the LIKE operator) in other table by using IN? Or other way that doesn't cross TABLE_1 and TABLE_2?
Thank you!!!
useful to me.
Based on the small data set shown, and my presumed answer to #Shawn's question (K801 is a typo and is meant to be K80 or K81) I assume the following problem description:
Find a row in COD_2 such that the value in COD_1 is between {value1}-{value2} in COD_2; the - being significant and dependable.
I cannot speak to speed, but I would approach it this way:
SELECT value1, value2
from COD_1,COD_2
where value1 between substr(value2,1,instr(value2,'-')-1) and substr(value2,instr(value2,'-')+1)
The thought being: split the value from COD-2 into a "start" and an "end" value.

SQLite: Two different Counts on different table

I am pretty sure you guys have a simple and fast solution to my problem but my SQL-Skills are limited and I can't figure it out by my self.
So what I have is something like:
Newsgroup: [Name(PK)]
Article: [Id(PK)] [newsgroupName (FK)] [date:Date] [read:Boolean]
What I want know is a query that gives me the Name of each Newsgroup along with the Count of unread articles, Count of all articles and the date of the most recent one...
Is this even possible to archieve in a single Select-Query?
Thank you guys in advance!
You can simply use the appropriate aggregation functions:
SELECT newsgroupName,
SUM(NOT read) AS countUnread,
COUNT(*) AS countAll,
MAX(date) AS mostRecentDate
FROM Article
GROUP BY newsgroupName;
I guess something like this:
SELECT name, COUNT(countUnread) AS countUnread, COUNT(countRead) AS countRead, MAX(mostRecentDateUnread) AS mostRecentDateUnread, MAX(mostRecentDateRead) AS mostRecentDateRead FROM (
SELECT newsgroupName AS name, COUNT(newsgroupName) AS countUnread, 0 AS countRead, MAX(date) AS mostRecentDateUnread, NULL AS mostRecentDateRead
FROM Article
WHERE read = 0
GROUP BY newsgroupName, read
UNION ALL
SELECT newsgroupName AS name, 0 AS countUnread, COUNT(newsgroupName) AS countRead, NULL AS mostRecentDateUnread, MAX(date) AS mostRecentDateRead
FROM Article
WHERE read = 1
GROUP BY newsgroupName, read
)
GROUP BY name
I haven't tried but in theory with some fix it could work.

Recursive SQLite CTE with JSON1 json_each

I have a SQLite table where one column contains a JSON array containing 0 or more values. Something like this:
id|values
0 |[1,2,3]
1 |[]
2 |[2,3,4]
3 |[2]
What I want to do is "unfold" this into a list of all distinct values contained within the arrays of that column.
To start, I am using the JSON1 extension's json_each function to extract a table of values from a row:
SELECT
value
FROM
json_each(
(
SELECT
values
FROM
my_table
WHERE
id == 2
)
)
Where I can vary the id (2, above) to select any row in the table.
Now, I am trying to wrap this in a recursive CTE so that I can apply it to each row across the entire table and union the results. As a first step I replicated (roughly) the results from above as follows:
WITH RECURSIVE result AS (
SELECT null
UNION ALL
SELECT
value
FROM
json_each(
(
SELECT
values
FROM
my_table
WHERE
id == 2
)
)
)
SELECT * FROM result;
As the next step I had originally planned to make id a variable and increment it (in a similar manner to the first example in the documentation, but haven't been able to get that to work.
I have gone through the other examples in the documentation, but they are somewhat more complex and I haven't been able to distill those down to see how they might apply to this problem.
Can someone provide a simple example of how to solve this (or a similar problem) with a recursive CTE?
Of course, my goal is to solve the problem with or without CTEs so Im also happy to hear if there is a better way...
You do not need a recursive CTE for this.
To call json_each for multiple source rows, use a join:
SELECT t1.id, t2.value
FROM my_table AS t1
JOIN json_each((SELECT "values" FROM my_table WHERE id = t1.id)) AS t2;

Using the SQLite LIKE clause

Kindly review this simple SQLite-fiddle .
If any word in the column ITEM of table TWO matches any word in the column NAME_2 of table ONE, there should be a result(s).
Tried many permutes, yet not sure how to form the query for achieving this. Should the GLOBclause be used instead of LIKE, or perhaps some other wildcard patterns?
Many thanks in advance!
As per my comment, I think you can make use of instr as well. For example:
SELECT * FROM Table1 T1, Table2 T2 where instr(T2.NAME_2,T1.ITEM);
The above should return rows where T2.NAME contains a string = T1.ITEM

Resources