SQLite3 getting output from one table as input to another - sqlite

Table1 has the following columns
problem
t1
t2
t1 and t2 are INT
problem is the result of the values from t1,t2
t1 can be 1,2,3,4,5
t2 can be 6,7,8,9,10
I could write an SQL query that says
SELECT * from Table1 WHERE (t1=1 OR t1=2 OR t1=3 OR t1=4 OR t1=5) AND (t2=6 OR t2=7 OR t2=8 OR t2=9 OR t2=10)
But instead of writing out the entire expression (shown above), I want to store it into another table (called Table2)
Table2 has the following columns
data_entered
data_results
data_entered is 1
data_results is a string with the following data "(t1=1 OR t1=2 OR t1=3 OR t1=4 OR t1=5)"
data_entered is 2
data_results is a string with the following data "(t2=6 OR t2=7 OR t2=8 OR t2=9 OR t2=10)"
so I want to create a QUERY where I could say
SELECT * from Table1 WHERE ... (and specify data_entered) the results should be the same as the above mentioned SQL.
Please help.
Thanks

For your first query, you can use IN instead:
select * from table1 where t1 in (1,2,3,4,5) and t2 in (6,7,8,9,10);
Then instead of storing sql code in table2, store each value in a seperate row.
create table data (entered, result);
insert into data values (1, 1);
insert into data values (1, 2);
...
insert into data values (2, 10);
Then you can do:
select *
from table1
where t1 in (select result from data where entered = 1)
and t2 in (select result from data where entered = 2);

Related

PL SQL nested subquery

I'm trying to get the result into a variable (is it possible to do it as a %rowtype of an existing table? probably not because of conflicting columns) where it would display me all the values where the two refs overlap and the i_ref(which is inputted ) also overlaps with the ref from t1.
select *
into aRow
from table1 t1
where t1.ref = i_ref
and (select * from table2 t2 where t1.ref = t2.ref);
What am I doing wrong with my select?
You can join tables instead of nested subquery:
select t1.*
into aRow
from table1 t1 join table2 t2 on t1.ref = t2.ref
where t1.ref = i_ref

how to select the same content of the field and to delete it in sqlite?

C:\Users\pengsir>sqlite3 e:\\test.db
sqlite> create table test (f1 TEXT,f2 TEXT, f3 TEXT);
sqlite> insert into test values("x1","y1","w1");
sqlite> insert into test values("x1","y1","w2");
sqlite> insert into test values("x1","y3","w2");
sqlite> insert into test values("x2","y3","w2");
sqlite> insert into test values("x3","y4","w4");
sqlite> insert into test values("x2","y3","w4");
sqlite> insert into test values("x1","y3","w2");
sqlite>
1.select the record rows which contain the same f1 and f2 ,and the rowid .
sqlite> select rowid,f1,f2 from test group by f1,f2 having(count(f2)>1 and count(f2)>1);
2|x1|y1
7|x1|y3
6|x2|y3
I want the result to be :
1|x1|y1
2|x1|y1
3|x1|y3
4|x2|y3
6|x2|y3
7|x1|y3
2.select the record rows which contain the same f1 f2 and f3,and the rowid .
sqlite> select rowid,f1,f2,f3 from test group by f1,f2,f3 having(count(f2)>1 and count(f3)>1);
7|x1|y3|w2
I want the result to be
3|x1|y3|w2
7|x1|y3|w2
let us discuss this problem further , i want to delete one |x1|y3|w2 and keep one |x1|y3|w2 in the table?here is my method.
DELETE FROM test
WHERE rowid in(
SELECT rowid FROM test
WHERE (SELECT count(*)
FROM test AS t2
WHERE t2.f1 = test.f1
AND t2.f2 = test.f2
AND t2.f3 = test.f3
) >= 2 limit 1);
Is there more simple and smart way to do that? (the method is wrong)
I find the proper way to do .
delete from test
where rowid not in
(
select max(rowid)
from test
group by
f1,f2,f3
);
and the method to more than one duplicate for a f1/f2 combination is :
delete from test
where rowid not in
(select rowid from test group by f1,f2);
It will be executed only one time.
You want records that have duplicates (in those fields), i.e., where the number of records with the same values in those fields is at least two:
SELECT rowid, f1, f2
FROM test
WHERE (SELECT count(*)
FROM test AS t2
WHERE t2.f1 = test.f1
AND t2.f2 = test.f2
) >= 2
This requires executing the subquery for each record.
Alternatively, compute the records with duplicates in a subquery once; this might be more efficient:
SELECT test.rowid, test.f1, test.f2
FROM test
JOIN (SELECT f1, f2
FROM test
GROUP BY f1, f2
HAVING count(*) >= 2
) USING (f1, f2)
If you want to remove one of the duplicates, this is easier to do because GROUP BY already returns exactly one output row for each group:
DELETE FROM test
WHERE rowid IN (SELECT max(rowid)
FROM test
GROUP BY f1, f2
HAVING COUNT(*) >= 2)
(If there is more than one duplicate for a f1/f2 combination, you have to execute this multiple times.)
Try using SELECT ALL instead of SELECT but according to the linked docs SELECT should behave like SELECT ALL by default and not like SELECT DISTINCT so I don't know where the problem may be.

