insert a random number into each row in table - sql

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

Related

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
);

PostgreSQL can't update a column with content of another column

checked several threads and some did help but now I'm getting an error:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Not sure what's the exact issue even though read some explanations. Would appreciate if someone could explain with using my code bellow.
I have a table / view called vw_inv_stock_art_global, and it has a column "stock" with a number.
Then also I have a table dis_orderoutdetails with a column "onstock" which needs to copy the "stock" cells based on article_id both tables have in common.
UPDATE dis_orderoutdetails
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id)
WHERE onstock is NULL
AND EXISTS(SELECT stock
FROM vw_inv_stoc_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id);
Additional help if you can be bothered:
I was wondering if there's a possibility to change background colour of a cell just in SQL? Wanted to make an if...else and change colours of a cell depending on the result.
The problem is with this part of the statement:
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id)
The SELECT query returns more than one row => there are more than one row in the vw_inv_stock_art_global view that has this article_id. If the result of a SELECT is to be used as a value there may only be one matching row.
Not sure about Postgres SQL syntax but it seems you would use LIMIT 1 to solve this.
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id LIMIT 1)
However when you use LIMIT you probably need to use an ORDER BY clause as well to make sure that you use the most relevant row, not just any single row that matches the criterion. The most relevant row is most of the times the latest, so if there is some date column that specifies the entry of the row it is a good bet to be the column you need to order on. This is something you need to review according to your needs.
The ORDER BY would be inserted like this:
(Place holder needs to be replaced)
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id
ORDER BY <some column>
LIMIT 1)
You can refactor your SQL without using subqueries, like:
UPDATE dis_orderoutdetails
SET onstock = g.stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id
and onstock is NULL;
But be aware that the correctness of this SQL depends on the relationship between dis_orderoutdetails(article_id) and vw_inv_stock_art_global(article_id): article_id isn't a unique column in vw_inv_stock_art_global, this UPDATE won't be predictable, as each dis_orderoutdetails could be updated more than once, with different stock values.

SQL Delete with group by clause

I want to write a SQL command that will delete all rows with a 0 as the last digit in a column, then a 1 as the last digit, a 2 as the last digit, and so on.
delete from BASE_TABLE where SET = 'ABCD'
This statement would delete 400,000+ rows at once and my service can't handle this.
to break it up I wanted to to say something like
delete from BASE_TABLE
where BASE_TABLE.SET = 'ABCD'
and BASE_TABLE.TAG LIKE '%0'
After I delete everything with LIKE '%0' I would want to delete everything with LIKE '%1', all the way through LIKE '%9'
Is this possible?
You can do that in a while loop. However, more typical approach would be:
delete top 10 percent from BASE_TABLE
where BASE_TABLE.SET = 'ABCD';
Or, just a fixed number that you can handle:
delete top 1000 from BASE_TABLE
where BASE_TABLE.SET = 'ABCD';
You can then put this in a loop:
declare #x int;
set #x = 1;
while #x > 0 begin
delete top 1000 from BASE_TABLE
where BASE_TABLE.SET = 'ABCD';
set #x = ##ROWCOUNT;
end;
Don't put the percent method in the while loop. It will keep going for a long time -- because the percent is based on the rows in the table at the time. I believe it will eventually delete the last row, but there will be a lot of iterations.
If system load is all you care about, just delete the top X rows. Here's the syntax
DELETE TOP (top_value) [ PERCENT ]
FROM table
[WHERE conditions];
You could apply an order by if you want to delete in a certain order. If you want the process to do all the work for you, stick it in a loop until the table is empty.
You need [0-9]% in where clause
This will delete all the rows begining with 0 to 9 and if you want ending with shift the % to the left as
%[0-9]

SQL Replace entire row with value

So I have a row in a table which contains 300 randomly generated hashes. I would like to replace all of them with one specific hash. How could I write a query that replaces every value in said table with my specific hash? Right now my query looks like:
SELECT TOP 1000 [Hash]
FROM [x].[y].[z]
X/Y/Z are different in my query obviously. However I do not know how I can then replace every value in the top 1000 Hashes with my specific hash.
UPDATE [x].[y].[z]
SET Hash = 'OneHashToRuleThemAll'
No WHERE condition will update the entire table. Make sure this is what you want.
Use self inner join to update just the top 1000, like below. Note that I replaced TOP with LIMIT since we are talking about MySQL
UPDATE [x].[y].[z] XYZ
INNER JOIN
(
select SOME_KEY_FROM_XYZ from [x].[y].[z] LIMIT 1000
) ZYX ON XYZ.SOME_KEY_FROM_XYZ = ZYX.SOME_KEY_FROM_XYZ
SET Hash = 'OneSpecificHash'

how to update the max 10 values in 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