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.
Related
I want to count number of occurrences of a word in a string for example ,
[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]
in above string i want to count no of occurrences of a word 'DELIVERED' here it is 2.
i want to get result 2. pls help me on this. i should have to use only sql query to achieve this.
thanks in advance.
If your table's name is tablea and the column's name is col:
SELECT
(LENGTH(col) - LENGTH(REPLACE(col, '"DELIVERED"', '')))
/
LENGTH('"DELIVERED"') as counter
from tablea
remove every occurrence of "DELIVERED" and subtract the length of the string from the original string and finally divide the result with the length of "DELIVERED"
Assuming your data is in a table something like:
CREATE TABLE example(json TEXT);
INSERT INTO example VALUES('[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]');
and your instance of sqlite has the JSON1 extension enabled:
SELECT count(*) AS "Number Delivered"
FROM example AS e
JOIN json_each(e.json) AS j
WHERE json_extract(j.value, '$.messageStatus') = 'DELIVERED';
gives you:
Number Delivered
----------------
2
This will return the total number of matching entries from all rows in the table as a single value. If you want one result per row instead, it's an easy change but the exact details depend on your table definition. Adding GROUP BY e.rowid to the end of the query will work in most cases, though.
In the long run it's probably a better idea to store each object in the array as a single row in a table, broken up into the appropriate columns.
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;
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;
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
Here's what I want to do:
Insert some data being passed into a procedure into a new table, but also include some data from another table too.
Example:
INSERT INTO my_new_table(name, age, whatever)
VALUES (names.name, ages.age, passed_in_data);
But that won't work.
I did check another question on here, but it was a bit much for my feeble brain.
Something like this should do it:
insert into my_new_table (
name,
age,
whatever
)
select
names.name,
ages.age,
passed_in_data
from
names inner join
ages on ....
where
....