How to update a column with values depending upon conditions - Oracle - sql

My requirement is to update a column with 'Y' if a set of conditions are met or with 'N'. I want to do it in the same query.
For example :
CREATE TABLE TAB1 (COL1 INT, COL2 INT);
CREATE TABLE TAB2 (COL1 INT);
INSERT INTO TAB1 VALUES(1, 3);
INSERT INTO TAB1 VALUES(2, 3);
INSERT INTO TAB2 VALUES(1);
INSERT INTO TAB2 VALUES(1);
I want to update the COL2 of TAB1 with 'Y' if TAB1.COL1 matches with the TAB2.COL1 or COL2 should be updated with 'N'. This of course a simple example and the actual requirement far more complex.

Edit: Corrected my answer to update 0 as well.
Assuming Y as 1 and N as 0 (since you said COL2 is INT)
UPDATE TAB1 T1
SET T1.COL2 =
CASE WHEN EXISTS(SELECT 1 FROM TAB2 T2 WHERE T2.COL1 = T1.COL1) THEN 1
ELSE 0
END;
Here is the SQLFiddle.

Related

Arrange NULL at the end in each column of the table

I have a table that contains two columns col1, col2.
NULL need to be placed at the end of each column.
Data in my table now.
--- ---
col1 col2
--- ---
1 null
2 2
3 null
4 4
null 5
create table mytable (
col1 number(3),
col2 number(3)
);
insert into mytable values(1, null);
insert into mytable values(2, 2);
insert into mytable values(3, null);
insert into mytable values(4, 4);
insert into mytable values(null, 5);
I need to update them in this format.
--- ---
col1 col2
--- ---
1 2
2 4
3 5
4 null
This mostly solves the problem; you'll need to put the final update in a recursive trigger so it executes the proper number of times.
-- remove nulls from col2
delete from mytable where col2 is null;
-- erase col1
update mytable
set col1 = null;
-- add "footer" row
insert into mytable (col1,col2) values ((select count(*)+1 from mytable), null);
-- update col1 for 1 remaining record
update mytable
set col1 = (select count(*) from mytable where col1 is not null)
where col2 = (select min(col2) from mytable where col1 is null);

migration script sql server for inserting an item

I am trying to create an Migration Script but i need some help:, i had the very basic script to insert an item into a table but i am trying to do it in a way to check for the item first if it exists, then insert it else skip it
here is my code:
use mydatabase;
INSERT INTO dbo.mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html';
You don't need to repeat the expression just use value construct like that :
INSERT INTO dbo.mytable(col1,col2,col3)
SELECT t.col1, t.col2, t.col3
FROM (values (3, 'test1','test3/abc.html')) t (col1, col2, col3)
WHERE NOT EXISTS (
SELECT 1
FROM dbo.mytable m
WHERE m.col1 = t.col1
AND m.col2 = t.col2
AND m.col3 = t.col3
);
INSERT INTO dbo.mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html'
WHERE NOT EXISTS (
SELECT 1
FROM dbo.mytable
WHERE col1='3'
AND col2='test1'
AND col3='test3/abc.html'
)
You can change the where depending on what you consider already inserted.
Something like this (but you would need some kind of key to identify if the record exists or not) -
IF NOT EXISTS(SELECT 1 FROM dbo.mytable WHERE col1 = '3' AND col2 = 'test1' AND col3 = 'test3/abc.html')
BEGIN
insert into dbo.mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html'
END
Use a MERGE:
CREATE TABLE #mytable (col1 nvarchar(20), col2 nvarchar(20), col3 nvarchar(20))
INSERT INTO #mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html';
Merge #mytable t
USING (SELECT '3','test1','test3/abc.html') s (col1, col2, col3)
ON t.col1 = s.col1
AND t.col2 = s.col2
AND t.col3 = s.col3
when not matched by target then
INSERT (col1,col2,col3) VALUES (s.col1, s.col2, s.col3);

MERGE syntax SQL Server 2012 error

