How do I insert a random value into mysql? - mysql

It looks like RAND is what I need but I'm having a bit of trouble understanding how it works.
I need to insert a random number between 60 and 120 into a couple thousand rows. Table name is: listing and the column name is: hits
Could you please help?

To make a random integer between 60 and 120, you need to do a bit of arithmetic with the results of RAND(), which produces only floating point values:
SELECT FLOOR(60 + RAND() * 61);
So what's going on here:
RAND() will produce a value like 0.847269199. We multiply that by 61, which gives us the value 51.83615194. We add 60, since that's your desired offset above zero (111.83615194). FLOOR() rounds the whole thing down to the nearest whole number. Finally, you have 111.
To do this over a few thousand existing rows:
UPDATE table SET randcolumn = FLOOR(60 + RAND() * 61) WHERE (<some condition if necessary>);
See the MySQL docs on RAND() for more examples.
Note I think I have the arithmetic right, but if you get values of 59 or 121 outside the expected range, change the +60 up or down accordingly.

Here is how to get the random number in a range. The following can bit a bit ambiguous simply because the 61 is actually your max value (120) minus your min value (60) + 1 to get inclusive results.
SELECT FLOOR(60 + (RAND() * 61));
SELECT FLOOR(MIN_Value + (RAND() * (MAX_Value - MIN_Value) + 1);
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand

UPDATE X SET C = FLOOR(61 * RAND() + 60) WHERE ...;
to get a number between 60 and 120 (including 60 and 120);
RAND() creates a number in the interval [0;1) (that is excluding 1). So 61 * RAND() yields a number in [0, 61). 61 * RAND() + 60 is in [60;121) By rounding down you ensure that your number is indeed in [60;120].

When I faced this kind of issue, I tried manual, but I have over 500 lines,
I logically brought a trick which helped me, because if you run RAND on query,
you might end up getting error report due to Duplicates, OR PRIMARY KEY issue, especially if that column is a PRIMARY KEY and AUTO INCREMENT.
Firstly - I renamed the column in question, e.g. mine was ID -> IDS
Secondly - I created another column and Called it ID
Thirdly - I RAN this code
UPDATE history SET id = FLOOR( 217 + RAND( ) *2161 )
This created a random numbers automatically, later i deleted the renamed IDS colume
credit FROM MICHAEL.
Thank you

Related

List [rows] within range of plus or minus the average of a column

I am doing a homework problem: "List each of the clients’ name and weight, for those clients who within 10 pounds of the average weight. (Do NOT use [an integer] as the average weight, you will have to use a function to find the average weight.)
I have tried several things like: "WHERE c_weight BETWEEN avg(c_weight - 10) and avg(c_weight + 10)" to no avail.
My current code compiles:
SELECT c_first, c_last, c_weight
FROM client
WHERE c_weight between 160 and 180;
The code above is just a placeholder; I know correction lies in my WHERE statement, I'm just not sure how to properly create it. Thanks for the help!
I would phrase this as:
SELECT c_first, c_last, c_weight
FROM client
WHERE c_weight - (SELECT AVG(c_weight) FROM client) BETWEEN -10 AND 10;
Note that we use a subquery to find the average weight across the entire table, and then compare each record's weight against that to see if it be within range.
To understand the logic in the WHERE clause, let's that the average weight were 150. Then the range in the WHERE clause is saying:
c_weight - 150 >= -10 AND c_weight - 150 <= 10
This is identical to saying:
c_weight >= 140 AND c_weight <= 160

Replace values in MySQL colums from row x to row y

I'd need to replace post dates from wordpress post table.
There are >800.000 post entries with the same date because of a migration.
How can I replace the date by "from row x to row"?
For example:
row 1 - 10.000 should have date 2013-01-02 09:20:10
row 10.001 - 20.000 should have date 2013-02-05 12:30:21
and so on...
Or maybe replacing by post id?
I know there is a sql query to do this, but I can not remember which one and how to use it correctly.
try adding a LIMIT to the sql to update rows:
UPDATE {table}
SET {datefield} = "{desired date}"
WHERE {datefield} = "{bad date}"
LIMIT 10000;
this will update 10000 rows at a time with a new date as desired, however it's not particularly picky about which ones get updated in which order, generally it will be in the database's internal order which is (roughly) chronological.
is there any other part of the data you can use to determine which records should be updated with which date?
This is not what you asked for, but might be better. You can create distinct timestamps, as if a post has been created every X seconds:
update posts
set created = timestamp('2013-01-02 12:00:00') + interval id * 140 second
where 1=1
http://sqlfiddle.com/#!9/a6c7e0/2
You can even make them look random:
update posts
set created =
timestamp('2013-01-02 12:00:00')
+ interval id * 140 second
+ interval floor(rand()*140) second
where 1=1
http://sqlfiddle.com/#!9/b394c/1

Speed up SQL SELECT with arithmetic and geometric calculations

This is a follow-up to my previous post How to improve wind data SQL query performance.
I have expanded the SQL statement to also perform the first part in the calculation of the average wind direction using circular statistics. This means that I want to calculate the average of the cosines and sines of the wind direction. In my PHP script, I will then perform the second part and calculate the inverse tangent and add 180 or 360 degrees if necessary.
The wind direction is stored in my table as voltages read from the sensor in the field 'dirvolt' so I first need to convert it to radians.
The user can look at historical wind data by stepping backwards using a pagination function, hence the use of LIMIT which values are set dynamically in my PHP script.
My SQL statement currently looks like this:
SELECT ROUND(AVG(speed),1) AS speed_mean, MAX(speed) as speed_max,
MIN(speed) AS speed_min, MAX(dt) AS last_dt,
AVG(SIN(2.04*dirvolt-0.12)) as dir_sin_mean,
AVG(COS(2.04*dirvolt-0.12)) as dir_cos_mean
FROM table
GROUP BY FLOOR(UNIX_TIMESTAMP(dt) / 300)
ORDER BY FLOOR(UNIX_TIMESTAMP(dt) / 300) DESC
LIMIT 0, 72
The query takes about 3-8 seconds to run depending on what value I use to group the data (300 in the code above).
In order for me to learn, is there anything I can do to optimize or improve the SQL statement otherwise?
SHOW CREATE TABLE table;
From that I can see if you already have INDEX(dt) (or equivalent). With that, we can modify the SELECT to be significantly faster.
But first, change the focus from 72*300 seconds worth of readings to datetime ranges, which is 6(?) hours.
Let's look at this query:
SELECT * FROM table
WHERE dt >= '...' - INTERVAL 6 HOUR
AND dt < '...';
The '...' would be the same datetime in both places. Does that run fast enough with the index?
If yes, then let's build the final query using that as a subquery:
SELECT FORMAT(AVG(speed), 1) AS speed_mean,
MAX(speed) as speed_max,
MIN(speed) AS speed_min,
MAX(dt) AS last_dt,
AVG(SIN(2.04*dirvolt-0.12)) as dir_sin_mean,
AVG(COS(2.04*dirvolt-0.12)) as dir_cos_mean
FROM
( SELECT * FROM table
WHERE dt >= '...' - INTERVAL 6 HOUR
AND dt < '...'
) AS x
GROUP BY FLOOR(UNIX_TIMESTAMP(dt) / 300)
ORDER BY FLOOR(UNIX_TIMESTAMP(dt) / 300) DESC;
Explanation: What you had could not use an index, hence had to scan the entire table (which is getting bigger and bigger). My subquery could use an index, hence was much faster. The effort for my outer query was not "too bad" since it worked with only N rows.

Setting A Column to the 100th dec in SQL Query

I have a variance report query here I need the 'Variance' to not have 10 decimal points in the Variance Column. What is the most convenient way to round Variance results to the 100th?
WITH A AS
(
select
A.FACTORY,
A.JOB_NUMBER,
A.PROCESS_STAGE,
A.PART_CODE,
B.PART_DESC_1,
A.INPUT_QTY_STD,
A.QUANTITY_INPUT,
A.QUANTITY_OUTSTANDING,
A.INPUT_QTY_ACTUAL,
(A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)) as variance,
A.ACTUAL_CLOSE_DATE
from
(select * from [man_prod].[dbo].[JOB_STAGE_LINES]
where JOB_NUMBER in (select JOB_NUMBER from JOB_OUTPUTS where
BF_QTY_ACTUAL<>0
and ABS(DATEDIFF(HOUR,ACTUAL_CLOSE_DATE,GETDATE())) < 12 and STATUS_FLAG='C'
)) A
join fin_prod.dbo.PRODUCT_MASTER B
ON A.PART_CODE=B.PART_CODE
WHERE
A.INPUT_QTY_STD<>0 and
A.QUANTITY_OUTSTANDING <>0
)
SELECT * FROM A WHERE A.variance >10.000000 OR A.variance <-10
order by PROCESS_STAGE asc ,PART_CODE asc, variance desc ;
The Variance column comes out at 00.0000000000 i need it to display 00.000 or 00.000000
Help is greatly appreciated
Use the MySQL ROUND() function, the second argument is the number of decimal places if it is positive.
ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
In this example if the value is 0.0000000000 it would be rounded to 3 decimal places, or 0.000.
You can use the TRUNCATE option:
TRUNCATE((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
or use the ROUND if you are looking for rounding(as suggested by doublesharp)
ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
Using Convert to convert it to a decimal of the desired length is what i prefer when i am not actually rounding the value, just formatting.
CONVERT(DECIMAL(10,3),10000)

MySQL select the nearest lower value in table

I have an SQL table that stores running times and a score associated with each time on the table.
/////////////////////
/ Time * Score /
/ 1531 * 64 /
/ 1537 * 63 /
/ 1543 * 61 /
/ 1549 * 60 /
/////////////////////
This is an example of 4 rows in the table. My question is how do I select the nearest lowest time.
EXAMPLE: If someone records a time of 1548 I want to return the score for 1543 (not 1549) which is 61.
Is there an SQL query I can use to do this thank you.
Use SQL's WHERE clause to filter the records, its ORDER BY clause to sort them and LIMIT (in MySQL) to obtain only the first result:
SELECT Score
FROM my_table
WHERE Time <= 1548
ORDER BY Time DESC
LIMIT 1
See it on sqlfiddle.