Scalar subqueries produced more than one element - sql

I know this error has been produced many time and a lot of answers came by but I believe every situation might be unique.
So i am trying to get a deficit value(imports - exports) from a table. Both values are on one column
value account
100 export
200 import
SO now i need to calculate the deficit or surplus, which is either import-export or export-import. I tried scalar subqueries but i am always getting this error.
SELECT label, product_type,status,((select value from Task2.quarterly_report where account="Imports") - (select value from Task2.quarterly_report where account="Exports")) As trade_deficit
so basically i am trying to get a table with:-
label product_type status trade_deficit
Can anyone explain to me the issue and why is it happening and how to solve it.
Thanks in advance

You can use the conditional aggregation:
select sum(case when account = 'import' then value
when account = 'export' then - value
end)
from t;
This is based on the question and sample data. I don't see what your query has to do with the rest of the question.

Related

Stuck on column was specified multiple times...can't find the workaround for no alias on WHERE

So I have the following query that returns "The column 'ReceiverNo' was specified multiple times for location_4" error. I'm not an SQL expert but I know you cannot use an alias in a WHERE clause but I need to ensure those 2 similarly named columns match values in order to update the correct Estim row. Is there a work around for this that anyone is aware of? I know this has been asked but I found nothing quite this similar...
With location_4 as (
SELECT TOP (1) ReceiverDET.ReceiverNo, ReceiverDET.PartNo, Estim.User_Text4,
Receiver.ReceiverNo, Receiver.ReceiveDate
FROM Estim, ReceiverDET, Receiver
WHERE Receiver.ReceiverNo = ReceiverDET.ReceiverNo
AND Estim.PartNo = ReceiverDet.PartNo
ORDER BY Receiver.ReceiveDate DESC
)
UPDATE location_4
SET User_Text4 ='Not in Location'
WHERE User_Text4 = ''
If the above columns from the different tables do not match values it does not work, and there has to be a way... I've tried 100 different ways so far. When I remove the update statement it works fine and displays the proper row every time, it just breaks trying to actually use update. Any help would be greatly appreciated, thank you.

Looking for guidance on my sql query that apparently includes an array

Quite new to sql, and looking for help on what i'm doing wrong.
With the code below, i'm getting the error "cannot access field value on a value with type array<struct> at [1:30]"
The "audience size value" comes from the dataset public_campaigns where as the engagement rate comes from the data set public_instagram_channels
I think the dataset that's causing the issue here is the public_campaigns.
thanks in advance for your help!
SELECT creator_audience_size.value, AVG(engagement_rate/1000000) AS avgER
FROM `public_instagram_channels` AS pic
JOIN `public_campaigns`AS pc
ON pic.id=pc.id
GROUP BY creator_audience_size.value
This is to do with the type of one of the columns using REPEATED mode.
In Google BigQuery you have to use UNNEST on these repeated columns to get their individual values in the result set.
It's unclear from what you've posted which column is the repeated type - looking at the table definition for public_instagram_channels and public_campaigns will reveal this - look for the word REPEATED in the Mode column of the table definition.
Once you've found it, include UNNEST in your query, as per this untested example:
SELECT creator_audience_size.value, AVG(engagement_rate/1000000) AS avgER
FROM `public_instagram_channels` AS pic,
UNNEST(`column_name`) AS whatever_you_want
JOIN `public_campaigns`AS pc ON pic.id = pc.id
GROUP BY creator_audience_size.value

Quick one on Big Query SQL-Ecommerce Data

