How to concatenate a selection of sqlite columns spread over multiple tables? - sqlite

I am aware that you can concatenate multiple columns within a single table with a query like this:
SELECT ( column1 || column2 || column3 || ... ) AS some_name FROM some_table
Is it possible to concatenate columns from multiple tables with a single sqlite query?
Very simple example - Two tables and the result:
Table1 | Table2 | Result
Col1 Col2 | Col3 |
-------------------------------------------------------------------------------
A B | C | ABC
If this is possible, what would the query be?
I can always manually concatenate multiple single table concatenation results, but having sqlite do all the work would be great:)

Try this
SELECT a.col1 || a.col2 || b.col3 AS 'SUM'
FROM table1 a, table2 b
WHERE a.id = b.id;
In where you have top mention the joining condition between the tables
Fiddle

Related

how to join 2 table column data into single column

I did find some examples but they do not merge into single column.
So, I am trying to join 2 table columns data into single column
I have Url1, site1, url2, site2, endurl 5 columns in table1
and keywords column in table2
I want to join or merge these columns into one column like
url1 site1 keywords,url2 site2 keywords endurl this will convert to a url generation just for understanding.
I tried
SELECT table1.Url1, table1.site1, table1.url2, table1.site2, table1.endurl, table2.keywords
FROM table1
LEFT JOIN table2
ON table1.site1 = table2.keywords AND table1.site2 = table2.keywords;
want to merge all columns into single column.
What you're probably looking for is the format function which uses SQLite's builtin printf implementation. So, assuming your columns are all TEXT columns, this will give you what you're looking for:
SELECT format('%s, %s, %s, %s, %s, %s', table1.Url1, table1.site1, table1.url2, table1.site2, table1.endurl, table2.keywords) as my_column
FROM table1
LEFT JOIN table2
ON table1.site1 = table2.keywords AND table1.site2 = table2.keywords;
You could probably concat those columns into one.
Edit: Now for SQLite
SELECT t1.Url1 || ' ' || t1.site1 || ' ' || t2.keywords ||',' ||t1.url2||' '||t1.site2||' '||t3.keywords as column_name
FROM table1 t1, table2 t2, table2 t3
WHERE t1.site1 = t2.keywords AND t1.site2 = t3.keywords;

check for null values in all columns of a table using SQLite

I have a table with more than 15 columns. 2 of of them are of the type varchar, and most of them of type int and float.
I am new to SQL and am trying to figure out a way by which I can check if any of the columns have a NULL value in it.
Had there been just 4 or 5 columns I could have checked them individually with
SELECT COUNT(*) FROM table_name WHERE col1 IS NULL OR col2 IS NULL OR col3 IS NULL ...
But is there any efficient way to do this on a lot of columns in SQLite specifically?
I have referred to other questions regarding this here but I cannot use xml or store anything. Also I am using SQLite and can only run a query.
There is no way (that I know of) to check all columns if they contain null without explicitly listing all the column names.
Your query is the proper way to do it.
If you want to shorten (not significantly) the code you could use these alternatives:
SELECT COUNT(*) FROM table_name WHERE col1 + col2 + col3 IS NULL;
or:
SELECT COUNT(*) FROM table_name WHERE col1 || col2 || col3 IS NULL;
or:
SELECT COUNT(*) FROM table_name WHERE MAX(col1, col2, col3) IS NULL;
The above queries work, for any data type of the columns, because if there is even only 1 column equal to null then addition, concatenation and the scalar function MAX() (and MIN()) all return null.
See the demo.

Find difference between tables where values differ

