SQL UPDATE Select row based on column cross referenced values - sql

I would like to do an update to a row based on a value in another column.
Example table
Col1 Col2 Col3
'A' | '' | 100
'B' | '' | 120
'C' | 'A' | 0
UPDATE Table SET Col3 = Col3 - 10 WHERE Col1 = 'A'
However, if Col2 is not blank then I want to update the row that has it's value in Col2 so that
UPDATE Table SET Col3 = Col3 - 10 WHERE Col1 = 'C'
would update the Col1 = 'A' row
update
I discovered the following query works for me
UPDATE Table SET col3 = Col3 - 10
WHERE Col2 = '' and Col1 = 'C'
OR Col1 = (SELECT Col2 FROM Table WHERE Col1 = 'C')

where tbl is the table name
update tbl set col3=col3-10
from tbl t
inner join tbl tt
on t.col1=tt.col2 or t.col2=''
where t.col1 = 'C'

Related

Print value in SQL depending on its presence in another column

I have a table of the form
Col1 | Col2
-------------
A | C
B | A
C | X
D | A
E | NULL
If any element of Col1 is present in Col2, then It should be printed as
Element, YES.
If it is not present in Col2, then it needs to be printed as element, NO and if corresponding col2 value is NULL then it needs to be printed as element, NULL
So final output should look like
A YES
B NO
C YES
D NO
E NULL
I was able to write three individual queries for the same but am struggling with the moment on how to put them inside Case statements in SQL.
SELECT Col1 FROM table WHERE col1 IN (SELECT col2 FROM table)
Select col1 FROM table where Col2 is NULL
SELECT Col1 FROM table WHERE col1 NOT IN (SELECT col2 FROM table)
I tried putting them inside case statements
Select col1, Case
when (SELECT Col1 FROM table WHERE col1 IN (SELECT col2 FROM table))
then "YES"
when (Select col1 FROM table where Col2 is NULL)
then "NULL"
else
"NO"
But I was getting an error. How should I fix this?
I would expect the query to look like this:
select col1,
(case when col2 is null then NULL
when col1 in (select t2.col2 from t t2)
then 'YES'
else 'NO'
end)
from t;

Distinct by comparing multiple columns SQL

I have a select query
Select col1,col2,col3
from table;
The table contains following rows
col1 col2 col3
A | B | C
B | A | C
C | B | C
I need to get the distinct result which contains a single combination A,B,C by comparing multiple columns.
Result Should be some thing like
col1 col2 col3
A | B | C
the order can be changed of result rows.
How can I achieve this ?
Please try out this, I am not sure about you proper requirement. But on sample data given above. I have came across this solution,
With CTE as
(
Select MIN(col1) as col1 from MyTable
)
Select * from CTE
cross apply
(
Select MIN(col2) as col2 from MyTable
where col2 <> CTE.col1
)as a
cross apply
(
Select MIN(col3) as col3 from MyTable
where col3 not in (CTE.col1,a.col2)
)as b
DEMO HERE
SELECT *
FROM table
WHERE (col1 = 'A' AND col2 = 'B' AND col3 = 'C')
You can also go with this below query if the number of column and the values are known.
The CASE statement is the closest to IF in SQL
SELECT
CASE
WHEN (col1 = 'A' and col2 = 'B' and col3='C') or (col1 = 'C' and col2 = 'A' and col3='B') or (col1 = 'B' and col2 = 'C' and col3='A' )
THEN 1
ELSE 0
END as RESULT, *
FROM table
From the result you can take the required output by checking the value of RESULT==1(integer)
If you want the result as a boolean value then do the CAST like ,
SELECT
CAST(
CASE
WHEN (col1 = 'A' and col2 = 'B' and col3='C') or (col1 = 'C' and col2 = 'A' and col3='B') or (col1 = 'B' and col2 = 'C' and col3='A' )
THEN 1
ELSE 0
END as RESULT_INT)
as RESUTL, *
FROM table

How to get the minimum value in a set of columns while excluding a certain value?