I am trying to replicate the Google Analyitcs data in Big Query but couldnt do that.
Basically I am using Custom Dimension 40 (user subscription status)
but I am getting wrong numbers in BQ.
Can someone help me on this?
I am using this query but couldn't find it out the exact one.
SELECT
(SELECT value FROM hits.customDimensions where index=40) AS UserStatus,
COUNT(hits.transaction.transactionId) AS Unique_Purchases
FROM
`xxxxxxxxxxxxx.ga_sessions_2020*` AS GA, --new rollup
UNNEST(GA.hits) AS hits
WHERE
(SELECT value FROM hits.customDimensions where index=40) IN ("xx001","xxx002")
GROUP BY 1
I am getting this from big query which is wrong.
I have check out the dates also but dont know why its wrong.
Your question is rather unclear. But because you want something to be unique and numbers are mysteriously not what you want, I would suggest using COUNT(DISTINCT):
COUNT(DISTINCT hits.transaction.transactionId) AS Unique_Purchases
As far as I understand, you imported Google Analytics data into Bigquery and you are trying to group the custom dimension with index 40 and values ("xx001","xxx002") in order to know how many hit transactions were performed in function of these dimension values.
Replicating your scenario and trying to execute the query you posted, I got the following error.
However, I created a query that could help with your use-case. At first, it selects the transactionId and dimension values with the transactionId different from null and with index value equal to 40, then the grouping is done by the dimension value, filtered with values equals to "xx001"&"xxx002".
WITH tx AS (
SELECT
HIT.transaction.transactionId,
CD.value
FROM
`xxxxxxxxxxxxx.ga_sessions_2020*` AS GA,
UNNEST(GA.hits) AS HIT,
UNNEST(HIT.customDimensions) AS CD
WHERE
HIT.transaction.transactionId IS NOT NULL
AND
CD.index = 40
)
SELECT tx.value AS UserStatus, count(tx.transactionId) AS Unique_Purchases
FROM tx
WHERE tx.value IN ("xx001","xx002")
GROUP BY tx.value
For further details about the format and schema of the data that is imported into BigQuery, I found this document.

Query to update values in a table with averages from the same table

I'm looking for a bit of support regarding using a value from a separate query in an update query. The background is that i have a query calle qry_AvgOfXCoeff which calculates the average of tbl_ConvertToDouble.XCoeff. What i would like to do is replace any Xcoeff value that is greater than 0 with the avg calculated in the first query. At present i cannot use the qry directly in an Update query as i received the dreaded 'Must use a updateable query' error.
qry_AvgOfXCoeff:
SELECT Avg(tbl_ConvertToDouble.XCoeff) AS [Avg]
FROM tbl_ConvertToDouble;
Now i've been informed that i should be able to do this by using an IN condition in the update query, but im really stumped with this one and cannot seem to find any examples of how i would implement this. I've had a play with some code as per below, but please can someone help with this. It seems such a simple thing.
UPDATE qry_AvgOfXCoeff, tbl_ConvertToDouble SET tbl_ConvertToDouble.[Xcoeff]
WHERE (( ( tbl_ConvertToDouble.[xcoeff] ) IN (SELECT [qry_AvgOfCoeff].[Avg]
FROM [qry_AvgOfCoeff] AS Tmp
Where [tbl_ConvertToDouble].[Xcoeff] > 0) ))
ORDER BY tbl_calcreg.[xcoeff];
Thank you kindly in advance.
Donna
Access offers Domain Aggregate Functions that can be helpful in avoiding the "Operation must use an updateable query" issue. In this case, you can use the DAvg() function
UPDATE tbl_ConvertToDouble
SET XCoeff = DAvg("XCoeff", "tbl_ConvertToDouble")
WHERE XCoeff>0

Oracle SQL find minumum value above a certain threshold

Had a quick google to see if this can be done without much luck, but is there any way in oracle sql to return the minumum value of something but above a certain number (i.e. minimum value above negative numbers). Currently I'm using this line of code
min(ROUND(IA.ASM_START_DATE -REF.ASM_START_DATE,0)) over (partition by IA.ASM_ID) min_wk
To return the lowest difference grouped by ID - it's working to a point, but I want it to bring back the lowest difference above -10. Ideally I'm trying to achieve this in the select rather than using the where query, as I want to use it to identify issues but not exclude them from the report completely.
A simple hack is to use a case statement to set any values that are too low to null so they won't change the minimum:
min(case when ROUND(IA.ASM_START_DATE -REF.ASM_START_DATE,0)<-10 then null else ROUND(IA.ASM_START_DATE -REF.ASM_START_DATE,0) end) over (partition by IA.ASM_ID)