I have a table called TableA and TableB.
TableA (fieldID, fieldType, fieldNo)
TableB (fieldNo, field1, field2)
TableA - fieldNo is a FK.
TableB - fieldNo is a PK.
fieldType is a combo box consisting of value1 and value2
I want it to only fill in records of fieldNo (in TableA) where fieldType Like "value1" using a validation rule. I tried using "If" but it doesn't work.
Related
How do I run the following query
INSERT INTO table1
SELECT sourceId
FROM table2
WHERE table2.id = table1.productId
given that table2 has an id column and a sourceId column (and few others) and table1 already contains productId and I want to copy the sourceId column into the table as well?
The error message I'm getting with this query is simply "Unknown column 'table1.productId' in where clause", but if I include table1.productId in the SELECT and table1 on the FROM row, I get the "Column count doesn't match value count at row 1" error.
Any idea how to fix up the query?
when you are inserting into a table, you have to specify what to insert in all columns of that table. Should look like this:
INSERT INTO table1 (column1, column2, column3, ...)
SELECT value1, value2, value3, ...
FROM table2
WHERE ...;
But in your case since you said table1 already contains productId, you probably should do an UPDATE and not an INSERT
UPDATE table1 t1
SET t1.sourceId =
(SELECT sourceId
FROM table2
WHERE table2.id = t1.productId);
If this works for you, you can improve on it with JOINs
The select part is supposed to work stand-alone, so you must select from table1 in that part, if you want to refer to its rows.
INSERT INTO table1
SELECT sourceId
FROM table2
WHERE table2.id IN (SELECT productId FROM table1);
In MySQL, I could use
update TableA, TableB
set TableA.field1 = TableB.field1
where TableA.id = TableB.id
TableA
id field1 field2
1 hello hi
TableB
id field1 field2
1 world earth
After run the query, TableA value will be updated to
TableA
id field1 field
1 world hi
In BigQuery, I cannot do the same. It seems that I have to loop through TableB, update TableA for each matching row. BigQuery is with huge data. Looping through TableB for each matching row, do an update query is not going to perform well.
Or for performance reason, in bigquery, I could
select field1, field2 from TableA where id not in Select id from TableB (if id not in TableB, take TableA values)
select TableB.field1 as field1, TableA.field2 as field2 from TableA, TableB where TableA.id = TableB.id (if Id in TableB, then use TableB.field1 value)
Union the above two results
Any suggestions?
In BigQuery, I cannot do the same ...
You sure can!!! With DML of BigQuery Standard SQL
UPDATE `project.dataset.TableA` AS TableA
SET TableA.field1 = TableB.field1
FROM `project.dataset.TableB` AS TableB
WHERE TableA.id = TableB.id
I am really confused writing this SQL query, it might be easy but I still cannot come to the right solution.
Idea: Delete rows (foreign keys) from TableA based on TableB, if in TableB exist Primary Keys which match some other value within TableB.
For table B it should look like this:
SELECT Column1
FROM TableB
WHERE Column2 = 'Value';
And then
Delete rows in TableA which match to values inside of Column1 (TableB).
IN operator is good when you have hard coded values in IN operator like where SomeCoumn IN ('value1', 'Value2')
Or you are checking against a Primary key column like WHERE SomeColumn IN (select PK_Column from SomeTable)
Because in either of the above cases you will not have a NULL value inside your IN operator.
Null values inside IN operator brings back unexpected results.
A better option would be to use Exists operator... something like....
DELETE FROM TableA
WHERE EXISTS ( SELECT 1
FROM TableB
WHERE TableA.ColumnX = TableB.Column1
AND TableB.Column2 = 'Value'
);
Assuming that you need to match ColumnX in TableA:
DELETE FROM TableA
WHERE ColumnX IN (SELECT Column1
FROM TableB
WHERE Column2 = 'Value');
Am new to SQL and am stuck here with a very simple-looking query request.
I have 2 tables, both having exactly the same structure (IE same no. of columns, same no. Of rows) except for the actual contents. so for example,tableA has 2 columns called col1&col2; tableB has 2 columns too called col1&col2. Now I want to create a 3rd new tale, where 1st column is tableA's col1, and 2nd column is tableB's col1. preferably the name of the 1st column is fromTableA, and name of 2nd column is fromTableC. How do I achieve this please? I tried all the following ways but I always get the same error: "number of query values and destination fields are not the same."
variation 1:
insert into newTable(fromTable1,fromTable2)
select col1 from table1
select col1 from table2
variation 2:
insert into newTable(fromTable1,fromTable2)
select col1 from table1,col1 from table2
variation 3:
insert into newTable(fromTable1,fromTable2)
select col1 from table1, table2
Presumably you have fields in the two tables that can be joined, so this:
insert into newtable (romTable1,fromTable2)
select a.col1, b.col1
from table1 a, table2 b
where a.col1 = b.col1;
The a/b are aliases that differentiate between the two columns in each table. If you don't have fields to join then whatever you're trying to do probably needs a rethink.
You may try following sql query to achieve your purpose:
with OrderedTableA as (
select row_number() over (order by Col1) RowNum, *
from TableA (nolock)
),
OrderedTableB as (
select row_number() over (order by Col1) RowNum, *
from TableB (nolock)
)
select T1.Col1, T2.Col2 into TableC
from OrderedTableA T1
full outer join OrderedTableB T2 on T1.RowNum = T2.RowNum
Above query will create a new table as TableC with column col1 from TableA and col2 from TableB. You may change the queries to your need.
I hope you will understand the above queries. Give it a try.
I want to copy data to column A in Table1 from column B in Table2. Rows for column A are empty and there are exists other columns in Table1 with already populated data. So I need to grab the whole column B from Table2 and insert all those values in column A in Table1. The two table are completely identical, except that column A has no values at all.
How do I do this in sqlite3?
Use:
INSERT INTO TABLE1
SELECT B,
NULL,
NULL,
NULL
FROM TABLE2
Use NULL as the placeholder for however many columns you can't populate from TABLE2, assuming TABLE1 columns allow NULL values.
UPDATE TABLE1 SET A = (SELECT B FROM TABLE2 WHERE ...)
Come to think of it, if the tables are truly identical, why do you need two of them? In any case you can also do this:
BEGIN;
DELETE FROM TABLE1;
INSERT INTO TABLE1 (A, col1, col2, ...) SELECT (B, col2, col2, ...) FROM TABLE2;
COMMIT;
Try this:
INSERT INTO TABLE1 (A) SELECT B FROM TABLE2