Fetching and concatenate two rows value from two different table in SQL - sql

I am unable to Fetch and concatenate two rows value from two different table in SQL.
Please see my query in attached photo.
Following query doesn't providing me the exact data
SELECT RequestNo+'::'+convert(varchar(200),(select count(RID)+1
from BDProjectProposal
Group by RID)) AS Number
FROM BDRequestorInfo
WHERE (RID = #RID)
Is there any way?

You should use a correlated subquery:
SELECT (RequestNo + '::' +
convert(varchar(200),
(select count(RID) + 1
from BDProjectProposal pp
where pp.RID = ri.RID)
)
)
) AS Number
FROM BDRequestorInfo ri
WHERE ri.RID = #RID;

Can you try below
SELECT RequestNo+'::'+convert(varchar(200),isnull((select count(RID)+1
from BDProjectProposal
Group by RID
having RID = #RID),1) AS Number
FROM BDRequestorInfo
WHERE (RID = #RID)
I only added WHERE clause (having RID = #RID) in Subselect to ensure that there will be only one value returning.
Your subselect returns a dataset causing possibly an SQL error
I modified above query and added ISNULL(....,1) for NULL returns with no rows for a given #RID

Related

ERROR: more than one row returned by a subquery used as an expression when updating field

I have a table where for the same ID I have different information. Ex.
ID
Activity
1
12
1
15
2
15
3
20
I want to update the field "Activity", joining all different values of activity for each id as a single row.
When i do the select with the following code is what I want:
SELECT string_agg(id_epigrafe, ', ') AS epigrafe_list
FROM febrero20_2
GROUP BY id_local;
The result being:
However, when I want to introduce that query in my update query, posgresql (version 13), gives me the following error: ERROR: more than one row returned by a subquery used as an expression.
The code I am using to trying to update the field is:
UPDATE febrero20_2
SET id_epigrafe = (SELECT string_agg(id_epigrafe, ', ') AS epigrafe_list
FROM febrero20_2
GROUP BY id_local);
I have tried to create a new table and in that case I can do it correctly with the following code:
CREATE TABLE febrero20_3
AS
SELECT id_local, string_agg(id_epigrafe, ', ') AS epigrafe_list
FROM febrero20
GROUP BY id_local
ORDER BY id_local;
Could anyone help me to understand why am I getting that error? I am just new in posgresql and, therefore, I am sorry if it is just some simple error, but I could not find any answer
You have to join the table you update with the table from which you take the values, so that you don't end up with a subquery that has multiple result rows:
UPDATE febrero20_2 AS f_1
SET id_epigrafe = f_2.epigrafe_list
FROM (SELECT id_local,
string_agg(id_epigrafe, ', ') AS epigrafe_list
FROM febrero20_2
GROUP BY id_local) AS f_2
WHERE f_1.id_local = f_2.id_local;
Let me remark that that seems to be a strange statement: you essentially destroy information. Wouldn't it be better to perform such an aggregation when you query from the table?

Updating a table row multiple values oracle 11g

I m struggling to update one column for a table with a sub query. I have a table where currently one of the values is null.
Currently I have:
UPDATE DW1_PURCHASES SET DW1_PURCHASES.TOTAL_AMT =
(
SELECT DW1_PURCHASES.QUANTITY * DW1_PRODUCTS.PRICE
FROM DW1_PURCHASES, DW1_PRODUCTS
WHERE DW1_PURCHASES.PRODUCT_ID = DW1_PRODUCTS.PRODUCT_ID
)
Although subquery returns data which I need to insert I get a error of single row subquery returns multiple rows.How do I basically shift subquery result to the table?
Thanks.
You don't have to JOINthe update table inside the sub-query. Just correlate the sub-query with update table
UPDATE DW1_PURCHASES
SET DW1_PURCHASES.TOTAL_AMT =
(
SELECT DW1_PURCHASES.QUANTITY * DW1_PRODUCTS.PRICE
FROM DW1_PRODUCTS
WHERE DW1_PURCHASES.PRODUCT_ID = DW1_PRODUCTS.PRODUCT_ID
)
Note : If your DW1_PRODUCTS table has duplicated PRODUCT_ID then even now there is a possibility to get the same error

Subquery returns more than one row and stops working

I have a table which has got a column. I have to fetch the values from the column and modify it in the view before comparing it with an externally supplied value.
For that I am using the following query:
SELECT COUNT(*)
FROM tblMaster WITH (NOLOCK)
WHERE (SELECT test
FROM
(SELECT RIGHT('00000000000000000' + RTRIM(CODE), 17) as test
FROM tblMaster ) t) = '00001231231231231'
Subquery returns modified values of the column extracted from the actual table in form of a column. So I am using the column returned out of the subquery. I don't know if I can use a subquery which returns a column on the left side of equality.
Subquery returns multiple values.
You don't need a subquery for this:
SELECT COUNT(*)
FROM tblMaster WITH (NOLOCK)
WHERE right('00000000000000000' + RTRIM(CODE), 17) = '00001231231231231';
You can simplify this. You don't need to add 0s to integers, SQL will trim them off when comparing. Also you don't need the subquery:
SELECT COUNT(*)
FROM tblMaster WITH (NOLOCK)
WHERE CODE = '1231231231231'

performing an update query with a select subquery returning either the same value for ALL of the records or ora-01427 error

I need to update a column in one table with the results from a select sub-query (and they should ultimately be different). But When I do this, I either get the exact same number for the hundreds of records, or I get the ORA-01427: single row sub-query returns more than one row query. error.
Can you please take a look and see what it is that I am overlooking? (I could just be overlooking something simple for all I know)
UPDATE WD_PRODUCT_CLASS
SET CURRENT_CASES = ( WITH STUFF_COUNT AS
(
SELECT sum(CURRENT_DETAIL.COMBINED_QTY) AS TOTAL_CASES
FROM CURRENT_DETAIL, SKU_MAJORS, WD_PRODUCT_CLASS
WHERE CURRENT_DETAIL.LOC_ID =
&PARM_LOC_ID
AND CURRENT_DETAIL.INVEN_ID = SKU_MAJORS.INVEN_ID
AND WD_PRODUCT_CLASS.CATEGORY = SKU_MAJORS.CONT_DESC
GROUP BY WD_PRODUCT_CLASS.CATEGORY
)
(
SELECT SUM(Z.TOTAL_CASES) FROM STUFF_COUNT Z
)
);
Maybe you need someting like this:
UPDATE WD_PRODUCT_CLASS wpc
SET wpc.CURRENT_CASES = (
SELECT sum(cd.COMBINED_QTY)
FROM CURRENT_DETAIL cd join SKU_MAJORS sm ON cd.INVEN_ID = sm.INVEN_ID
WHERE cd.LOC_ID = &PARM_LOC_ID
AND sm.CONT_DESC = wpc.CATEGORY
)
WHERE 1=1; -- if you don't set a condition all the rows will be updated
Your query updates the table with the same values because you're using a not correlated subquery in the SET clause. This subquery don't depends on the parent query, so it's calculated only once.
I suppose you need a correlated subquery so I changed your update + removed some extra parts.

Update query comparing two tables - Oracle

I am trying to change the sign of amount field in tdataseg table when the account_type in aif_hyp_acct_type table is ' ','R','L','Q'. aif_hyp_acct_type is the master table. It has loadid, account and account_type fields. tdataseg table has account, amount and many other fields.
I tried this query and get ORA-01427 error.
Single row subquery returns more than one row.
update tdataseg
set tdataseg.amount =
(select decode(sign(tdataseg.amount),-1,abs(tdataseg.amount),1,-abs(tdataseg.amount),0)
from tdataseg, aif_hyp_acct_type
where tdataseg.loadid = aif_hyp_acct_type.loadid
and tdataseg.account = aif_hyp_acct_type.account
and aif_hyp_acct_type.account_type in (' ','R','L','Q'))
Presumably the problem is that you think you have a correlated subquery, but you don't. The outer table is mentioned in the inner query. You need to remove that reference:
update tdataseg
set tdataseg.amount = (select decode(sign(tdataseg.amount), -1, abs(tdataseg.amount),
1,-abs(tdataseg.amount), 0)
from aif_hyp_acct_type
where tdataseg.loadid = aif_hyp_acct_type.loadid and
tdataseg.account = aif_hyp_acct_type.account and
aif_hyp_acct_type.account_type in (' ','R','L','Q')
);
EDIT:
You would get that error if there were no matches and the column were declared not null. Here is one fix:
update tdataseg
set tdataseg.amount = (select coalesce(max(decode(sign(tdataseg.amount), -1, abs(tdataseg.amount),
1,-abs(tdataseg.amount), 0)), 0)
from aif_hyp_acct_type
where tdataseg.loadid = aif_hyp_acct_type.loadid and
tdataseg.account = aif_hyp_acct_type.account and
aif_hyp_acct_type.account_type in (' ','R','L','Q')
);
That will set non-matches to 0.
I'd try to solve such update problems with a MERGE statement. I find it a bit more natural to write.
MERGE INTO tdataseg
USING (
SELECT loadid, account
FROM aif_hyp_acct_type
WHERE account_type IN (' ','R','L','Q')
) q
ON (tdataseg.loadid=q.loadid AND tdataseg.account=q.account)
WHEN MATCHED THEN UPDATE SET amount = - ABS(amount);
You put the table you want to change after the MERGE INTO. The master table is subsetted in the USING query. The join condition goes into the ON clause, and the actual data change is specified after WHEN MATCHED THEN UPDATE SET.