How to divide fields in Sub query and Sql Server Case Statement - sql

I am getting the error below on execution of query:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
My query is as follows:
CASE
WHEN GL.LDCategory IN ('21069','21070','21051','21073') THEN
CASE
WHEN LDContractType = 'AMORT' THEN (SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE)
WHEN LDContractType = 'BULLET' THEN ISNULL(GL.LDSSAMOUNT,0)
END
END AS LDSSAMOUNT,

The query causing the problem: SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE. It must return one row of data only, yours is returning multiple.
Here there are two problems:
1- The one you stated in your question: Subquery can return more than 1 result. You need to set a "definer", to which data you want. For example: If you table is something like below:
ID Total_prft No_Of_Installment
1 15 3
2 20 5
Then you can do like: SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE WHERE ID = #ID, where #ID is a pre-defined variable (maybe you can get it from your GL table).
Or, as it is recommended by #Suraj Kumar, you can use TOP 1 keyword as well, if it works for you.
2- Another problem here is you are not checking if No_OF_Installment is null or zero, which will cause a division error. Please add a check by adding something like:
WHEN LDContractType = 'AMORT' THEN (
case when NO_OF_INSTALLMENT IS NOT NULL AND NO_OF_INSTALLMENT > 0
THEN SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE
ELSE 0
END)
...

Related

A subquery within a CASE Statement

So I'm trying to put a subquery within a CASE statement. The subquery itself is working fine, but if I put it in another code it can't process. What can I do best to solve?
CASE WHEN dbo.T1.TYPE = 0
THEN dbo.Data.QTY * dbo.Data.SALESPRICE
ELSE
CASE WHEN dbo.T1.TYPE = 1
THEN dbo.Data.QTY *
(
SELECT dbo.Data.ID,
CASE WHEN SUM(dbo.Data.QTY) = 0
THEN SUM(dbo.Data.SALESPRICE)
ELSE SUM(dbo.Data.SALESPRICE) / SUM(dbo.Data.QTY)
END AS REVph
FROM dbo.Data LEFT OUTER JOIN
dbo.T1ON dbo.Data.ID = dbo.T1.ID
WHERE (dbo.T1.TYPE = 1)
GROUP BY dbo.Data.ID
)
ELSE 0
END
END
You are using a subquery in a context where a single value is allowed. Such a subquery is called a scalar subquery.
However, the subquery is returning more than one column. That is not allowed. A scalar subquery can only return one column and at most one row.
Your question is rather unclear on what you want to accomplish, so I can only explain the problem that you are having.

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.

Subquery returning multiple values ... not permitted to do so

declare #week1S DATE = '07/01/2016';
declare #week1E DATE = '07/07/2016';
WITH A as (
SELECT COUNT(ARB_accomplished) as ARB_accomplished_CNT
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name
)
Update ArbitrageResponse
Set arb_count1 = (SELECT ARB_accomplished_CNT FROM A)
I'm trying to update the column in one table based on a count of responses in another table. I'm trying to group on dispgroups and since my import table is massive I'm trying to filter it down to a selected date range. I'm going to have to run this a lot so It would be cool to keep the variables but not neccessary.
I want my response table to be recording responses every week so I just need columns being added to this table which is why I did an update.
I am getting an error
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
I'm not sure if this has to do with my variables as they are being called in my subquery - I know that my subquery is returning the aggregated results of many different rows. Is there a better way to do this?
Try something like this:
set #countaccomplished = SELECT COUNT(ARB_accomplished) as ARB_accomplished_CNT
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name WHERE DISPATCH_GROUP_NAME = SOMETHING
Update ArbitrageResponse
Set arb_count1 = #countaccomplished
OR
WITH A as (
SELECT COUNT(ARB_accomplished) as ARB_accomplished_CNT, dispatch_group_name
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name
)
Update b
Set b.arb_count1 = a.ARB_accomplished_CNT
FROM ArbitrageResponse b where a.dispatch_group_name= b.dispatch_group_name
Without knowing the structure of your table, this is just a guess:
WITH A as (
SELECT dispatch_group_name, COUNT(ARB_accomplished) as ARB_accomplished_CNT
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name
)
UPDATE ar
SET arb_count1 = a.ARB_accomplished_CNT
FROM ArbitrageResponse ar JOIN
A
ON ar.dispatch_group_name = a.dispatch_group_name

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.