How to SQL Query records from Multiple that Equal 0? - sql

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."

Related

SQL Server : select column with 3 matching characters

I am selecting the records from 2 tables in which the 1st table column named DESC (first 3 characters) should match with the project column of the 2nd table.
I want to get the last 2 characters from Table 1 column DESC to be added in my output, but the last 2 characters are not present in Table 2 column project.
select SUBSTRING(a.[DESC], 1, 3)
from Table1 a
join Table2 b on SUBSTRING(a.[DESC], 1, 3) = b.project
Input: 1st Table DESC Column: Value: '2AB F YY'
2nd Table Project Column: Value: '2AB'
Expected Output: Return all the records of value 2AB
Column result:
'2AB YY'
Wrong output: all the records of value starting other then 2AB
One option is as follows
with data
as (
select SUBSTRING(a.[DESC],1,3) as first_3_characters,
,REVERSE(SUBSTRING(REVERSE(a.[DESC]),1,2)) as last_2_char_tab1
,REVERSE(SUBSTRING(REVERSE(b.project),1,2)) as last_2_char_tab2 characters_tab2
from Table1 a
join Table2 b
on SUBSTRING(a.[DESC],1,3) = b.project
)
select *,CONCAT(first_3_characters,last_2_characters)
from data
where last_2_char_tab1 <> last_2_char_tab2
Since you don't seem to need data from Table2, an EXISTS could be used for this.
And RIGHT can be used to get the last N characters of a string.
SELECT
CONCAT(LEFT([DESC], 3),' ', RIGHT([DESC], 2))
FROM Table1 t1
WHERE EXISTS
(
SELECT 1
FROM Table2 t2
WHERE t2.project = LEFT(t1.[DESC], 3)
)
ORDER BY 1;

How select values where all columns are null for particular ID, ID is not unique

I have a table with following format and I want to get the LotId if Value1 is null for all the rows.
Now If I am doing Select,
Select * from Table1 where Value1 IS null , I am getting back a row .
But I want nothing should be returned as there are two rows which have some value.
I thought of self join , but this can have n number of rows.
Id LotId Value1
-------------------------------------------------
1 LOt0065 NULL
2 LOt0065 SomeValue
3 LOt0065 SomeValue
I think you'll need to use an EXISTS subquery here:
SELECT a.lotid
FROM table1 a
WHERE NOT EXISTS (
SELECT 1
FROM table1 b
WHERE b.lotid = a.lotid
AND b.value1 IS NOT NULL
);
If my syntax is right, then this will show you all records that don't have any NULL values for that lotid:
It uses a SELECT 1 because the subquery doesn't need to show any value, it just needs to match on the outer query.
You compare the table in the inner query to the table in the outer query and match on the common field you're looking at (lotid in this case)
This could also be done with a NOT IN clause.
Does this give you the result you want?

bigQuery assign a value to table 1 based on table 2

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)

Select from a table based on another table Number of records Oracle

I have a unique requirement to select records from one table based on another table only if the second table has at least one record with a certain flag. The query should not return two records for the same ID: example:
Table 1
id name location
4 myname MyLocation
6 hisname HisLocation
7 hername herlocation
The id in this table are unique:
Table two
id details1 details2 closureflg
4 somdetails somedetails Y
4 somdetails somedetails Y
6 somdetails somedetails N
7 somdetails somedetails N
7 somdetails somedetail N
I need to select from the first table one record only as long as the corresponding id has records in Table 2 whose closure flag is N:
I have tried:
select * from table1 where id in(select id from tbale2 where closureflg = 'N');
this returns two records for id 7;
My expected output:
id name location
6 hisname HisLocation
7 hername herlocation.
Please help.
please, try this one
SELECT *
FROM table1 t1
WHERE EXISTS(SELECT * FROM table2 t2 WHERE closureflag = 'N' AND t1.ID = t2.ID);
I would try it with a join
select id, name, location from table1
join table2 on table1.id=table2.id
where table2.closureflg = 'N'
group by id

SQL query to find rows where 1 column value matches another column on another row

I have a database table with a column called 'symbol', that is unique via a non-clustered index.
We now need to change the data in the 'symbol' column, using the data from another column in the same table, say column2.
Trying to do an update, e.g.
update table
set symbol = column2
where column2 <> '' and
deleted = 0
results in a 'Cannot insert duplicate key row in object' error, so there must be 1 or more rows existing in the table that already have a value in the symbol column that is equal to the value in column2, or there are some rows that have a duplicate column 2 value.
I can find the rows that have duplicates in column2, but I'm struggling to come up with a query to find those rows that have a value in the symbol column that exists in any row in column2. Any one got any ideas?
Thanks.
select t1.symbol, count(0) as rows
from table t1
join table t2 on t2.column2 = t1.symbol
group by t1.symbol
Test data:
symbol column2
----------- -----------
1 1
2 1
3 3
4 5
find those rows that have a value in the symbol column that exists in
any row in column2.
select symbol, column2
from table
where symbol in (select column2
from table)
Result:
symbol column2
----------- -----------
1 1
3 3
Or possibly this depending on what result you want.
select symbol, column2
from table as T1
where exists (select *
from table as T2
where T1.symbol = T2.column2 and
T1.symbol <> T2.symbol)
Result:
symbol column2
----------- -----------
1 1