how to update the max 10 values in SQL - sql

I want to update the maximum 10 value by add one to each of those max value. But I don't know how to write the SQL query.
I try my way to create a new table which contains 10 maximum value, and then do the following query below, but got one error: column total can't be null.
update familyone
set familyone.total =
(select totalmax.total-1
from totalmax
inner join (select * from familyone) as t on t.familyone_id2 = totalmax.familyone_id2
where familyone.familyone_id2 = totalmax.familyone_id2)
Can someone point out my error or think of another way to solve it?

;with a as
(
select top 10 total
from familyone
order by total desc
)
update a
set total +=1

Related

Updating all the record for a column in a table with value from another table

Hi I want to update all the values of RequestId column of IIL_CHANGE_REQUEST Table with the RequestId of TABLE_NAME_4MINS Table where REQUESTBY column in both tables are same. I am trying to do this in oracle daatabalse(sql developer)
My query:
Update IIL_CHANGE_REQUEST
set REQUESTID=(SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
WHERE EXISTS (SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
But every time I do this I get an error saying:
Error starting at line : 1 in command -
Update IIL_CHANGE_REQUEST
set REQUESTID=(SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
WHERE EXISTS (SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
Error report -
ORA-01427: single-row subquery returns more than one row
Please anyone help how can I do it.
It depends on what you want to do in such cases. If you don't really care, take any value, for example minimum:
update iil_change_request a
set a.requestid = (select min(b.requestid) --> here
from table_name_4mins b
where a.requestby = b.requestby)
where exists (select c.requestid
from table_name_4mins c
where a.requestby = c.requestby);
If that's not what you want, then you'll have to figure out what to do with those "duplicates". Perhaps you'll have to include yet another WHERE condition, or fix data, or ... who knows? I don't, while you should.
You need to find a condition to narrow down returned rows to the only per REQESTSTBY, for example you can replace ... with a column name in the query below to return just first row as per order-by:
MERGE INTO IIL_CHANGE_REQUEST r
USING (
SELECT *
FROM (
SELECT REQUESTBY, REQUESTID
,row_number()over(partition by REQUESTBY order by ...) rn
FROM TABLE_NAME_4MINS
)
where rn=1
) t
ON (r.REQUESTBY = t.REQUESTBY)
WHEN MATCHED THEN UPDATE
set REQUESTID=t.REQUESTID;

Update table column with count from rows in two tables

I'm updating a count column in one table with the count of corresponding rows in another table like so, which seems to work fine.
UPDATE #Circuits
SET CMLTotal = (SELECT COUNT(#UTCMLs.Drawing)
FROM #UTCMLs
WHERE #UTCMLs.DRAWING = #Circuits.DRAWING)
Now I need to include the rows from another table, #XRAYCMLs to get the total count of corresponding rows from both tables. I'm thinking UNION, but do not know how to make that happen.
How can I update my existing statement to accomplish my goal?
You might just want + and two subqueries:
Update #Circuits
SET CMLTotal = (select COUNT(u.Drawing)
from #UTCMLs u
where u.DRAWING = #Circuits.DRAWING
) +
(select COUNT(#UTCMLs.Drawing)
from #XRAYCMLs x
where x.DRAWING = #Circuits.DRAWING
);

Update all rows affected by a group by

I have the following query using PostgreSql :
SELECT SUM(table.a)
FROM table
GROUP BY table.b
HAVING SUM(table.a) > x;
And now I need to update a column in all rows affected by the precedent query.
I tried the following solution :
UPDATE table
SET c = 'value'
WHERE (SELECT SUM(table.a)
FROM table
GROUP BY table.b) > x;
but I get the following error
More than a line returned by a sub-request used as an expression
I cannot find a solution to update a column in all rows affected by a group by. If anyone can show me the way, it would be greatly appreciated.
You want to update table rows only for those rows that have table.b in the list of values of those whose sums of table.a column values exceeded your defined x
So, I believe you want to use this:
UPDATE TABLE
SET c = 'value'
WHERE b IN (SELECT b
FROM TABLE
GROUP BY table.b
HAVING SUM (table.a) > x));

Fetching and concatenate two rows value from two different table in 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

insert a random number into each row in table

I currently have an oracle table (lovalarm) containing around 600,000 rows. I need to be able to run a query which will cycle through each row and update a field (lovsiteid) to a random number between 14300 and 17300.
So far I have:
update lovalarm
set lovsiteid = (select TRUNC(dbms_random.value(14300,17300)) FROM dual)
Sadly this picks a random number and then updates all rows with the same number which isn't exactly what I'm after!
Can anyone point me in the right direction?
Many thanks,
Cap
Just not use subquery:
update lovalarm
set lovsiteid = TRUNC(dbms_random.value(14300,17300))
Try this:
update lovalarm set lovsiteid = (select FLOOR(RAND() * (17300 - 14300) + 14300))
works in MySQL