I am getting this error while executing below query:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
This is the query I am using:
UPDATE BatchRecords SET Fees = (
SELECT
(Fees/NoOfPayments) as AvgTotal
FROM BatchFx
WHERE BatchRecords.BatchId = BatchFx.BatchId
AND BatchFx.CreatedOn < '2019-05-04'
);
Your error is happening because your WHERE clause is returning more than 1 record. So the update statement is confused which row does it need to update (because you most probably have more than 1 record per ID in the BatchFX table). I replicated your issue in this link and got the same output.
So to solve this issue you need to use an aggregator to group all the rows and output one record from the subquery.
UPDATE BatchRecords
SET Fees = (
SELECT
AVG(Fees / NoOfPayments) as AvgTotal
FROM BatchFx
WHERE BatchRecords.BatchId = BatchFx.BatchId
AND BatchFx.CreatedOn < '2019-05-04'
);
Hope this helps :)
The alias AvgTotal seems to imply that you want to take an average of something, so why not try doing that:
UPDATE BatchRecords br
SET Fees = (SELECT AVG(Fees/NoOfPayments) AS AvgTotal
FROM BatchFx bf
WHERE br.BatchId = bf.BatchId AND bf.CreatedOn < '2019-05-04');
Note that the error message you are seeing implies that the subquery in some cases would return more than one record. Selecting an aggregate function would be one way to get around this problem.
Related
I am facing ORA-01427: single-row subquery returns more than one row error in one of my report. The report was build by previous vendor and it is a big SQL which seems like was generated by some tool and used in the report. I am having a tough time to figure out what's wrong. All the sub queries are either using aggregate functions or ROWNUM = 1 to return 1 record.
Please see if you can give me any pointers to which part I need to focus on. I checked the log files generated by the BIP but it just narrowed down it to the dataset.
Due to large size of the file, I provide the SQL in this file:
SQL
You'll have to scan the query line by line and check all subqueries that do not select an aggregated function (e.g. MIN(..)) and don't contain the ROWNUM = 1 limitation.
Example
(
SELECT
( ko.extn_attribute_timestamp006 )
FROM
moo_ref_entities ko
WHERE
ko.extn_attribute_number001 = lea.id
AND ko.attribute_category = 'RenewalAndOtherOptions_c'
AND nvl(ko.extn_attribute_timestamp003, sysdate) >= sysdate
AND ko.extn_attribute_timestamp005 = (
SELECT
MIN(ko.extn_attribute_timestamp005)
FROM
moo_ref_entities ko
WHERE
ko.extn_attribute_number001 = lea.id
AND ko.attribute_category = 'RenewalAndOtherOptions_c'
AND nvl(ko.extn_attribute_timestamp003, sysdate) >= sysdate
)
) AS option1_exc,
This is a typical trap as you constrain the extn_attribute_timestamp005 with a MIN subquery, but if you have ties in the timestamps you get exact the error you observe.
Here's a SELECT nested in a SELECT list that isn't guaranteed to yield just one row.
(
SELECT FL.lookup_code
FROM fnd_lookups FL
,hz_organization_profiles HO1
WHERE ho1.party_id = LEA.extn_attribute_number010
AND FL.lookup_code = HO1.extn_attribute_char035
AND FL.lookup_type = 'FND_SALES_CATEGORY'
) AS Sales_Category
It wouldn't have been hard for somebody to INSERT something to that fnd_lookups table to make that subquery yield more than one row. You could go for AND ROWNUM=1 or SELECT MAX(FL.lookup_code) lookup_code to see if it helps.
I have three tables
2x tables of observational point data with a point geom column (for different time periods)
A table of hexbins across the study area, with a polygon geom column
Both are in the same coordinate system.
The points tables are always intersecting the hexbin table. i.e. no points outside the hexbin layer.
Running the following query:
UPDATE OBS_MONDAY
SET GRID_ID = (
SELECT GRID_ID
FROM SYDHEX s with (index(FDO_Shape))
WHERE (OBS_MONDAY.Shape.STIntersects(s.Shape) = 1))
This executes fine, and calculates the GRID ID field from the Hexbin layer into a column in the Point layer.
However, running this same query against the 2nd point table causes 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.
Can someone help me pinpoint the problem here please.
You have to decide what to do. The "work-around" is to use SELECT TOP (1) or aggregation:
UPDATE OBS_MONDAY
SET GRID_ID = (SELECT TOP (1) GRID_ID
FROM SYDHEX s with (index(FDO_Shape))
WHERE OBS_MONDAY.Shape.STIntersects(s.Shape) = 1
);
By the way, this has nothing to do with spatial data. You are trying to assign a single value to GRID_ID and your subquery is returning more than one value.
I'm facing a little issue with SSAS, I'm trying to add a column to my fact table «Fact_Ventes» from a dimension table.
This is my query :
Select dc.[CuNumber]
from [Warehouse].[dbo].[Dim_Clients] dc
inner join [Warehouse].[dbo].[FACT_Ventes] fv on fv.SK_Clients = dc.SK_Clients
and this is the error I got:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Can any one help me on this?
I found the solution here : http://msdn.microsoft.com/en-us/library/ms174859.aspx and I wanna share it, it seems that my query need to be between brackets and I change the inner join to where clause , it works just fine :
(SELECT Dim_Clients.CuNumber
FROM Dim_Clients
where Dim_Clients.SK_Clients = FACT_Ventes.SK_Clients)
The Expression for a Named Calculation should return a Scalar Value. Yours would return a result set.
I would abandon the use of Named Calculations and Named Queries, and access all data via SQL views.
If I understand you correctly you're just trying to update your Fact_Ventes table with a value from another table. A simple UPDATE FROM should do the trick:
UPDATE [Warehouse].[dbo].[FACT_Ventes]
SET fv.CuNumber = dc.[CuNumber]
FROM [Warehouse].[dbo].[FACT_Ventes] fv
INNER JOIN [Warehouse].[dbo].[Dim_Clients] dc ON fv.SK_Clients = dc.SK_Clients
This assumes you've already updated your Fact_Ventes schema to include the new column.
I am trying to perform a complex operation where I pull the sum for an entire column of data and subtract the running subtotal from the sum for each row. I can do the component parts of Sum and Running Subtotal alone. Used this for running subtotal:
sum(UsageMetric) over(order by Nested1.IDNumber) as RunningTotal
However, I get this error when trying to comine them:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used
as an expression.
So I rigged this up, and it returns the 'stand-alone' running subtotal for a given row:
declare #TargetNumber BIGINT
Set #TargetNumber=(select Nested1.IDNumber from TargetTable Nested1 where Nested1.IdNumber='1234567890' and (Extraneous Criteria Here))
select sum(Usage.UsageMetric)
from Table_Usage Usage, IDTable IDT
where IDT.IDNumber <= #TargetNumber
and (Extraneous Criteria Here)
But what I would really like to do is be able to remove the qualifier "Nested1.IDNumber='1234567890' and just perform this for each IDNumber in TargetTable.
If I understand you correctly, you could do the following:
sum(UsageMetric) over () -
sum(UsageMetric) over (order by Nested1.IDNumber) as ...
Although simply reverting the order of rows in the OVER clause would yield the same results, I believe:
sum(UsageMetric) over (order by Nested1.IDNumber DESC) as ...
I have a table that contains a list of deparments. The key ('code' in the sample) is just an assigned number. Unfortunately, this table now needs to accomodate some new software that comes with pre-defined deparment codes. Of course, these defined codes collide with the existing codes. I want to take the existing keys and increase each one by 200 to move them out of the collision space. I tried this query:
update [TEST-PH].[dbo].[Emp_PosDepartments]
set Code = (select code + 200 from [TEST-PH].[dbo].[Emp_PosDepartments] o where o.Code = Code)
But when I try to run this against SQL server, I get the following message:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Any suggestions how to accomplish the task of renumbering the keys?
The correct syntax doesn´t necessarily imply the use of a subquery:
update [TEST-PH].[dbo].[Emp_PosDepartments]
set Code = Code + 200