SQL QUERY UPDATE CONDITION - sql

I need to add one condition to this query :
UPDATE o36t_orders s
SET s.bonifico = EXISTS (SELECT 1
FROM mytable d
WHERE d.Descrizione_operazione
LIKE CONCAT('%', s.shipping_number,'%') )
The condition should be to do the update only if s.bonifico != 1

What you probably want is this:
UPDATE o36t_orders s
SET s.bonifico = EXISTS (SELECT 1
FROM mytable d
WHERE d.Descrizione_operazione
LIKE CONCAT('%', s.shipping_number,'%') )
WHERE s.bonifico != 1;

You can use this code for your problem:
//CONNECT DB - FETCH RESULT
$variable=$row['s.bonifico'];
if($variable!='1'){
UPDATE o36t_orders s SET s.bonifico = EXISTS (SELECT 1 FROM mytable d WHERE d.Descrizione_operazione LIKE CONCAT('%', s.shipping_number,'%') )
}

Related

Implements select statement in update clause in SQL

I want to write an update SQL statement, which implements a select statement with join and group by another table.
Something like:
UPDATE coaches *******
here I would like to implement SELECT statement below:
SET coach_level = coach_level +1
This statement works correctly, but I really don't know how to use it in update statement:
SELECT c.first_name,c.coach_level,COUNT(pc.coach_id)
FROM coaches AS c
JOIN players_coaches AS pc
ON c.id = pc.coach_id
GROUP BY pc.coach_id
HAVING COUNT(pc.coach_id) >= 1 AND c.first_name LIKE 'A%';
I want to return the increased value of coach_level by one, which is a column from table coaches.
Like this:
SET coach_level= coach_level +1
Does anybody know how should I do this?
Thanks in advance!
Aggregate in the table players_coaches and join to coaches:
UPDATE coaches AS c
INNER JOIN (
SELECT coach_id
FROM players_coaches
GROUP BY coach_id
HAVING COUNT(*) >= 1
) AS pc ON pc.coach_id = c.id
SET c.coach_level = coach_level + 1
WHERE c.first_name LIKE 'A%'
This is MySql syntax. It can be modified if you use another rdbms.
Use where clause:
update coaches
set coach_level = coach_level + 1
where id in (select pc.coach_id
from player_coaches pc
group by pc.coach_id
having count(*) >= 1
) and
first_name like 'A%';
That said, your subquery is just checking if any row exists for the coachin player_coaches. This can be simplified to:
update coaches
set coach_level = coach_level + 1
where exists (select pc.coach_id
from player_coaches pc
where pc.coach_id = coaches.id
) and
c.first_name like 'A%';

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

SQL MERGE statement with conditional MATCH clause

As you can see in the code below the MERGE statement is exactly the same except based on the IF statement I'm changing the column that will be merged into the target table. Is there a better way of writing this? As the number of #Event's grow I don't want to be copying and pasting more and more of these as its not maintainable.
IF #Event = 1
BEGIN
MERGE INTO SomeTable
USING (
-- Some Query
) AS A ON SomeTable.Id = A.Id
WHEN MATCHED THEN
UPDATE SET SomeTable.Column1 = SomeTable.Column1 + 1;
END
ELSE IF #Event = 2
BEGIN
MERGE INTO SomeTable
USING (
-- Some Query
) AS A ON SomeTable.Id = A.Id
WHEN MATCHED THEN
UPDATE SET SomeTable.Column2 = SomeTable.Column2 + 1;
END
It might be a matter of debate whether one statement is better than two. But, you can combine these:
MERGE INTO SomeTable
USING (
-- Some Query
) AS A ON SomeTable.Id = A.Id
WHEN MATCHED THEN
UPDATE SET SomeTable.Column1 = (CASE WHEN #Event = 1
THEN SomeTable.Column1 + 1
ELSE SomeTable.Column1
END),
SomeTable.Column2 = (CASE WHEN #Event = 2
THEN SomeTable.Column2 + 1
ELSE SomeTable.Column2
END);
You dont really need a Merge statement for this and yes you can write your UPDATE statement a bit more dynamically. something like this.....
UPDATE ST
SET ST.Column1 = CASE WHEN #Event = 1
THEN ST.Column1 + 1
ELSE ST.Column1 END
,ST.Column2 = CASE WHEN #Event = 2
THEN ST.Column2 + 1
ELSE ST.Column2 END
FROM SomeTable ST
INNER JOIN
(
-- Some Query
) AS A
ON ST.Id = A.Id
Also you might want to read Use Caution with SQL Server's MERGE Statement as well though.

Combine a 'Distinct' SQL Query with a single value query

