How to update many rows with random values - sql

I would like to perform update query to table and set 2 columns to random values. Here is an example:
Update Network_Info_Detail set ART = (rand()*4000)+2, NRT = (rand()*1000)+2
It's obvious that all rows will be updated with the same value randomly generated so I needed to create a cycle to generate a random value for each row.
DECLARE #size integer
SET #size = (SELECT Count(*) from Network_Info_Detail)
While #size > 1
BEGIN
Update top (#size) Network_Info_Detail set ART = (rand()*4000)+2, NRT = (rand()*1000)+2
SET #size = #size - 1
END
This script updates rows with different numbers, but it's very slow. Is there a way to improve the execution time?

You can use a SQL Server trick to generate a random number: rand(checksum(newid())).
Update Network_Info_Detail
set ART = (rand(checksum(newid()))*4000)+2,
NRT = (rand(checksum(newid()))*1000)+2;

Related

Improving SQL Query to check condition

Currently, the sql code that I have is to check for the three fee_code records do any of them have election_code = NA.
I'm trying to figure out how to change the code to check that all of the 3 records have election_code = NA.
Original Query:
DECLARE #fee_code1 tinyint = 1
,#fee_code2 tinyint = 2
,#fee_code3 tinyint = 3
,#election_code tinyint = 0
IF EXISTS(SELECT *
FROM Product
WHERE
product_id = #product_id
AND fee_code in (#fee_code1, #fee_code2, #fee_code3)
AND election_code = #election_code)
The solution I have is to do a count check, but I know count has a performance impact and there's probably a better way to do it. What's a better way to do it?
Count solution:
IF ((SELECT COUNT(*)
FROM Product
WHERE
product_id = #product_id
AND fee_code in (#fee_code1, #fee_code2, #fee_code3)
AND election_code = #election_code) = 3)

Update data from the same table for 235 rows

I need help updating rows to equal to another set of rows for the same table for example:
M005E globalpickesequense = 6627,
globalallocationsequense = 7080,
globalputawaysequence = 4268
so these numbers need to equal the same numbers as the M005D 7607,8068,5256.
M006E same thing needs to equal M007D globals.
and so forth...
I have to do this update for a total of 235 rows but in the image I am just adding part of rows in the database.
So if this possible to do? a query that can update all at the same time without updating row by row individually
I have been using this query where it works but I have do one by one changing the ids so that would take me too much time for all my 235 row I need a query that can do the update all at the same time
UPDATE lc1
SET lc1.globalPickSequence = lc2.globalPickSequence,
lc1.globalAllocationSequence = lc2.globalAllocationSequence,
lc1.globalPutAwaySequence = lc2.globalPutAwaySequence
from mytable lc1
JOIN mytable lc2 ON lc1.id = 27234 AND lc2.id = 16358

update column with numbers in sqlite table

This should be very simple but I cannot figure out how to do it. I would like to modify the values of two different columns. One from 1 to the total number of rows and the other one from the total of rows to one (basically increasing and decreasing number). I tried:
start = 0
end = number_of_rows + 1
c.execute('SELECT * FROM tablename')
newresult=c.fetchall()
for row in newresult:
start += 1
end -= 1
t = (start,)
u = (end,)
c.execute("UPDATE tablename SET Z_PK = ?", t) ---> this will transform all rows with Z_PK since there is no where statement to limit
c.execute("UPDATE tablename SET Z_OPT = ?", u)
The thing is that I don't know how I can add the "where" statement since I have no values I am sure for rows (like IDs number). A possibility would be to return the current row as the argument for "where" but I don't know how to do it...
Without an INTEGER PRIMARY KEY column, your table has an internal rowid column, which already contains the values you want:
UPDATE MyTable
SET Z_PK = rowid,
Z_OPT = (SELECT COUNT(*) FROM MyTable) + 1 - rowid

sql - Possible to update two rows with different critera in one query?

This is for SqlCe,
I am trying to update a table and set won +=1 for a winner, and lost =1 for the loser.
I know I can do this with two different update statements but I was wondering if I could make update the winners "won" value at the same time that I update the losers "lost" value.
Basically just looking like this,
UPDATE player SET won = won +1 WHERE id = 0
UPDATE player SET won = lost +1 WHERE id = 1
This isn't pretty but it works
UPDATE player SET won = won + CASE WHEN id = 0 THEN -1 ELSE 1 END WHERE id in (0,1)
I'd personally stick with the two update statements

SQL function to get count of how many times string appears in column?

Is there a function for MySQL that will count the number of times a string occurs in another string or column? Basically I want:
SELECT
SUB_COUNT('my word', `my_column`) AS `match_count`
FROM `table`
Thanks!
EDIT:
I need to know how many times the string appears in a column for each row in a SELECT.
An obvious but unscalable way is like this
(LENGTH(`my_column`) - LENGTH(REPLACE(`my_column`, 'my word', '')))/LENGTH('my word')
Have you investigated Full Text search in MySQL?
I just needed to do something similar, but took a different approach. I copied the relevant string into a temp table where I can add columns to track an index through each occurrence on each row.
In my example, I'm looking for substrings " - " (space-dash-space) in product descriptions, with the intent of eventually chopping those apart to show as bullet points, and I'm analyzing the data like this to see how many "bullets" products typically have.
I suspect this is more efficient than repeatedly re-writing string values, but I haven't actually benchmarked.
SELECT
ccp.ProductID, p.ProductDescription, descrlen=LEN(p.ProductDescription),
bulletcnt=0, indx=0, lastmatchat=0
INTO #DescrBullets
FROM Private.CompositeCatalogProduct AS ccp WITH(NOLOCK)
INNER JOIN Products.Product AS p WITH(NOLOCK) ON p.ProductId = ccp.ProductID
WHERE ccp.CompositeCatalogID=53
DECLARE #rows INT = 1
WHILE #rows>0
BEGIN
-- find the next occurrence on each row that's still in play
UPDATE #DescrBullets
SET lastmatchat = PATINDEX('% - %',RIGHT(ProductDescription,descrlen-indx))
WHERE indx<descrlen
-- anywhere that a match was found, increment my counter, and move my
-- index "cursor" past it
UPDATE #DescrBullets
SET bulletcnt = bulletcnt + 1,
indx = indx + lastmatchat + 2
WHERE lastmatchat>0
SET #rows = ##ROWCOUNT
-- for all the ones that didn't have a match, advance indx past the end
-- so we don't need to reprocess on next iterations
UPDATE #DescrBullets
SET indx=descrlen
WHERE lastmatchat=0
RAISERROR('processing, %d products still have bullets', 0, 1, #rows) WITH NOWAIT
END
SELECT db.bulletcnt, occurs=COUNT(*)
FROM #DescrBullets AS db
GROUP BY db.bulletcnt
ORDER BY 1
I think you may be able to use the following example. I was trying to count the number of times a particular carton type was used when shipping.
SELECT carton_type, COUNT(carton_type) AS match_count
FROM carton_hdr
WHERE whse ='wh1'
GROUP BY "carton_type"
Your scenario:
SELECT my_column COUNT(my_column)
FROM my_table
WHERE my_column = 'my_word'
GROUP BY my_column
If you take out the "where" function, it will count the number of times each distinct entry appears in "my_column".