updating one table to another sql - 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

Related

Subquery error message while running query

UPDATE rm
SET rm.cost_sqft = ISNULL((SELECT
CASE
WHEN (rm.rm_cat IN ('8','NON-REPORT') AND rm.space_fee = 'No')
THEN '0'
ELSE rent_w.w * #rental_rate
END
FROM
#rental_weight AS rent_w, bl, rm
WHERE
rm.main_contact = rent_w.space_type
AND rm.description = rent_w.space_quality
AND bl.cost_type = rent_w.campuse_cost
AND bl.bl_id = rm.bl_id),'0')
WHERE rm.bl_id = #bl_id
AND rm.fl_id = #fl_id
AND rm.rm_id = #rm_id
While running this statement, I get this error:
Msg 512, Level 16, State 1, Procedure icat_rm_cost_sqft, Line 100 [Batch Start Line 2]
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The error means that the subquery (the select) is returning more than one row for a given key to be applied to the update.
Consider the scenario where the update becomes something like this very contrived scenario:
Sample data in a table called Customer:
cust_id cust_type name
1 X Fred
1 Y jack
And the query which will generate the error:
update customer
set name = (
select "MR " || name
from customer
where cust_id = 1 /* returns Fred and Jack */
)
where cust_id = 1 and cust_type = 'X';
What name should customer 1X be updated to? "Mr Fred" or "Mr Jack"? the RDBMS can't decide for you, you need to modify the query as a correlated subquery (add a join condition on cust_type) or a subquery that just returns one row.
Granted the above is very contrived, but that is what the error is telling you.

SQL GROUP BY function returning incorrect SUM amount

I've been working on this problem, researching what I could be doing wrong but I can't seem to find an answer or fault in the code that I've written. I'm currently extracting data from a MS SQL Server database, with a WHERE clause successfully filtering the results to what I want. I get roughly 4 rows per employee, and want to add together a value column. The moment I add the GROUP BY clause against the employee ID, and put a SUM against the value, I'm getting a number that is completely wrong. I suspect the SQL code is ignoring my WHERE clause.
Below is a small selection of data:
hr_empl_code hr_doll_paid
1 20.5
1 51.25
1 102.49
1 560
I expect that a GROUP BY and SUM clause would give me the value of 734.24. The value I'm given is 211461.12. Through troubleshooting, I added a COUNT(*) column to my query to work out how many lines it's running against, and it's giving a result of 1152, furthering reinforces my belief that it's ignoring my WHERE clause.
My SQL code is as below. Most of it has been generated by the front-end application that I'm running it from, so there is some additional code in there that I believe does assist the query.
SELECT DISTINCT
T000.hr_empl_code,
SUM(T175.hr_doll_paid)
FROM
hrtempnm T000,
qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE 1 = 1
AND T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
AND (T175.hr_paym_type = 'd' OR T175.hr_paym_type = 't')
GROUP BY T000.hr_empl_code
ORDER BY hr_empl_code
I'm really lost where it could be going wrong. I have stripped out the additional WHERE AND and brought it down to just T166.hr_empl_code = T175.hr_empl_code, but it doesn't make a different.
By no means am I any expert in SQL Server and queries, but I have decent grasp on the technology. Any help would be very appreciated!
Group by is not wrong, how you are using it is wrong.
SELECT
T000.hr_empl_code,
T.totpaid
FROM
hrtempnm T000
inner join (SELECT
hr_empl_code,
SUM(hr_doll_paid) as totPaid
FROM
hrtpaytp T175
where hr_paym_type = 'd' OR hr_paym_type = 't'
GROUP BY hr_empl_code
) T on t.hr_empl_code = T000.hr_empl_code
where exists
(select * from qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
)
ORDER BY hr_empl_code
Note: It would be more clear if you have used joins instead of old style joining with where.

Update table with condition matching

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

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

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.