I would like to update Table 2 based based on Table 1 that is given by:
Row sample_id PIK3CA_features
1 huDBF9DD chr3_3268035_CT
2 huDBF9DD chr3_3268043_AT
3 huDBF9DD chr3_3268049_T
Table 2:
Row sample_id chr3_3268035_CT chr3_3268043_AT chr3_3268049_C
1 huDBF9DD 1 1 null
2 huDBF9De null null null
3 huDBF9Dw null null null
For each row in Table 1, if its samle_id is correspondent in Table 2 then I'd like to update the respective PIK3CA_feature in Table 2 to 1.
How can I pass the sample_id and PIK3CA_features values from Table 1 as parameters to update Table 2 in a SQL command?
You can use an UPDATE statement to accomplish this. Assuming I understand correctly, you want something like this query:
#standardSQL
UPDATE table2 AS t2
SET
chr3_3268035_CT =
IF(t1.PIK3CA_features = 'chr3_3268035_CT', 1, chr3_3268035_CT),
chr3_3268043_AT =
IF(t1.PIK3CA_features = 'chr3_3268043_AT', 1, chr3_3268043_AT),
chr3_3268049_C =
IF(t1.PIK3CA_features = 'chr3_3268049_C', 1, chr3_3268049_C)
FROM table1 AS t1
WHERE true;
This will set the appropriate column in table 2 to have a value of 1 based on the value of PIK3CA_features. If you have a lot of these columns, you can generate the query using Python or some other programming language, or you can generate all the column_name=expression pairs using a query:
#standardSQL
SELECT
STRING_AGG(FORMAT('%s=IF(t1.PIK3CA_features="%s",1,%s)',
PIK3CA_features, PIK3CA_features, PIK3CA_features), ',\n')
FROM (
SELECT DISTINCT PIK3CA_features
FROM table1
);
This produces a list like:
chr3_3268035_CT=IF(t1.PIK3CA_features="chr3_3268035_CT",1,chr3_3268035_CT),
chr3_3268049_C=IF(t1.PIK3CA_features="chr3_3268049_C",1,chr3_3268049_C),
chr3_3268043_AT=IF(t1.PIK3CA_features="chr3_3268043_AT",1,chr3_3268043_AT)
Related
Using Oracle SQL, I am trying to insert into table A based on select from table B, but I am not sure how to achieve this, since the select is returning more than one row.
INSERT INTO A
VALUES
(
SELECT id FROM B WHERE status = 'APPROVED',
'Hardcoded-Value'
);
Table B:
id
status
1
APPROVED
2
DECLINED
3
APPROVED
Based on that insert, I want to achieve following:
Table A:
Column A
Column B
1
Hardcoded-Value
3
Hardcoded-Value
You can use a const in the select list
INSERT INTO A(colA, colB)
SELECT id, 'Hardcoded-Value'
FROM B
WHERE status = 'APPROVED'
I have come across a unique situation where I have a column called id which may have temp id until the final id comes through like:
id
temp id
1
null
2
1
6
null
7
6
I want a query that updates the table as :
id
temp id
2
null
2
1
7
null
7
6
basically once the id has a temp id associated with id, we just update all those temp ids with the real_id.
Any idea if this can be achieved. I try using case statements inside the updated table set but this doesn't work for me and also there are thousands of such records.
No issues with the temp id being redundant later because that id cannot repeat itself and thus it will not be a concern for analysis as we will use id only for analysis
You can use an update:
update t
set id = (select t2.id from t t2 where t2.tempid = t.id)
where t.tempid is null;
I have a table that has multiple duplicate records in the first column (ID records), but has varying numerical data in the second column.
I want to be able to identify which ID records have 0 for all of their numerical records.
For example the table can look like:
ID Value
1 2
1 2
1 0
2 0
2 0
2 0
I would want to only identify ID 2 because all the values are equal to 0. I don't want ID 1 because there are values > 0
Sorry if this isn't formatted properly or confusing.
You might use "NOT IN":
SELECT DISTINCT Id
FROM table1
WHERE Id NOT IN (SELECT Id FROM table1 WHERE Value <> 0)
SELECT DISTINCT ID FROM TABLE
WHERE ID NOT IN (SELECT DISTINCT ID FROM TABLE WHERE VALUE <> 0)
This will take all ID in the table where there is not a row where the value is non-zero.
SELECT *
FROM table t1
WHERE NOT EXISTS (SELECT 1 FROM table t2 WHERE t2.ID = t1.id AND Value <> 0)
"Select all records whose ID is not in the set of records that have a non-zero Value."
I am trying to figure out how I can select multiple rows from a table, based on the id column.
Something like this:
select * from table where id=1 or id=2 or id=3
Should I loop through every id and perform a query for each iteration?
select *
from table
where id in (1, 2, 3)
If you want to have results where id = 1 and results where id = 2 and results where id = 3, you have to use a different logic.
You actually want results where id = 1 or id = 2 or id = 3
Or you want results where id in (1, 2, 3)
Or you want results where id between 1 and 3
I'm a bit confused on this. I have a data table structured like this:
Table: Data
DataID Val
1 Value 1
2 Value 2
3 Value 3
4 Value 4
Then I have another table structured like this:
Table: Table1
Col1 Col2
1 2
3 4
4 3
2 1
Both columns from Table1 point to the data in the data table. How can I get this data to show in a query? For example, a query to return this:
Query: Query1
Column1 Column2
Value 1 Value 2
Value 3 Value 4
Value 4 Value 3
Value 2 Value 1
I'm familiar enough with SQL to do a join with one column, but lost beyond that. Any help is appreciated. Sample sql or a link to something to read. Thanks!
PS: This is in sqlite
You can join the same table twice:
Select
d1.val As column1,
d2.val As column2
From table1 t
Join data d1 On ( d1.dataId = t.col1 )
Join data d2 On ( d2.dataId = t.col2 )