I have been trying a query to select the minimum value in a row but also exclude a certain value (-998).
The table looks like this:
col1 col2 col3
----------------------------------
1 1 -998
2 -998 2
3 2 1
-998 1 3
So in the first row, the minimum value would be 1; in the second row, it would be 2; and in the third row, it would be 1 again.
I tried using a case statement and excluding -998 in each condition, but it keeps grabbing -998 for some reason.
SELECT
CASE
WHERE (col1 <= col2 and col1 <= col3) and col1 != -998 THEN col1
WHERE (col2 <= col1 and col2 <= col3) and col2 != -998 THEN col2
WHERE (col3 <= col1 and col3 <= col2) and col3 != -998 THEN col3
END AS [MIN_VAL]
FROM myTable
If anyone can point me in the right direction that would ge awesome.
Use the table value constructor to unpivot your column values and exclude values from there.
SQL Fiddle
MS SQL Server 2012 Schema Setup:
create table YourTable
(
col1 int,
col2 int,
col3 int
);
insert into YourTable values
(1 , 1 , -998),
(2 , -998 , 2 ),
(3 , 2 , 1 ),
(-998 , 1 , 3 );
Query 1:
select (
select min(R.Value)
from (values(T.col1),
(T.col2),
(T.col3)) as R(Value)
where R.Value <> -998
) as min_val
from YourTable as T;
Results:
| MIN_VAL |
|---------|
| 1 |
| 2 |
| 1 |
| 1 |
How about this:
use tempdb
create table myTable(
col1 int,
col2 int,
col3 int
)
insert into myTable values
(1, 1, -998),
(2, -998, 2),
(3, 2, 1),
(-998, 1, 3)
;with cte as(
select
rn = row_number() over(order by (select null)),
col = col1
from myTable
union all
select
rn = row_number() over(order by (select null)),
col = col2
from myTable
union all
select
rn = row_number() over(order by (select null)),
col = col3
from myTable
)
select
minimum = min(col)
from cte
where col <> - 998
group by rn
drop table mytable
SELECT
CASE
WHEN (col1 <= col2 or col2 = -998)
and (col1 <= col3 or col3 = -998)
and col1 != -998
THEN col1
WHEN (col2 <= col1 or col1 = -998)
and (col2 <= col3 or col3 = -998)
and col2 != -998
THEN col2
WHEN (col3 <= col1 or col1 = -998)
and (col3 <= col2 or col2 = -998)
and col3 != -998
THEN col3
END AS [MIN_VAL]
FROM myTable;

Where clause based upon the null and non null column in SQL Server

Table
ID | col1 | col2
-------------------------
1 | val1 | val2
2 | val1 | null
3 | val1 | val3
4 | val5 | null
Client will always pass valid col1 and col2 values to select single row where col2 might not exist in table. So if col2 is available than it should return the row with provided col2 otherwise return row which contain col2 = null and col1
return ID = 2 if col1 = val1 and col2 = val10
return ID = 3 if col1 = val1 and col2 = val3
How can this be accomplish with single SQL query?
This will work. Without top 1 the query may select 2 rows so I order it to return the one where col2 is not null first.
select top 1 * from table1
where col1 = #param1 and (col2 is null or col2 = #param2)
order by case when col2 is null then 1 else 0 end
select * from table1 where col1 = #col1 and col2 = #col2
union
select * from table1 as t where col1 = #col1 and col2 is null
and not exist (select * from table1 as c where c.col1 = t.col1 and c.col2 = #col2)
i'm not sure if the solution with top 1 is faster, sometimes ordering is very slow.

SQL assigning a value after testing against multiple columns

I have two columns:
INPUT
col1 col2
1 0
1 0
1 0
2 1
2 0
3 0
3 0
3 1
3 1
Let's suppose col1 holds some entity ID number, which is repeated. I'm testing whether this entity ID contains value 1 in col2. So if entity ID has 1 in corresponding col2 then I create another col3 with "NO"/"YES" values or just "0"/"1" accrodingly.
OUTPUT
col1 col3
1 NO
2 YES
3 YES
SELECT Col1, MAX(CASE WHEN col2 = 1 THEN 1 ELSE 0 END) Col3
FROM YourTable
GROUP BY Col1
UPDATED
Well, since the query above doesn't work for you, you can try the following:
SELECT Col1, MAX(Col3) Col3
FROM ( SELECT Col1,
CASE WHEN col2 = 1 THEN 1 ELSE 0 END AS Col3
FROM YourTable) A
GROUP BY Col1
UPDATE table
SET
col3 = DECODE(col2, 1, 'YES', 'NO');
If you want to run this for a given entity id only:
UPDATE table
SET
col3 = DECODE(col2, 1, 'YES', 'NO')
WHERE
col1 = yourid;
If your column 3 does not exist, you'll have to create it before hand anyway:
ALTER TABLE table ADD (col3 NUMBER NOT NULL);
SELECT Col1,
CASE WHEN MAX(Col2) = 1 THEN 'YES' ELSE 'NO' END AS Col3
FROM TableName
GROUP BY Col1
SQLFiddle Demo
SELECT DISTINCT col1,
CASE WHEN EXISTS(SELECT * FROM MyTab WHERE col1 = M.col1 AND col2 = 1)
THEN 'Yes' ELSE 'No' AS col3 FROM MyTab M