Have a question about the MERGE syntax for which I cannot find the answer.
I have the following case:
Step1:
create temp table #TempTbl
Step2: MERGE:
MERGE INTO T1 target
USING T2 AS source ON (bunch of columns)
WHEN MATCHED
UPDATE
SET some columns from target equal some columns from source
WHEN NOT MATCHED BY TARGET
THEN INSERT (bunch of columns)
VALUES (bunch of columns from SOURCE)
OUTPUT $action, deleted.* into #TempTbl
What I need to know is for my above steps wouldn't I find only empty data in my temporary table #TempTbl, as I only stated WHEN NOT MATCHED ... THEN INSERT, not DELETE?
Second question, what type of column should $action be, as I'm having the error message:
Column name or supplied values do not match table definition
Although I've tried to define the first column from my table both varchar(100), nvarchar(100), but with no luck. But, If I omit the $action field, then my statement works.
So, the column that will hold the $action should be nvarchar(10).
The following statement would add rows to the temp table for both insert and update (as the update is really a delete followed by an insert) but with different actions:
-- sample test data
create table t1 (col1 int, col2 int)
create table t2 (col1 int, col2 int)
insert t1 values (1,1),(2,1)
insert t2 values (2,2),(3,3)
create table #temptbl (dml_action nvarchar(10), col1 int, col2 int)
-- merge statement
merge into t1 target
using t2 as source
on target.col1 = source.col1
when matched
then update set target.col2 = source.col2
when not matched by target
then insert (col1, col2) values (source.col2, source.col2)
output $action, inserted.col1, inserted.col2 into #temptbl ;
-- sample result
select * from #temptbl
dml_action col1 col2
---------- ----------- -----------
INSERT 3 3
UPDATE 2 2
If you don't want the update rows you could wrap the entire batch into another statement like so:
insert #temptbl (dml_action, col1, col2)
select dml_action, col1, col2
from
(
merge into t1 target
using t2 as source
on target.col1 = source.col1
when matched
then update set target.col2 = source.col2
when not matched by target
then insert (col1, col2) values (source.col2, source.col2)
output $action as dml_action, inserted.col1, inserted.col2
) a
where a.dml_action = 'INSERT'

finding counts assigned to another field's value

Create table t1 (col1 (number), col2 (number), col3 (number);
Insert into t1 values (1,1,1);
Insert into t1 values (1,2,5);
Insert into t1 values (1,3,1);
Insert into t1 values (2,1,1);
Insert into t1 values (2,1,1);
Desired result
col1 col2
1 3
2 2
I need to return the value in col1 and the count of values found in col 2 for each distinct col1 value. Do not need col3
select col1, count(col1) from t1
group by col1

SQL get multiple values of columns in one row

I am using MS Sql server 2008 R2.
I have a query that gives me output like this
Col1....Col2
CV1.....AV1
CV1.....AV2
CV2.....AV3
CV2.....AV4
The query is
select Tab1.Col1, Tab2.Col2
from Table1 Tab1
JOIN Table2 Tab2 on Tab1.PKID = Tab2.FKID
What I want is one row for each distinct values in Col1 and in Col2 all the values related to col1 with comma or pipeline delimiter
Col1....Col2
CV1.....AV1,AV2
CV2.....AV3,AV4
Can anyone help me on this?
Basically I need something like group_concat that is available in My sql
CREATE TABLE a(
Col1 varchar(50),
Col2 varchar(20));
INSERT INTO a (Col1,Col2) values ('CV1','AV1');
INSERT INTO a (Col1,Col2) values ('CV1','AV2');
INSERT INTO a (Col1,Col2) values ('CV2','AV3');
INSERT INTO a (Col1,Col2) values ('CV2','AV4');
with t as (SELECT Col1,(CAST(Col2 AS nvarchar (12))) as col2 from a )
Select distinct T2.Col1,
substring((Select ',' + T1.col2 AS [text()]
From t T1
Where T1.Col1 = T2.Col1
ORDER BY T1.Col1
For XML PATH ('')),2, 100) [col2]
From t T2
Try this query. I am doing it in sql server. check at sqlfidddle
http://sqlfiddle.com/#!3/7ab28/1