On DUPLICATE KEY, Conditional statement error ambiguous - mysql

INSERT INTO tb1 (title,Family,Complexity)
Select o.title,o.Family,o.Complexity from tb2 o
ON DUPLICATE KEY UPDATE
title = CASE WHEN title <> o.title THEN o.title ELSE title END,
Family = CASE WHEN Family <> o.Family THEN o.Family ELSE Family END,
Complexity = CASE WHEN Complexity <> o.Complexity THEN o.Complexity ELSE Complexity END ;
I am trying to update tb1 with the records in tb2, ONLY if the records do not match up, as I am using an after update trigger in another table.
However when trying to execute this, I get an error "Column 'title' in field list is ambiguous".
I have been unable to solve this. Please assist

Try with full table name
INSERT INTO tb1 (title,Family,Complexity)
Select o.title,o.Family,o.Complexity from tb2 o
ON DUPLICATE KEY UPDATE
tb1.title = CASE WHEN tb1.title <> o.title THEN o.title ELSE tb1.title END,
tb1.Family = CASE WHEN tb1.Family <> o.Family THEN o.Family ELSE tb1.Family END,
tb1.Complexity = CASE WHEN tb1.Complexity <> o.Complexity THEN o.Complexity ELSE tb1.Complexity END ;

This is because the title field exists in multiple tables/references.
You should prefix your title with the table name or it's reference.

Related

SQL Server Update Case When Then statment

I need to update a column [Recipient on contract] in the table [Check_Result]. But the value to insert into the column is not a fixed string but a value from [All_Contracts] table. Thus, all rows to inset into [Recipient on contract] are unique and can be found with the keys [ID Contract] = [Référence]
The link between tables [Check_Result] and [All_Contracts] is [ID Contract] = [Référence]
Update [Check_Result]
set [Recipient on contract] =
If [Bénéficiaire] ="Personne morale" Then
If [Organisme] is not null Then get [Organisme]
Else get [Professionnel de santé]
Else
If [Professionnel de santé] is not null Then get [Professionnel de santé]
Else get [Organisme]
Is It possible to use THEN in such situation? with a Update Case when then (select from inner join where) statement?
Thank you
Yes, each of the values in CASE statement can be replaced with nested select statement surrounded with '(' and ')', i.e. this is a valid statement:
update table1 set
field1 = CASE WHEN field2 = 'some value'
THEN (select field1 from table2 where table1.key_field = table2.key_field)
ELSE 'some default value'
END
You can do the same with IIF statement too
update table1 set
field1 = IIF(field2 = 'some value',
(select field1 from table2 where table1.key_field = table2.key_field),
'some default value')
Your update statement isn't quite clear. Which value you want to get from [All_Contracts] table? It can simplified like this:
Update [Check_Result] set
[Recipient on contract] = IIF([Bénéficiaire] = "Personne morale",
COALESCE([Organisme], [Professionnel de santé]),
COALESCE([Professionnel de santé], [Organisme]))
There you can replace [Professionnel de santé] or [Organisme] with (select SomeField from [All_Contracts] where [All_Contracts].[ID Contract] = [Check_Result].[Référence]).

Subquery returned more than 1 value.4...Different query

Hello I have this query that i am trying to execute and i keep getting this error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.", Kindly help please.
DECLARE #NUMCOUNT BIT
Select #NUMCOUNT = (SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C') ) THEN 1 else 0 END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
)
IF #NUMCOUNT = '1'
begin
UPDATE R5REQUISITIONS
SET R5REQUISITIONS.REQ_STATUS = 'CP'
end
Ok, it sounds like what you actually want to do is update R5REQUISITIONS when there is no RQL_STATUS = 'C' in R5REQUISLINES, since you said you want to count the records where the RQL_STATUS is A and where it's A or C, and then do the update if the counts are the same.. You can greatly simplify this task with the following query:
UPDATE r5
SET r5.REQ_STATUS = 'CP'
FROM R5REQUISITIONS r5
WHERE NOT EXISTS (SELECT 1 FROM R5REQUISLINES r5q WHERE r5q.RQL_REQ = r5.REQ_CODE AND r5q.RQL_STATUS = 'C')
Your 'SELECT CASE' is returning more than 1 record, so it can't be assigned to #NUMBER. Either fix the sub-query to only return the record your looking for or hack it to return only 1 with a 'LIMIT 1' qualification.
I don't know what your data looks like so I can't tell you why your case subquery returns more records than you think it should.
Try running this and see what it returns, that will probably tell you wall you need to know:
SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C')
)
THEN 1
ELSE 0
END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
If there is more than 1 row returned, that's where your problem is.