select rowid of certain row when using group by in sqlite

I have a query in SQLite where I group by a certain column and use an aggregate function MAX on another column in the select statement. Now I also want the rowid of the row which holds the value that is displayed by the MAX aggregate. I know that this must be a unique row because of the primary key constraint. I can't figure out how to write the query. See the following example:
create table t1 (c1, c2, constraint t1_pk primary key (c1, c2));
insert into t1 values ('boys', 1);
insert into t1 values ('boys', 2);
insert into t1 values ('girls', 1);
insert into t1 values ('girls', 2);
Now I have the table with the primary constraint over both columns. A SELECT query for the table gives the following output:
sqlite> select rowid, * from t1;
rowid|c1|c2
1|boys|1
2|boys|2
3|girls|1
4|girls|2
Now I want to group by c1 and select the MAX of c2. Then I want the rowid of the row which holds the values displayed now. See the following queries:
sqlite> select rowid, c1, max(c2) from t1 group by c1;
rowid|c1|max(c2)
2|boys|2
4|girls|2
sqlite> select rowid, c1, min(c2) from t1 group by c1;
rowid|c1|min(c2)
2|boys|1
4|girls|1
The second query with the MIN aggregate should return the rowids of the rows holding the MIN values, this is what I want to achieve:
rowid|c1|min(c2)
1|boys|1
3|girls|1
Now I've tried the following subselect, which doesn't work either because it gives an error:
sqlite> select (select rowid from t1 b where b.c1 = a.c1 and b.c2 = max(a.c2)), a.c1, max(a.c2) from t1 a group by a.c1;
Error: misuse of aggregate function max()
sqlite> select (select rowid from t1 b where b.c1 = a.c1 and b.c2 = min(a.c2)), a.c1, min(a.c2) from t1 a group by a.c1;
Error: misuse of aggregate function min()
The last thing I've tried is a subquery in the FROM clause, which also doesn't work:
sqlite> select
...> (select rowid from t1 b where b.c1 = c.c1 and b.c2 = c.c2),
...> c1,
...> c2
...> from
...> (select a.c1, max(a.c2) as c2 from t1 a group by a.c1) c;
Error: misuse of aggregate: max()
sqlite> select
...> (select rowid from t1 b where b.c1 = c.c1 and b.c2 = max(c.c2)),
...> c.c1,
...> max(c.c2)
...> from
...> (select a.c1, a.c2 from t1 a group by a.c1) c;
Error: misuse of aggregate function max()
Is there any solution for my problem? I really don't know what else I could try.
If I understood your question correctly, try like this:
select rowid, c1, min(c2) from t1 a
where c2=(select min(c2) from t1 b where b.c1=a.c1)
group by rowid,c1;
check the FIDDLE
Indeed, following the answer by #Pradeeshnarayan, I have been obliged to improve it to make it work in Oracle.
"Group by" clause is useless
select rowid, c1, c2 from t1 a
where c2=(select min(c2) from t1 b where b.c1=a.c1);

