Update table with condition matching - sql

Blockquote
I'm trying to mark with an 'X' all the records that match the fields Clave_Rel from table A with numero_cue from table B.
I'm doing something like this :
UPDATE clientes_MP_julio SET MARCA_X
= 'X' WHERE clientes_MP_julio.NUMERO_CUE = (SELECT CLAVE_REL FROM PAGOS A INNER JOIN clientes_MP_julio B ON A.CLAVE_REL =B.numero_cue)
But i'm receiving the message:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
I understand why the message, but I don't know how to solve it! Sorry... I'm new on this! and I really appreciate your help, hoping that someday I can help too.
Thanks!

You shouldn't need the subquery, just a simple inner join. The problem with your syntax is that the = sign expects a single value on the right side, whereas the subquery is returning multiple values.
Try the following:
UPDATE
clientes_MP_julio
SET
c.MARCA_X = 'X'
FROM
clientes_MP_julio AS c
INNER JOIN
PAGOS AS p
ON
p.CLAVE_REL = c.NUMERO_CUE

Related

SQL grouping not working

I am using MS SQL Server 2012
I have a query that uses a subquery to create a column that shows summed percent of assets. However I need that summed column to group by portfoliobasecode as shown below.
I have tried group by and Partition without success. With Group by the result is the portfolio codes correctly group but the summedpct is still the total of all portfolios and not subtotaling as I want.
With Partition I get the following error. I can use Top 1 but this does not give the desired result.
ERROR
Msg 512, Level 16, State 1, Line 17
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Using TOP 1
It may be I am placing group by or partition over in the wrong place in the query. I need a way to correctly group the column summedpct.
Here is the query:
https://dl.dropboxusercontent.com/u/29851290/cashpercent.sql
Here is the result set and desired result.
The problem with the actual result is it is taking the sum total of all PercentAssets and placing them in summedpct.
The result I want is these percent of assets grouped by portfoliobasecode. Note in the desired result set the summedpct of chambetr is 2.66 which is -457.50+460.18
You cannot use an "inline" query because it returns one row for each partition. So, you need a "join" I think. Perhaps this will work:
USE APXFIRM
--1. Establish the APX user session
DECLARE #SessionGUID nvarchar(70)
EXEC APXuser.pSessionInfoSetGuid #SessionGuid
--2. Execute the query against the Appraisal accounting function
DECLARE #ReportData varbinary(max)
EXEC APXUser.pAppraisal
-- Required Parameters. There may be other Optional Parameters.
#ReportData = #ReportData out,
#Portfolios = '#Test_Group',
#Date = '10/02/2013'
--3. Select the columns
SELECT
--Appraisal columns
a.MarketValue,
a.PercentAssets,
--Security Columns
s.SecuritySymbol,
s.SecurityTypeCode,
-- Portfolio Base columns
b.PortfolioBaseCode,
b.ReportHeading1,
bb.summedpct
--4. Join the Appraisal to additional views
FROM APXUser.fAppraisal (#ReportData) a
LEFT JOIN APXUser.vPortfolioBaseSettingEx b
ON b.PortfolioBaseID = a.PortfolioBaseID
LEFT JOIN APXUser.vSecurityVariant s
ON s.SecurityID = a.SecurityID
LEFT JOIN(
SELECT PortfolioBaseCode
, SUM(PercentAssets) as summedpct
FROM APXUser.fAppraisal (#ReportData) aa
LEFT JOIN APXUser.vPortfolioBaseSettingEx b
ON b.PortfolioBaseID = aa.PortfolioBaseID
LEFT JOIN APXUser.vSecurityVariant s
ON s.SecurityID = aa.SecurityID
WHERE s.SecTypeCode LIKe 'ca%'
AND s.SecTypeCode = aa.SecTypeCode
AND s.IsShort = aa.IsShortPosition
GROUP BY PortfolioBaseCode, SecurityTypeCode
) bb
on b.PortfolioBaseCode = bb.PortfolioBaseCode
WHERE s.SecTypeCode LIKe 'ca%'
AND s.SecTypeCode = a.SecTypeCode
AND s.IsShort = a.IsShortPosition
And summedpct >= #summedpct

updating one table to another sql

ok..i'm sure i'm having a brain fart moment..but for some reason i cant get this update to work. I have 2 tables, i'm pretty much trying to copy info from 8 columns to 8 columns in another table. ..
this is what i have so far
update a
set a.denialcharge_cost = b.denial_cost
, a.epd_cost = b.early_default_payment
, a.expeditecharge_cost = b.expeditecharge
, a.duediligence_cost = b.duediligence
, a.deskreview_cost = b.deskreview
, a.servicing_cost = b.servicingcharge
, a.mers_cost = b.merscharge
, a.qcplans_cost = b.qcplans
from orderheader a
inner join clients b
on a.prnt_id = b.id
i get the error
Msg 512, Level 16, State 1, Procedure UpdateOrderHeader, Line 13
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
i have x number of clients in clients..and y number of orders in orderheader..every order can have 1 client..every client can have multiple orders...prnt_id in orderheader has the id in the clients table....any help would be appreciated... i'm trying to copy denial_cost, early_default_payment, expeditecharge, duediligence, deskreview, servicingcharge, merscharge, qcplans
from clients to orderheader
Based on this answer to a previous question, and all of the other troubleshooting that we did, it appears as though you have a trigger getting in the way somewhere. Try disabling it and running the update.
SOMETHING like that should work (with formatting)
update orderheader
set denialcharge_cost = b.denial_cost,
epd_cost = b.early_default_payment,
expeditecharge_cost = b.expeditecharge,
duediligence_cost = b.duediligence,
deskreview_cost = b.deskreview,
servicing_cost = b.servicingcharge,
mers_cost = b.merscharge,
qcplans_cost = b.qcplans
from clients b
where orderheader.prnt_id = clients.id
from what i understand of the error, you are trying do fetch only one result, somewhere and the subquery returns more than a field.
like
select (select * from bb) as count from cc gives an error
because the subquery returns more than a field from the nested query

Update the table with 2 column values

I want to update the table value comparing with two column values.
Query
UPDATE acc SET slloc =
(SELECT Location
FROM Duplication$
WHERE Duplication$.GROUP1 = acc.grpcd
AND acc.ccode = Duplication$.div)
The above query showing error as
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
How can i change my query
Just rewrite your UPDATE to use a JOIN and you can update as many rows as you like...
UPDATE a SET slloc = d.Location
FROM acc a
JOIN Duplication$ d ON d.GROUP1 = a.grpcd
AND d.div = a.ccode
This error message means that there is more than one Location where:
Duplication$.GROUP1 = acc.grpcd AND acc.ccode = Duplication$.div
is true. You need to see when is that possible and if necessary rethink your strategy.

SSMS error: Subquery returned more than 1 value... but there is no subquery

The full error message is this one: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Everything I can find on it just says "yeah, just use TOP 1 and it'll be fine," only I don't even have a subquery or a select statement.
Here's the query:
update f
set f.new_monthnumber = datediff(mm, dateadd(dd, dbo.ufn_GetDaysInMonth(a.new_opendate) - 16, a.new_opendate), f.new_submit_date) + 1
from FilteredAccount a
left outer join FilteredNew_Financials f on f.new_name = a.accountnumber
where f.new_monthnumber is null
ufn_GetDaysInMonth is a user-defined function that analyzes the date passed to it and does not query the database at all, and in any case I've confirmed that it is not the cause of the error by running the query without it.
Anyone know what the heck is going on here?
Given that to write this query I would have swapped the tables and used an inner join, in order to have "FilteredNew_Financials" in both UPDATE and FROM clauses, pheraps there is a 1-to-n cardinality between the two tables on the join condition... maybe you have some dirty data in tables?
Are you sure "new_name" and "accountnumber" are primary and foreign keys (at least logically, if not structurally)?
I would have written the query slightly differenty:
update FilteredNew_Financials
set new_monthnumber = datediff(mm, dateadd(dd, dbo.ufn_GetDaysInMonth(a.new_opendate) - 16, a.new_opendate), f.new_submit_date) + 1
from FilteredAccount a
left outer join FilteredNew_Financials f on f.new_name = a.accountnumber
where f.new_monthnumber is null
Does that work?

Wrong SQLServer syntax

this is what I want to achieve:
4 tables are involved:
Players with PlayerID as PK,
Competitions with CompetID as PK
Results with ResultID as PK and CompetID as FK
And the 4th table: PlayerResultts with ResultID + PlayerID as PK and CompetID as new column I created.
Competitions, results and PlayerResults are already populated and quite large (300000 PlayerResults so far).
In order to populate the PlayerResults.CompetID column, I try a Update ... (Select....) request but I'm not aware of the right syntax and it fails.
Here is my feeble attempt:
update PlayerResults
set competid = (select distinct(r.competid) from results r, playerresults p
where r.resultID = p.resultid)
Error is (of course):
"Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
Can someone put me in the right direction? TIA
You don't need distinct
update PlayerResults
set competid = r.competid
from results r
where r.resultID = PlayerResults.resultid