Named calculation subquery issue - sql

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.

Related

Subquery returned more than 1 value - STIntersects

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.

Executing subquery to insert data on another table

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.

SqlSyntaxErrorException in DB2 query

I am trying to execute the following db2 query, but I'm getting this error:
SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-119, SQLSTATE=42803, SQLERRMC=ENTITLEMENT
The query is:
SELECT *
FROM reclaimbalance rb
,user_benefit_accrued_period ubap
WHERE rb.user_id = ubap.user_id
AND rb.component_id = ubap.PAY_HEAD_ID
AND ubap.CUSTOMER_ID = 281
AND rb.year = '2016-2017'
AND ubap.STATUS = 1
GROUP BY ubap.user_id
,ubap.PAY_HEAD_ID
HAVING sum(ubap.STD_BALANCE_ADDED_IN_PERIOD) != rb.ENTITLEMENT
One problem is SELECT *. I would expect the error to be a bit different from just a generic syntax error, though.
Also, you should learn to use explicit JOIN syntax. And, your HAVING clause has an unaggregated column.
I assume you want something like this:
SELECT ubap.user_id, ubap.PAY_HEAD_ID, rb.ENTITLEMENT,
sum(ubap.STD_BALANCE_ADDED_IN_PERIOD)
FROM reclaimbalance rb JOIN
user_benefit_accrued_period ubap
ON rb.user_id = ubap.user_id AND rb.component_id = ubap.PAY_HEAD_ID
WHERE ubap.CUSTOMER_ID = 281 AND rb.year = '2016-2017' AND ubap.STATUS = 1
GROUP BY ubap.user_id, ubap.PAY_HEAD_ID, rb.ENTITLEMENT
HAVING sum(ubap.STD_BALANCE_ADDED_IN_PERIOD) <> rb.ENTITLEMENT;
The problem diagnosed by the sqlcode=-119 is that the column "ENTITLEMENT" specified in the HAVING clause is neither coded on that HAVING clause within an aggregate function nor is the column "ENTITLEMENT" specified in the GROUP BY clause.
Recovery is by either removing the non-aggregate reference to the column "ENTITLEMENT" from the HAVING clause, changing the reference to the column "ENTITLEMENT" to be inside of an aggregate function, or adding the column "ENTITLEMENT" to the GROUP BY clause.
Noting however, that despite being valid recovery, the effect may not be what is required. And even after that sqlcode=-119 problem is resolved according to any one of the possible recovery actions noted above, almost surely the next problem will be for a sqlcode-=-122 suggesting that some columns or non-aggregate\non-[effective-]constant expressions included in the SELECT-list are not also named on the GROUP BY clause. FWiW: Despite allusion(s) otherwise, the SELECT * can be compatible with GROUP BY, but just a special-case; as the rules state, the selected column [implicitly selected per the asterisk] must have been specified also, on the GROUP BY.

Updating Variable with Current Row Value

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 ...

Renumber keys in a table using sub-query

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