How to check the duplicate row with multiple column in infopath repeating table - infopath

I want to check the duplicate row with three columns(ProductID,ProductName,ProductCategory). So user should not enter duplicate row with the same ProductID,ProductName and ProductCategory(with these 3 combination). ex1:
ProductID ProductName ProductCategory
1 Aciloc150 Medicine
User should be restricted to enter the above same ProductID,ProductName and ProductCategory(1 Aciloc150 Medicine)
I know the below code using this I can check the duplicate value in one column, How can I check the duplicate row with the above combination(ProductID ProductName ProductCategory)?
count(/my:myFields/my:group1/my:group2[my:field2 != ""]) > 1 and (. = ../preceding-sibling::my:group2/my:field2 or . = ../following-sibling::my:group2/my:field2)
Here /my:myFields/my:group1/my:group2 -> repeating group xpath
My:field2 -> field in repeating table

Related

How to split these records into individual new ones?

I want to search if a string exists in column2 (site_id) then put that string in a new table with the value of its before column in that row. The table has only 2 columns and the site_id column may have many 5-word strings that I want.
I want to get all of the the specific site id's. For example: E7089 or E7459 (I need all of them and the first word is random like E or T or etc and the four digits are variable).
The first row is with one ticket_id and many site_ids. I only need site ids like:g1231 or g1236 and not the addresses in parentheses:
ticket_id
site_id
sss-bb-12312312-12312
g1231(afsdgf-sdgsdgdg), g1236(sdfsdgsdg), g3212(asdfas-dfsd), b2311(asdasd), b3213(asdfsdf)
And make it like this:
ticket_id
site_id
sss-bb-12312312-12312
g1231
sss-bb-12312312-12312
g3211
sss-bb-12312312-12312
g1236
sss-bb-12312312-12312
b2311
sss-bb-12312312-12312
b3213
I can find the 5-word site id's with regexp [A-Z]\d{1,4}, but I can't extract and insert them into a new row. My code :
DROP TABLE IF EXISTS test2;
CREATE TABLE if NOT EXISTS test2
(
Ticket_id varchar,
site_id varchar
);
INSERT INTO test2
SELECT ticket_id, site_id
FROM TEST
WHERE site_id regexp '[A-Z]\d{1,4}';
This will find the site_id's and insert rows that match. I don't want that. How to convert the first one to the second?
Current db :
column1
column2
ticket1
many site ids
ticket2
many site ids
I want it to be :
column1
column2
ticket1
id
ticket1
id
ticket1
id
ticket1
id
ticket2
id
ticket2
id
ticket2
id
The tickets do not need any change except getting copied into new rows with their assigned site_id.
There are multiple site_ids for each ticket that need to be separated to new rows.
It needs to be done in SQLite db browser (unfortunately no Python).
You need a recursive CTE to split the site_id column of the table test1 and SUBSTR() function to take the first 5 chars to insert in the table test2:
WITH cte AS (
SELECT ticket_id, '' site_id, site_id || ',' s
FROM test1
UNION ALL
SELECT ticket_id,
SUBSTR(s, 0, INSTR(s, ',')),
SUBSTR(s, INSTR(s, ',') + 1)
FROM cte
WHERE s <> ''
)
INSERT INTO test2 (ticket_id, site_id)
SELECT ticket_id, SUBSTR(TRIM(site_id), 1, 5)
FROM cte
WHERE site_id <> '';
See the demo.

Is it possible to update a table by adding values from other two tables in SQLite

I am trying out SQLite and encountered a problem. There are 3 Tables A, B, and C.
I want to update Table A using the sum of B and C.
Table A.
James null.
Table B.
James 5.
Table C
James 2
so with the update, I want table A to have
James 3. (5-2)
Thank You
SQLite does not support joins in an UPDATE statement so you can do it by accessing directly the corresponding rows of the tables A and B like this:
update A
set value =
(select value from B where name = A.name) -
(select value from C where name = A.name)
If you want to update only the row with name = 'James' then add:
where name = 'James'
See the demo
Works in every DB:
UPDATE
"A"
SET
"x" =
(
SELECT
SUM("x")
FROM "B"
WHERE "B"."id"="A"."id"
) +
(
SELECT
SUM("x")
FROM "C"
WHERE "C"."id"="A"."id"
)
I believe the following demonstrates that Yes you can:-
DROP TABLE IF EXISTS ta;
DROP TABLE IF EXISTS tb;
DROP TABLE IF EXISTS tc;
CREATE TABLE IF NOT EXISTS ta (name TEXT, numb INTEGER);
CREATE TABLE IF NOT EXISTS tb (name TEXT, numb INTEGER);
CREATE TABLE IF NOT EXISTS tc (name TEXT, numb INTEGER);
INSERT INTO ta VALUES ('JAMES',null),('Mary',100);
INSERT INTO tb VALUES ('JAMES',5),('Sue',33);
INSERT INTO tc VALUES ('JAMES',2),('Anne',45);
UPDATE ta SET numb =
(SELECT sum(numb) FROM tb WHERE name = 'JAMES')
-
(SELECT sum(numb) FROM tc WHERE name = 'JAMES')
WHERE name = 'JAMES';
SELECT * FROM ta;
SELECT * FROM tb;
SELECT * FROM tc;
This :-
Drops the tables if they exist allowing it to be rerun (simplifies modifications if need be).
column names name and numb have been assumed as they weren't given.
Creates the 3 tables (note table names used for the demo are ta, tb and tc)
Adds some data (note that additional rows have been added to show how to distinguish (at least to a fashion))
Updates column numb of table A (ta) where the name column has a value of JAMES according to the sum of the numb column from all rows with the same name (JAMES) from table tb minus the sum of the numb column from all rows with the same name (JAMES) from table tc
This may not be exactly what you want so it assumes that you want to sum all rows with the same name per table (ta and tc)
Queries all the tables (first is shown below as that is the table that has been updated.)
The first result showing that the row has been updated from null to 3 (5 - 2) and that the row for Mary has remained as it was :-
The following change to the UPDATE gets the name (rather than hard-coding 'JAMES' multiple times, as per the row(s) extract from the ta table, the use of hard-coded names perhaps making it easier to understand the working of the SQL).
UPDATE ta SET numb = (SELECT sum(numb) FROM tb WHERE name = ta.name) - (SELECT sum(numb) FROM tc WHERE name = ta.name) WHERE name = 'JAMES';
Note that should there not be an associated row (i.e. with the same name) in either tb or tc then the result will be null (whether or not sum is used).

