Add a if clause into a sql query - sql

I got a query
sql_query := ' Select A,B,C from table where A = 'DG54FDG45SD' and B = 'FS487' ;
and I want to add it a if clause like this :
sql_query := ' Select A,B,C,D from table where A = 'DG54FDG45SD' and B = 'FS487' and (if C = 'AR' then AND D in (select uid from table2 where id = 'DGF'));
Thanks !

You don't do this with an if in a where clause -- simply because it is not necessary. You can arrive at the same logic uses basic boolean operations:
where A = 'DG54FDG45SD' and
B = 'FS487' and
(D in (select uid from table2 where id = 'DGF') or
C <> 'AR'
)
If you need to take NULL into account:
where A = 'DG54FDG45SD' and
B = 'FS487' and
(D in (select uid from table2 where id = 'DGF') or
C <> 'AR' or C is null
)

Related

sql function case returns more than one row

Going to use this query as a subquery, the problem is it returns many rows of duplicates. Tried to use COUNT() instead of exists, but it still returns a multiple answer.
Every table can only contain one record of superRef.
The below query I`ll use in SELECT col_a, [the CASE] From MyTable
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = myTable.sysno AND A_specAttr = 'value')
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo WHERE C_superRef = myTable.sysno AND b_type = 2)
THEN 2
ELSE (SELECT C_intType FROM C
WHERE C_superRef = myTable.sysno)
END
FROM A, B, C
result:
3
3
3
3
3
3...
What if you did this? Because Im guessing you are getting an implicit full outer join A X B X C then running the case statement for each row in that result set.
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = 1000001838012)
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo AND C_superRef = 1000001838012 )
THEN 2
ELSE (SELECT C_type FROM C
WHERE C_superRef = 1000001838012)
END
FROM ( SELECT COUNT(*) FROM A ) --This is a hack but should work in ANSI sql.
--Your milage my vary with different RDBMS flavors.
DUAL is what I needed, thanks to Thorsten Kettner
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = 1000001838012)
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo AND C_superRef = 1000001838012 )
THEN 2
ELSE (SELECT C_type FROM C
WHERE C_superRef = 1000001838012)
END
FROM DUAL

Oracle: Update field to 'T' based on criteria in subquery

I have a table that I'm attempting to add a boolean flag field to based on an aggregate query. Trying to put this out in the simplest form without providing customer information, I'll call the table MyTable with fields An, Ver, Co, and Rep.
MyTable
An Ver Co Rep NewFlag
A 1 Aa T
A 1 Ab F
A 1 Ac T
B 1 Ba F
B 1 Bb F
B 1 Bc T
C 1 Ca F
C 1 Cb T
C 1 Cc F
I want to set NewFlag = 'T' where there for a grouping of An, Ver, Co, there is only one Rep = 'T', otherwise it can remain null.
I've got a query that identifies the An, Ver, Co where the count of Reps='T' = 1, and want to use a result site like the following as my criteria to update the NewFlag field
QueryResults
An Ver Co
B 1 Bc
C 1 Cb
In a way, it would be something like this:
Update MyTable
Set NewFlag = 'T'
where
MyTable.An = QueryResults.An,
MyTable.Ver = QueryResults.Ver,
MyTable.Co = QueryResults.Co
QueryResults being the result set of my query that identifies those records with only one count of Rep = 'T'.
Outside of using a script to loop through and update records one-by-one, is there syntax where I can just insert my query in to update those particular fields?
You want a correlated subquery or in. Something like this:
Update MyTable
Set NewFlag = 'T'
where (An, Ver, Co) in (select An, Ver, Co
from <your query>
);
MERGE is perfect for this kind of assignment:
merge into mytable m
using ( select an, ver, co
from mytable
where rep = 'T'
group by an, ver, co
having count(*) = 1
) x
on ( m.an = x.an and m.ver = x.ver and m.co = x.co )
when matched then update set newflag = 'T'
;

how can get the result and appoint into next

1 )
SELECT A.SSID FROM T_TABLE_1 A, T_TABLE_2 B WHERE A.SSID = B.SSID AND B.NUMBER = '123456';`
2)
delete from T_TABLE_3 where ssid='139729252';
delete from T_TABLE_4 where ssid='139729252';
Result of 1) is a SSID, eg: '139729252' ,how can I use the result of 1) into 2), no need to copy and paste every time? thanks.
Just use IN operator if you expect more than 1 record to be retrieved using your select statement. Else you can use = operator.
Ex:
delete from T_TABLE_3
where ssid=(SELECT A.SSID FROM T_TABLE_1 A, T_TABLE_2 B WHERE A.SSID = B.SSID AND B.NUMBER = '123456');
or
delete from T_TABLE_3
where ssid IN (SELECT A.SSID FROM T_TABLE_1 A, T_TABLE_2 B WHERE A.SSID = B.SSID AND B.NUMBER = '123456');
delete from T_TABLE_3 where ssid in ( select a.ssid t_table_1 a, t_table_2 b where a.ssid=b.ssid and b.number='123456');
or

