Updating Variable with Current Row Value - sql

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

Related

this Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >=

i wrote a query to get total count of devices for user. but when i run the query for single user it working fine. when i keep mutiple emailids its not working getting message subquery should not contain multiple. I unable to change below query to accept multiple values. Can anyone help on this.
select distinct UserDisplayName,[UserName/Mail],SerialNumber,LastSeen as Lastcheckin,Model, EnrollmentStatus,(select count(*) from Intunedevices where [UserName/Mail] in
('aaa-xyz.onsite-caller#xyz.com','gbidi.i.habab#gsk.com') group by UserDisplayName) as EMSCOUNT
from Intunedevices where [UserName/Mail] in ('aaa-xyz.onsite-caller#xyz.com',
'gbidi.i.habab#xyz.com')
The problem is the group by in your sub-query is returning multiple rows. It should probably be correlated - I'm not completely sure as the in criteria are slightly different, is that a typo in your sample data?
Try the following:
select distinct
UserDisplayName,
[UserName/Mail],
SerialNumber,
LastSeen as Lastcheckin,Model,
EnrollmentStatus,
(select count(*) from Intunedevices d where d.[UserName/Mail] = id.[UserName/Mail] and d.UserDisplayName = id.UserDisplayName) as EMSCOUNT
from Intunedevices id
where id.[UserName/Mail] in ('aaa-xyz.onsite-caller#xyz.com','gbidi.i.habab#xyz.com')
Instead of using DISTINCT, just use a GROUP BY in your outer query, and then you can aggregate any non-grouped columns, eg:
select UserDisplayName,
[UserName/Mail],
SerialNumber,
LastSeen as Lastcheckin,
Model,
EnrollmentStatus,
count(*) EMSCOUNT
from Intunedevices
where [UserName/Mail] in ('aaa-xyz.onsite-caller#xyz.com', 'gbidi.i.habab#xyz.com')
group by UserDisplayName,
[UserName/Mail],
SerialNumber,
LastSeen,
Model,
EnrollmentStatus

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.

Named calculation subquery issue

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.

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