SQLITE equivalent for Oracle's ROWNUM?

I'm adding an 'index' column to a table in SQLite3 to allow the users to easily reorder the data, by renaming the old database and creating a new one in its place with the extra columns.
The problem I have is that I need to give each row a unique number in the 'index' column when I INSERT...SELECT the old values.
A search I did turned up a useful term in Oracle called ROWNUM, but SQLite3 doesn't have that. Is there something equivalent in SQLite?
You can use one of the special row names ROWID, OID or _ROWID_ to get the rowid of a column. See http://www.sqlite.org/lang_createtable.html#rowid for further details (and that the rows can be hidden by normal columns called ROWID and so on).
Many people here seems to mix up ROWNUM with ROWID. They are not the same concept and Oracle has both.
ROWID is a unique ID of a database ROW. It's almost invariant (changed during import/export but it is the same across different SQL queries).
ROWNUM is a calculated field corresponding to the row number in the query result. It's always 1 for the first row, 2 for the second, and so on. It is absolutely not linked to any table row and the same table row could have very different rownums depending of how it is queried.
Sqlite has a ROWID but no ROWNUM. The only equivalent I found is ROW_NUMBER() function (see http://www.sqlitetutorial.net/sqlite-window-functions/sqlite-row_number/).
You can achieve what you want with a query like this:
insert into new
select *, row_number() over ()
from old;
No SQLite doesn't have a direct equivalent to Oracle's ROWNUM.
If I understand your requirement correctly, you should be able to add a numbered column based on ordering of the old table this way:
create table old (col1, col2);
insert into old values
('d', 3),
('s', 3),
('d', 1),
('w', 45),
('b', 5465),
('w', 3),
('b', 23);
create table new (colPK INTEGER PRIMARY KEY AUTOINCREMENT, col1, col2);
insert into new select NULL, col1, col2 from old order by col1, col2;
The new table contains:
.headers on
.mode column
select * from new;
colPK col1 col2
---------- ---------- ----------
1 b 23
2 b 5465
3 d 1
4 d 3
5 s 3
6 w 3
7 w 45
The AUTOINCREMENT does what its name suggests: each additional row has the previous' value incremented by 1.
I believe you want to use the constrain LIMIT in SQLite.
SELECT * FROM TABLE can return thousands of records.
However, you can constrain this by adding the LIMIT keyword.
SELECT * FROM TABLE LIMIT 5;
Will return the first 5 records from the table returned in you query - if available
use this code For create Row_num 0....count_row
SELECT (SELECT COUNT(*)
FROM main AS t2
WHERE t2.col1 < t1.col1) + (SELECT COUNT(*)
FROM main AS t3
WHERE t3.col1 = t1.col1 AND t3.col1 < t1.col1) AS rowNum, * FROM Table_name t1 WHERE rowNum=0 ORDER BY t1.col1 ASC

Compare data of the same table

I need to compare data of the same table who do this.
At example, compare A,10 with B,10 when 10 is a repeat value.
You can do this using the cross-product operator, in SQL this would be done as
SELECT T1.colA, T2.colA, (T1.colA < T2.colA) as colA_comp
FROM TableName T1, TableName T2
WHERE T1.colB = T2.colB
What this does is take the cross-product of the table TableName with itself (renamed as T1 and T2), and the WHERE clause filters out those records that agree on colB (the repeated value 10, in your example).
If you compare with the same table then you may use left Join
SELECT t1.cola,t1.colb,t2.cola,...
from tableA t1
LEFT JOIN tableA t2 on t2.cola = t1.cola
WHERE t1.cola = 10
I hope it might be work!

Resources