MySQl query to UPDATE if Data is Null or Empty with a CASE Statement

Iam struck up with a query. When i edit a data in Table A, it needs to check if the same data is present in Table B, If data is present in Table B, just ignore updation, if data is empty or Null, then the data from Table A needs to Update in Table B. With the following query iam almost achieving it but the issue is When the Data is present in Table B, its just deleting the data. It should actually ignore it when data is present. A small issue with the case statement i guess. Pls help me on this.
$strSQLInsert2 = "UPDATE Table B
SET
`tender_intendername` = CASE WHEN `tender_intendername`='' or `tender_intendername` IS NULL
THEN '".$values["intendername1"]."' END,
`no_of_participants` = CASE WHEN `no_of_participants`='' Or `no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."' END
WHERE tender_id=" . $values["tender_id"];
You have to add an ELSE to the CASE expression:
UPDATE Table B
SET `tender_intendername` = CASE
WHEN `tender_intendername`='' or
`tender_intendername` IS NULL
THEN '".$values["intendername1"]."'
ELSE `tender_intendername` END,
`no_of_participants` = CASE
WHEN `no_of_participants`='' Or
`no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."'
ELSE `no_of_participants` END
WHERE tender_id=" . $values["tender_id"];
The ELSE statement sets each field equal to itself, hence it guarantees that the field will not change when the field is not empty and not NULL.
I solved it. But need to write two queries to achieve the result.
$strSQLInsert2 = "UPDATE `TableB`
SET
`tender_intendername` = '".$values["intendername1"]."'
WHERE
`tender_intendername` = ''
OR `tender_intendername` IS NULL AND tender_id=" . $values["tender_id"];

SELECT STATEMENT IN A CASE STEMENT INSIDE A SELECT STATEMENT

I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.

Check which condition failed in complex sql query statement

I want to know which condition from where clause of SQL query fails.
For example I have below statement
SELECT * from Users where status != 'inactive'
AND ptype != 'test' AND ptype != 'test1'
AND (p_name NOT IN ('ptest', 'ptest0', 'ptest1', 'ptest2') OR p_name IS NULL)
AND (p_m_c NOT LIKE '%pmcestt%'
OR (ptype LIKE 'SomeOthertest%' AND p_m_c IS NULL)
OR (TType = 'Broad' AND p_m_c IS NULL)
OR p_m_c IN ('pmctest', 'pmctest0', 'pmctest1', 'pmctest2', 'pmctest3',
'pmctest4', 'pmctest5', 'pmctest6', 'pmctest7', 'pmctest8'))
How can I know which condition failed, and get that condition?
If you want to check conditions you will have to move condition statement in WHERE clause to CASE statement for each condition in SELECT list apart from columns of table Users.
SELECT *,
CASE WHEN status !='inactive' THEN 1 else 0 end as statusTag,
CASE WHEN ptype !='test' THEN 1 ELSE 0 end as ptypeTag,
.
.
CASE WHEN p_m_c IN ('pmctest', 'pmctest0', 'pmctest1', 'pmctest2', 'pmctest3',
'pmctest4', 'pmctest5', 'pmctest6', 'pmctest7', 'pmctest8') THEN 1 else 0 end as p_m_cTag
FROM Users
This will generate list of columns for each table row indicating value 1 if condition is satisfied or 0 if condition is false for that particular row.