SQlite multiple IDs linking one to another table

I have one table containing rows with information about dealers and unique IDs.
id name
1 dealer1
2 dealer2
The other table contains products which are sometimes available at multiple dealers.
name dealerids
product1 1, 2
product2 2
Now I would like to query all dealers a product is available at, but I don't know how. I tried something like:
SELECT * FROM dealers WHERE id IN (SELECT dealerids FROM products WHERE name = "product1")
which didn't work. I'm in C# and dealerids is based on a string and stored as TEXT in the database. I tried "'1', '2'" and "1, 2", both didn't work for me.
I'm quiet new to SQL so is there a way to achieve what I want using the TEXT datatype?
Well, that's strange way to store the values in a database - as a string separated by commas. But if it's really the case and you can not change this to multiple lines where each record corresponds to one dealer id , you probably can try this:
select * from dealers where exists (select 1 from products where name = "product1" and dealerids like
dealers.id || ',' || '%') or exists
(select 1 from products where name = "product1" and dealerids like '%' || ', ' || dealers.id || ',%')
or exists
(select 1 from products where name = "product1" and dealerids like '%, ' || dealeris.id)
The first clause is taking care of the case where desired delearid is the first one in the string, the second clause is for the case when it's in the middle, and the third one is for the ending id.
A normalized database would look something like this, in your scenario.
// table_dealer
id name
1 dealer1
2 dealer2
// table_product
id title
1 product1
2 product2
//table_associate
id product_id dealer_id
1 1 1
1 2 1
Now I would like to query all dealers a product is available at, but I don't know how. I tried something like
//searching by product_id
select dealer_id from table_associate where product_id = X
Use JOIN query to get data from these 3 tables.

Sqlite trigger to calculate an ID based on an entry type

I am trying to setup a trigger that will auto calculate an ID field based on the sum of a specific type of entry. I have it working where the ID number in the ID indexes based on the number of all entries
> BEGIN
> UPDATE master_workorders
> SET wo_no = master_workorders.wo_sub || substr('0000'||master_workorders.pkuid, -4,4)||'-'|| substr(master_workorders.rdate,3,2)
> WHERE rowid = NEW.rowid;
> END
This returns ID's like WO0001-17 BB0002-17 and M0003-17 each ID number (middle 4 digits) is an entry. I want my ID numbers to represent the number of each type (WO, BB, M these values are stored in the wo_sub column) as WO0001-17 BB0001-17 M0001-17 and if a new BB work order is added it would be BB0002-17 and so on for each type.
To replace the autoincremented ID with the current count, replace master_workorders.pkuid with a subquery:
... || (SELECT COUNT(*) FROM master_workorders WHERE wo_sub = NEW.wo_sub) || ...

sqlite returns 0 rows

SELECT skill_name, character_name, cb_id, cb_id2 FROM characterbasics, characterskills WHERE characterbasics.character_name = 'Joe' & characterbasics.cb_id = characterskills.cb_id2
This, for some reason, returns 0 rows
The character name is in there (as well as 2 other dummy names).. and both cbid and cbid2 are the same.
When i try the query without the & cbid=cbid2 i get the name with the other data.. now when i check for JUST cbid=cbid2 i get 3 different dummy characters i created...
im trying to pull all "skills" associated with one character by matching the id of the character name in table 1 with the character id in table 2
Where have I erred?
cn = character name
cn cbid cbid2
Joe 2 2
This is what it SHOULD look like..
You cant use & as logical AND operator (& is binary operator), so sql should look like :
SELECT skill_name, character_name, cb_id, cb_id2
FROM characterbasics, characterskills
WHERE characterbasics.character_name = 'Joe' AND characterbasics.cb_id = characterskills.cb_id2

Resources