Want to copy one column data into anothers table column in SQL...using this query its not working

UPDATE temp_test
SET country_i = (select a.country_code from temp_test2 a,temp_test c
where c.country_i is null and
a.active = 'A' and c.created_by = a.partner_by)
You could use a MERGE statement. It is easy to understand.
MERGE INTO temp_test t
USING temp_test2 u
ON (t.created_by = u.partner_by)
WHEN MATCHED THEN
UPDATE SET
t.country_i = u.country_code
WHERE t.country_i IS NULL
AND u.active = 'A';
UPDATE temp_test c SET country_i = (select a.country_code from temp_test2 a where a.active = 'A' and c.created_by = a.partner_by) where c.country_i is null;
In my mind it should work...

update multiple columns in oracle

I have query :
UPDATE SDM_KARYAWAN
SET (ID_DIVISI,ID_UNIT_KERJA,ID_JABATAN) =
(
SELECT ID_DIVISI,ID_UNIT_KERJA,ID_JABATAN FROM
(
SELECT TO_CHAR(TGL_SK,'YYYYMMDD') URUT,ID_DIVISI,ID_UNIT_KERJA,ID_JABATAN
FROM SDM_KARYAWAN_JABATAN
WHERE ID_KARYAWAN = '0081005'
ORDER BY URUT DESC
)DETAIL
WHERE ROWNUM = 1
)X
WHERE ID_KARYAWAN = '0081005'
But show error like this : [Err] ORA-00933: SQL command not properly ended
Actually I can use this code :
UPDATE TABLE1 SET COL1 = 'A',COL2='B',COL3='C'...
but What i want is updating multiple columns like this :
UPDATE TABLE1 SET (COL1,COL2,COL3) (SELECT COL1,COL2,COL3 FROM TABLE2)
::EDIT::
I tried this :
SELECT A.ID_DIVISI,A.ID_UNIT_KERJA,A.ID_JABATAN,
DETAIL.ID_DIVISI X,DETAIL.ID_UNIT_KERJA Y,DETAIL.ID_JABATAN Z
FROM SDM_KARYAWAN A
INNER JOIN
(
SELECT ID_KARYAWAN, TO_CHAR(TGL_SK,'YYYYMMDD') URUT,ID_DIVISI,ID_UNIT_KERJA,ID_JABATAN
FROM SDM_KARYAWAN_JABATAN
WHERE ID_KARYAWAN = '0081005'
ORDER BY URUT DESC
)DETAIL
ON DETAIL.ID_KARYAWAN = A.ID_KARYAWAN
WHERE A.ID_KARYAWAN = DETAIL.ID_KARYAWAN
AND ROWNUM = 1
The results :
ID_DIVISI ID_UNIT_KERJA ID_JABATAN X Y Z
-----------------------------------------------------------
D1 D5000 D51000 D2 D200 D2100
Now, i want to update columns on table SDM_KARYAWAN with X,Y,Z value. what's missing from my query? pls.
This will work. Please check.
UPDATE SDM_KARYAWAN
SET (ID_DIVISI,
ID_UNIT_KERJA,
ID_JABATAN) =
(SELECT ID_DIVISI, ID_UNIT_KERJA, ID_JABATAN
FROM ( SELECT TO_CHAR (TGL_SK,'YYYYMMDD')
URUT,
ID_DIVISI,
ID_UNIT_KERJA,
ID_JABATAN
FROM SDM_KARYAWAN_JABATAN
WHERE ID_KARYAWAN = '0081005'
ORDER BY 1 DESC) DETAIL
WHERE ROWNUM = 1)
WHERE ID_KARYAWAN = '0081005'
try this:
UPDATE
(SELECT
Table1.Col1 as OLD1, Table2.Col1 as NEW1
Table1.Col2 as OLD2, Table2.Col2 as NEW2
FROM Table1
INNER JOIN Table2
-- ON Some Join Conditions
) t
SET t.OLD1 = t.NEW1,
SET t.OLD2 = t.NEW2
UPDATE <TABLENAME>
SET STATUS = CASE
WHEN WID = 1 THEN 1
WHEN WID = 2 THEN 0
WHEN WID = 3 THEN 1
END
WHERE WID IN (1,2,3);
NOTE: WHERE COMMAND IS NECESSARY...