SQL Update query - sql

Can i use several WHEN conditions in UPDATE clause to update a single column.
I want to update table TABLE having columns ID and NAME:
Is below query correct?
UPDATE TABLE
SET id = CASE id
WHEN id IN (2, 3, 4) THEN 1
WHEN id= 5 THEN 8
WHEN id IN(9, 7) THEN 6
WHERE name = 'abc'

Yes, that is allowed, but remove ID after CASE. And, you need to END your case.
UPDATE TABLE
SET ID = CASE
when ID in (2,3,4)
then 1
when ID = 5
then 8
when ID in (9,7)
then 6
END
where NAME = 'abc'
There are two alternate syntaxes for CASE. As above, and the other is where you want to compare a single value against others, like this:
UPDATE TABLE
SET ID = CASE ID
when 2
then 1
when 5
then 8
when 7
then 6
END
where NAME = 'abc'

Case comes in two versions:
version 1:
Case Id
When 2 Then 1
When 3 Then 1
When 4 Then 1
When 5 Then 8
When 7 Then 6
When 9 Then 6
End
version 2:
Case
When Id in (2,3,4) Then 1
When Id = 5 Then 8
When Id in (9,7) Then 6
End
Both of above are equivilent

Related

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Update multiple rows in one query

My current table:
id | count | group_id
1 1 employee
2 2 employee
3 3 employee
4 4 employee
What I want:
id | count | group_id
1 4 employee
2 3 employee
3 2 employee
4 1 employee
What i've attempted
UPDATE table SET count = 4 WHERE count = 1 AND group_id='employee';
UPDATE table SET count = 3 WHERE count = 2 AND group_id='employee';
UPDATE table SET count = 2 WHERE count = 3 AND group_id='employee';
UPDATE table SET count = 1 WHERE count = 4 AND group_id='employee';
For obvious reason this does not work because it executes each query row by row, so my result is wrong. I think i'm looking for a way of updating multiple tables with one query?
This specific case can be solved like this:
UPDATE table SET count = 5 - count
WHERE count between 1 and 4 AND group_id= 'employee';
A more general solution, use a CASE expression:
UPDATE table SET count = case count when 4 then 1
when 3 then 2
when 2 then 3
when 1 then 4
end
WHERE count between 1 and 4 AND group_id = 'employee';

How to select id when same id has multiple rows but I am looking for id which are missing a particular value

I have this table my_table_c with the below values
SELECT * FROM my_table_c
ID GROUP_ID GROUP_VALUE
1 2 1
3 3 2
3 4 1
5 4 1
5 2 1
2 2 2
2 3 2
2 4 1
I am looking for this output where I get only the ID which do not have group_id 2. Additionally, I don't want to get the ID where group_id 2 is absent but other group ids are present.
If group_id 2 is absent, that's my target id.
So with the values shown in table above, I just expect ID = 3 to be returned as other ids 1, 5 and 2 each have rows where group_id = 2.
Can anyone please help with a query to fetch this result.
You could get all the id's that have group_id = 2 and use NOT IN
select *
from my_table_c
where id not in (select id from my_table_c where group_id = 2)
Another way but using NOT EXISTS
select *
from my_table_c mtcA
where not exists (select *
from my_table_c mtcB
where mtcA.id = mtcB.id and mtcB.group_ID = 2)

how to get the even and odd column separately with separate column by query

I have an input:
id
1
2
3
4
5
6
7
8
9
10
I want get even and odd columns separately by columns in specified output like this
id col
1 2
3 4
5 6
7 8
9 10
here id and col are separate columns id contains the odd number and col contains the even number for specified input
SELECT MIN(id) as id, MAX(id) as col
FROM YourTable
GROUP BY FLOOR((id+1)/2)
For IDs 1 and 2, (id+1)/2 are 2/2 = 1 and 3/2 = 1.5, respectively, and FLOOR then returns 1 for both of them. Similarly, for 3 and 4, this is 2, and so on. So it groups all the input rows into pairs based on this formula. Then it uses MIN and MAX within each group to get the lower and higher IDs of the pairs.
Joined the table on itself
select *
from yourTable tA
left join yourTable tb on tA.id = (tB.id - 1)
where tA.id % 2 <> 0
If you use SQL you can try:
SELECT CASE WHEN column % 2 = 1
THEN column
ELSE null
END AS odds,
CASE WHEN column % 2 = 2
THEN column
ELSE null
END AS even
FROM yourtable
but not exactl as you ask
To show odd:
Select * from MEN where (RowID % 2) = 1
To show even:
Select * from MEN where (RowID % 2) = 0
Now, just join those two result sets and that's it.
Source

Combine two result sets and still keep one of the columns unique

I have two intermediate result sets in a create view statement. The result sets are derived from two different join paths and I need to union them. But it doesn't stop here. Since the ID column needs to be unique, I will then need the rows in result set 2 that contains the same IDs as the first result set to overwrite the same rows in the first result set.
Let me illustrate this here:
Result set 1
ID Value
------------
1 a
3 a
5 a
6 a
7 a
8 a
Result Set 2
ID Value
------------
2 b
4 b
5 b
7 b
9 b
10 b
End result set
ID value
------------
1 a
2 b
3 a
4 b
5 b
6 a
7 b
8 a
9 b
10 b
I am not sure how to approach this. Union/except/intersect will create duplicate ids, so that's no good.
SELECT COALESCE(set2.ID, set1.ID) AS ID,
CASE WHEN set2.ID IS NULL THEN set1.Value ELSE set2.Value END AS Value
FROM set1
FULL JOIN set2
ON set1.ID = set2.ID
Try deleting elements from result set 1 where id exists in result set 2 before union all.