I have an existing sql query that I'd like to apply to every record returned from a "distinct" query.
I guess something like looping through each of the returned records, storing it as a string, and using that value in the other query. How would I go about this?
sudo queries:
Select ...
for each record returned as X,
Select ... etc ... where ... LIKE X
Edit:
not sure how to make it clearer, but I know I'm probably not making it obvious. I'll try:
The distinct will return a single column, with many records. I need to apply each value to the second sql query.
So like.. Select X and Y, but Y is returned from the 2nd query I have, using X
Edit2:
If the distinct select returns
1
2
3
4
And the second query returns a single record "A" when the where clause looks like ... = '1', "B" when the where clause looks like ... = '2', "C" when the where clause looks like ... = '3', and C when the where clause looks like ... = '4'
Then I'd like my final output to look like
1 | A
2 | B
3 | C
4 | C
Edit 3:
first query:
SELECT DISTINCT [user_id] from dbo.sap_empl_subset
second query:
SELECT [name_pref_mixed]
FROM dbo.sap_empl_subset AS E
WHERE E.sap_position_no IN
(SELECT P.sap_position_no
FROM dbo.sap_position AS P
WHERE (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%[VICE ]PRESIDENT%')
OR (LTRIM(RTRIM(P.sap_position_desc)) LIKE 'CHIEF%'))
AND E.sap_org_code =
(SELECT
CASE
WHEN S.sap_org_code_level2 = 0 THEN S.sap_org_code
WHEN S.sap_org_code_level3 = 0 THEN S.sap_org_code_level1
ELSE S.sap_org_code_level2
END
FROM dbo.sap_org_structure AS S
WHERE S.sap_org_code =
(SELECT E1.sap_org_code
FROM dbo.sap_empl_subset AS E1
WHERE E1.[user_id] = '<each item from first query needs applied here>'))
SELECT *
FROM (
SELECT DISTINCT value
FROM mytable
) x
JOIN othertable y
ON y.value LIKE '%' || x.value || '%'
Update:
If you first query is
SELECT my_x
FROM mytable
WHERE my_y = '…'
and the second one is
SELECT other_z
FROM othertable
WHERE other_y = my_x
the you just need a join:
SELECT my_x, other_z
FROM mytable
JOIN othertable
ON other_y = my_x
WHERE my_y = '…'
It would be much more easy to answer if you just posted the queries.
Update 2:
Try this:
SELECT es.user_id, esp.name_pref_mixed
FROM sap_empl_subset es
JOIN sap_org_structure os
ON os.sap_org_code = es.sap_org_code
JOIN sap_empl_subset esс
ON esc.sap_org_code =
CASE
WHEN os.sap_org_code_level2 = 0 THEN os.sap_org_code
WHEN os.sap_org_code_level3 = 0 THEN os.sap_org_code_level1
ELSE os.sap_org_code_level2
END
WHERE esc.sap_position_no IN
(
SELECT sap_position_no
FROM sap_position sp
WHERE (LTRIM(RTRIM(sp.sap_position_desc)) LIKE '%[VICE ]PRESIDENT%')
OR (LTRIM(RTRIM(sp.sap_position_desc)) LIKE 'CHIEF%'))
)
DISTINCT seems to be redundant here. You have a condition in your second query:
WHERE S.sap_org_code =
(
SELECT E1.sap_org_code
FROM dbo.sap_empl_subset AS E1
WHERE E1.[user_id] = '<each item from first query needs applied here>')
)
which would throw an error if there were duplicates on sap_empl_subset.user_id
A join was not necessary to combine the two queries. All I needed was the nested select syntax as shown below, where the first line is the first query, and the first nested select is the second query. A join was not necessary.
SELECT Distinct U.[user_id] AS "User ID", (
SELECT [empl_last_name]
FROM dbo.sap_empl_subset AS E
WHERE E.sap_position_no IN
(SELECT P.sap_position_no
FROM dbo.sap_position AS P
WHERE (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%[VICE ]PRESIDENT%')
OR (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%CHIEF%')
OR (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%[EXECUTIVE ]VP%')
)
AND E.sap_org_code =
(SELECT
CASE
WHEN S.sap_org_code_level2 = 0 THEN S.sap_org_code
WHEN S.sap_org_code_level3 = 0 THEN S.sap_org_code_level1
ELSE S.sap_org_code_level2
END
FROM dbo.sap_org_structure AS S
WHERE S.sap_org_code =
(SELECT E1.sap_org_code
FROM dbo.user_id AS E1
WHERE E1.[user_id] = U.[user_id]))) As "VP"
From dbo.user_id As U WHERE U.[user_id] <> ''
ORDER BY [User ID]

UPDATE row when matching row exists in another table

I need to update a field on a table to be true only if a matching row exists in another table, for all the rows where the column is currently null in the main table.
This is a description of what I want to achieve:
UPDATE [LenqReloaded].[dbo].[Enquiry] A
SET [ResponseLetterSent] = 1
WHERE [ResponseLetterSent] IS NULL
AND EXISTS
(
SELECT * FROM [LenqReloaded].[dbo].[Attachment] B
WHERE A.[EnquiryID] = B.[EnquiryID]
)
This isn't syntactically correct.
I can't code it via an IF EXISTS... statement because I don't have the [EnquiryID] without reading the data from the table.
How should I format my UPDATE statement?
You weren't far off...
UPDATE A
SET A.[ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
WHERE A.[ResponseLetterSent] IS NULL
AND EXISTS ( SELECT * FROM [LenqReloaded].[dbo].[Attachment] B WHERE A.[EnquiryID] = B.[EnquiryID] )
You need to use a join in your update:
UPDATE [LenqReloaded].[dbo].[Enquiry] SET [ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
join [LenqReloaded].[dbo].[Attachment] B on A.[EnquiryID] = B.[EnquiryID]
WHERE A.[ResponseLetterSent] IS NULL
This seems counterintuitive, but you need to establish a table alias in a From clause but use that alias in the Update Clause...
Update E Set
ResponseLetterSent = 1
From LenqReloaded.dbo.Enquiry E
Where ResponseLetterSent Is Null
And Exists (Select * From LenqReloaded.dbo.Attachment
Where EnquiryID = E.EnquiryID)
The thing you are missing is the 'from' clause, which is a t-sql extension - it is the only way to assign an alias to the updated table
update [lenqreloaded].[dbo].[enquiry]
set [responselettersent] = 1
from [lenqreloaded].[dbo].[enquiry] a
where [responselettersent] is null
and exists (
select *
from [lenqreloaded].[dbo].[attachment] b
where a.[enquiryid] = b.[enquiryid]
)