When I search for how to compare two tables in SQLite, and see what's differ, I mostly find answers like this:
SELECT B.id FROM B LEFT JOIN A ON B.id = A.id WHERE A.id IS NULL
and yes, it's correct if you want do find all the elements (or values for keys named 'id' in this case) in table B that is not in table A, i.e. all the new elements in B if B is a later version of A.
But what if I want to find all the id:s in B where the value for a certain key (or keys) deviate from the corresponding value in A? For example, if I have two tables, A and B with id:s and positions, and I want to get the result id=3 in this case, because it is the element in B that has a value that differ. What would be the easiest way to do that?
Table A Table B
id | x_value | y_value id | x_value | y_value
----------------------- -----------------------
1 | 29.9563 | 12.6764 1 | 29.9563 | 12.6764
2 | 45.5843 | 7.6733 2 | 45.5843 | 7.6733
3 | 28.2313 | 15.6579 3 | 39.2003 | 15.6579
Result:
id
--
3
You can do it with a inner join with your condition in the where clause.
select a.id
from tableA a join tableB b on a.id = b.id
where ifnull(a.x_value, 0) <> ifnull(b.x_value, 0)
or ifnull(a.y_value, 0) <> ifnull(b.y_value, 0)
You can use INTERSECT:
LiveDemo
SqlFiddleDemo
SELECT tA.id
FROM TableA tA
JOIN TableB tB
ON tA.id = tB.id
WHERE NOT EXISTS( SELECT tA.x_value, tA.y_value
INTERSECT
SELECT tB.x_value, tB.y_value);
I like this solution, because it is easy to extend. Just add new column names. No need to handle NULL manually.
I agree with shawnt00 that you can read the question that the goal was to find all the id:s where values have changed between the two tables AND id:s of new instances inserted to the second table. Here is the select-statement to accomplish that, if anyone is interested:
select b.id
from b left join a on b.id = a.id
where ifnull(a.x_value, 0) <> ifnull(b.x_value, 0)
or ifnull(a.y_value, 0) <> ifnull(b.y_value, 0)
or a.id is null;

Update SQL column with SELECT statement data

I am currently working with DB Browser and I have a problem with updating columns.
I have a table which contains two columns: one is filled with strings and the other one is completely empty.
I have trimmed these strings with rtrim():
SELECT rtrim(column1, 'e') FROM table1
Now I want to place these trimmed strings into the empty column:
UPDATE table1 SET column2 = (SELECT rtrim(column1, 'e') FROM table1)
But now it fills the empty column with only the first trimmed string of column1, because it won't see the SELECT statement as a column, but as a string.
I also tried:
INSERT INTO table1 (column2) SELECT rtrim(column1, 'e') FROM table1
But this just doubles the amount of rows, as column2 is not 'empty', but filled with null.
I want to get something like
| column1 | column2 |
---------------------
| apple | appl |
| orange | orang |
| grape | grap |
Any ideas on how to solve this?

Zipping rows with the same "key" while joining tables

I have two tables, one with objects, one with properties of the objects. Both tables have a personal ID and a date as "key", but since multiple orders of objects can be done by one person on a single day, it doesn't match well. I do know however, that the entries are entered in the same order in both tables, so it is possible to join on the order, if the personID and date are the same.
This is what I want to accomplish:
Table 1:
PersonID Date Object
1 20-08-2013 A
2 13-11-2013 B
2 13-11-2013 C
2 13-11-2013 D
3 21-11-2013 E
Table 2:
PersonID Date Property
4 05-05-2013 $
1 20-08-2013 ^
2 13-11-2013 /
2 13-11-2013 *
2 13-11-2013 +
3 21-11-2013 &
Result:
PersonID Date Object Property
4 05-05-2013 $
1 20-08-2013 A ^
2 13-11-2013 B /
2 13-11-2013 C *
2 13-11-2013 D +
3 21-11-2013 E &
So what I want to do, is join the two tables and "zip" the group of entries that have the same (PersonID,Date) "key".
Something called "Slick" seems to have this (see here), but I'd like to do it in SQLite.
Any advice would be amazing!
You are on the right track. Why not just do a LEFT JOIN between the tables like
select t2.PersonID,
t2.Date,
t1.Object,
t2.Property
from table2 t2
left join table1 t1 on t2.PersonID = t1.PersonID
order by t2.PersonID
Use a additional column to make every key unique in both tables. For example in SQLite you could use RowIDs to keep track of the order of insertion. To store this additional column in the database itself might be useful for other queries as well, but you do not have to store this.
First add the column ID to both tables, the DDL queries should now look like this: (make sure you do not add the primary key constraint until both tables are filled.
CREATE TABLE table1 (
ID,
PersonID,
Date,
Object
);
CREATE TABLE table2 (
ID,
PersonID,
Date,
Property
);
Now populate the ID column. You can adjust the ID to your liking. Make sure you do this for table2 as well:
UPDATE table1
SET ID =(
SELECT table1.PersonID || '-' || table1.Date || '-' || count( * )
FROM table1 tB
WHERE table1.RowID >= tB.RowID
AND
table1.PersonID == tB.PersonID
AND
table1.Date == tB.Date
);
Now you can join them:
SELECT t2.PersonID,
t2.Date,
t1.Object,
t2.Property
FROM table2 t2
LEFT JOIN table1 t1
ON t2.ID = t1.ID;

Resources