A very simple CASE statement in sqlite - sqlite

If an entry in column1 is larger than or equal to 0 then it should print 1 in column2. Else it should print a 0.
The table looks like this:
create table table1 (column1 REAL);
insert into table1 values (8);
insert into table1 values (-10);
insert into table1 values (53);
And this is the query:
SELECT column1
CASE
WHEN column1 >=0 THEN 1
ELSE 0
END as column2
FROM table1;
So simple, but not working. Any help is greatly appreciated!!!

Result columns must be separated with a comma:
SELECT column1,
CASE ... END AS column2
FROM table1;

You are missing a comma after the column1 column name.
SELECT column1,
CASE
WHEN column1 >=0 THEN 1
ELSE 0
END as column2
FROM table1;

Related

Update table if any value =

I'm trying to update my table if any value from table1 = value from table2
Table1 as 1 column with data
Table2 as many columns with data
If table2(data) = table1(data) update
But isn't working
I had one code that was working if i set table2 with 1 column
This one is working but table2 needs to have 1 column only
UPDATE table1
SET column1 = 'correct'
WHERE column2 in (SELECT column1 from table2);
I want to be able to do having more column
maybe something like this:
UPDATE
SET column1 = 'correct'
WHERE column2 in (SELECT * from table2);
The error:
Result: sub-select returns 11 columns - expected 1
How should I do it?
Maybe you can do it with EXISTS:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn = table2.someothercolumn
);
If you want to check table1.someothercolumn against multiple columns of table2:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn in (table2.col1, table2.col2, ...)
);

"datatype mismatch" when doing an INSERT CASE in sqlite3

I have two tables, one is populated with data
CREATE TABLE `tableX` (`column1` REAL);
INSERT INTO tableX VALUES (5);
INSERT INTO tableX VALUES (-3);
CREATE TABLE `tableY`
(`rowid` INTEGER PRIMARY KEY, `column2` REAL);
The INSERT CASE statement is
INSERT INTO tableY SELECT column1,
CASE WHEN column1 >=0 THEN 1 ELSE 2 END as column2 FROM tableX;
Error is "datatype mismatch" in my DB Browser but it seems to work in sqlfiddle...not error at least
Any help is greatly appreciated! Thanks :D!
Not the most elegant solution, but it is a work around.
CREATE TABLE `tableX` (`column1` REAL);
INSERT INTO tableX VALUES (5);
INSERT INTO tableX VALUES (-3);
CREATE TABLE `tableZ` (`column1` REAL, `column3` REAL);
INSERT INTO tableZ SELECT column1,
CASE WHEN column1 >=0 THEN 1 ELSE 2 END as column3 FROM tableX;
CREATE TABLE 'tableY' (`rowid` INTEGER PRIMARY KEY, `column2` REAL)
INSERT INTO tableY (column2) SELECT column3 FROM tableZ;
DROP TABLE tableZ;

SQLITE UPDATE query replicating rows

I have an update query that when I run it instead of matching each unique row it is replicating the same row through the entire column.
Any help would be massively appreciated.
I have got
UPDATE Table1
SET Column1 = (SELECT Column1 FROM [Table2] WHERE Column2 = [Table2].Column2)
[Table2].Column2 referes to the column in Table2.
Column2 refers to the column in some table that has such a column. The innermost such table is Table2.
So this ends up being the same as Table2.Column2 = Table2.Column2.
To refer to the column in Table1, specify that table:
UPDATE Table1
SET Column1 = (SELECT Column1
FROM Table2
WHERE Table2.Column2 = Table1.Column2);

SQLite select where all columns are not null

I have column1, column2, column3, column4, column5 and so on.
If I know that one column is not null, I can use this query:
SELECT *
FROM table
WHERE columnA IS NOT NULL;
But now I want to select rows where all the columns are not null.
How to write is query easily?
Use the query look like this
SELECT *
FROM table_name
WHERE NOT column1 IS NULL AND NOT column2 IS NULL...

I need to create an Oracle query listing my table names followed by column names

Do you know how to create an Oracle query that will list my table name, followed by all the column names in that table? I have 5 tables in total.
Something like:
Table
Column1
Column2
Column3
Table2
Column1
Table3
Column1
Column2
I'm hard-pressed to imagine why you would want a single-column result that doesn't do anything to differentiate between what is a column name and what is a table name. You can do something like
select pseudo_column_name
from (
select table_name, table_name pseudo_column_name, 0 column_id
from user_tables
union all
select table_name, column_name, column_id
from user_tab_columns
)
order by table_name, column_id

Resources