i think this one is simple but i cant find a good example.
What i have is three columns. StartPrice EndPrice Dividens and results. Each of these is a decimal(10,2) column in MySql. What i'm looking for is (preferably) is an update statement that basically will do Dividens / (AddPrice + EndPrice) and return the decimals.
Example:
Dividens (AddPrice + Endprice) = result
2.09 / (7.52 + 10.52) = .11
7.40 / (1.75 + 1.25) = 2.47
All columns are decimal(10,2)
I'm looking for basically
update tblmath
set result = '<this is what i cant do>
where result = null
Assuming your table is called tblmath and you're trying to update a column called result to contain the result of the calculation, this should work for you:
update tblmath set result = Dividens / (AddPrice + Endprice) where result is null;
BTW: You misspelled "Dividends", but I preserved the misspelling in the SQL above.
Related
I'm currently using PHP and MySQL to retrieve a set of 100,000 records in a table, then iterate over each of those records to do some calculations and then insert the result into another table. I'm wondering if I'd be able to do this in pure SQL and make the query run faster.
Here's what I"m currently using:
$stmt= $pdo->query("
SELECT Well_Permit_Num
, Gas_Quantity
, Gas_Production_Days
FROM DEP_OG_Production_Import
ORDER
BY id ASC
");
foreach ($stmt as $row) {
$data = array('well_id' => $row['Well_Permit_Num'],
'gas_quantity' => $row['Gas_Quantity'],
'gas_days' => $row['Gas_Production_Days'],
'gas_average' => ($row['Gas_Production_Days']);
$updateTot = $pdo->prepare("INSERT INTO DEP_OG_TOTALS
(Well_Permit_Num,
Total_Gas,
Total_Gas_Days,
Total_Gas_Avg)
VALUES (:well_id,
:gas_quantity,
:gas_days,
:gas_average)
ON DUPLICATE KEY UPDATE
Total_Gas = Total_Gas + VALUES(Total_Gas),
Total_Gas_Days = Total_Gas_Days + VALUES(Total_Gas_Days),
Total_Gas_Avg =(Total_Gas + VALUES(Total_Gas)) / (Total_Gas_Days + VALUES(Total_Gas_Days))");
}
I'd like to see if this can be done in pure MySQL instead of having to use PHP just for the fact of using it to hold the variables.
My Result should be 1 record that is a running total for each Well. The source table may house 60-70 records for the same well, but over a few thousand different Wells.
It's a constant import process that has to be run, so it's not like there is a final table which you can just do SUM(Gas_Quantity)... etc.. on
As commented by Uueerdo, you seem to need an INSERT ... SELECT query. The role of such query is to INSERT insert the resultset returned by an inner SELECT. The inner select is an aggregate query that computes the total sum of gas and days for each well.
INSERT INTO DEP_OG_TOTALS (Well_Permit_Num, Total_Gas, Total_Gas_Days, Total_Gas_Avg)
SELECT
t.Well_Permit_Num,
SUM(t.Gas_Quantity) Total_Gas,
SUM(t.Gas_Production_Days) Total_Gas_Days
FROM DEP_OG_Production_Import t
GROUP BY t.Well_Permit_Num
ON DUPLICATE KEY UPDATE
Total_Gas = Total_Gas + t.Total_Gas,
Total_Gas_Days = Total_Gas_Days + t.Total_Gas_Days,
Total_Gas_Avg =(Total_Gas + t.Total_Gas) / (Total_Gas_Days + t.Total_Gas_Days)
Just want to get an idea & advise.. how to get the exact result for my situation when query..see below table..
I try to use this query and its look fine..but the problem is when I give the input ABC1001Z (different only last character Z).. the query still return Honda as result.. it's supposed not return any result/no result found.. any solution for my case?
SELECT Name
FROM CarNo
WHERE ('ABC1001Z' BETWEEN Start AND End)
AND (len('ABC1001Z') = len (Start));
Your kind support is much appreciated..
Update you code to
SELECT Name FROM CarNo
WHERE (SUBSTRING('ABC1001Z',0,7) BETWEEN SUBSTRING(Start,0,7) AND SUBSTRING(End,0,7) )
AND (len('ABC1001Z') = len (Start));
Maybe that's what you are looking for:
SELECT Name FROM CarNo
WHERE (Start = 'ABC1001Z' OR End = 'ABC1001Z')
AND (len('ABC1001Z') = len(Start));
You seem to want the "number" in the middle to be treated as a number. This suggests something like this:
where left('ABC1001Z', 3) between left(start, 3) and left(end, 3) and
substr('ABC1001Z', 4, 4) + 0 between substr(start, 4, 4) + 0 and substr(end, 4, 4) + 0
I'm not sure how the last character relates to a between query of this form, so I'm not addressing that.
How do I correctly replace the first character with 'M'? Suppose you have a PATIENT_ID_NONNUM = 'M001', and we want 1001 as a result.
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),'1' + CONVERT(NVARCHAR(50),PATIENT_ID))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
EDIT:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_MEDICATION]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),CONVERT(NVARCHAR(50),'1') + CONVERT(NVARCHAR(50),SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM))))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
I find STUFF() (an often overlooked function) and LEFT() are a little more readable, but others may disagree:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(STUFF(PATIENT_ID_NONNUM, 1, 1, '1') AS NUMERIC(22,0))
WHERE LEFT(PATIENT_ID_NONNUM, 1) = 'M'
I would suggest something like this:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(('1' + SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM) - 1)) AS NUMERIC(22,0))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
That's going to find all records where the first character is M, and replace the first character with a 1. I haven't tested this, but I believe it should work properly.
I would also suggest not running this type of operation on a production database as a test, which I would assume is what the -PROD stands for in your catalog name.
EDIT: Since it seems important that this query comes out with the PATIENT_ID as a NUMERIC(22,0), I've added the necessary CAST.
I had the following code:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 'not_duplicate'
and DB.T2.DUP = 'not_duplicate'
and DB.T3.DUP = 'not_duplicate'
;
The output returned 0 rows, however. So, I changed the values of the "DUP" column in each table from duplicate/not_duplicate instead to 0/1. I tried this code and it worked:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 1
and DB.T2.DUP = 1
and DB.T3.DUP = 1
;
The second code works perfectly, the first one returned 0 rows. Does anyone know why was this happening? The values "not_duplicate" and "duplicate" were the exact same strings as the csv's that I imported into the database from. I can't explain why this would be the case and I'm really pretty curious.
Thanks very much!
because the DUP column they dont have this not_duplicate in fields. they have 1
then it doesnt match your query .
the query returns values which are stored in the column DUP .
There are two types of records in my Db such as MS-NW and CS in the same column of table DICIPLINE I want to wrap if its CS (ANY TWO STRING LIKE CS,TE OR THE LIKE) then wrap it to BS(CS) (OR BS(TE) ETC) or if its MS-NW (Or MS-CS, MS-TE and the like) then wrap it to MS(NW) from the column dicipline.
I updated for two strings successfully and following is the query for that kindly let me know how can i do it for values like MS-NW OR MS-CS and convert it to the format like MS(NW) from following query .
UPDATE DEG set DICIPLINE = concat("BS(",DICIPLINE,")") where CHAR_LENGTH(DICIPLINE) = 2
The below query helps you to update your data.
update deg set DISIPLINE = if(length(DISIPLINE)= 2,concat('BC(',DISIPLINE,')')
,concat('MS(',substr(DISIPLINE, 4,4),')'));
See Sqlfiddle demo.
For safety, create a temporary column of same type and perform an update like this:
UPDATE deg
SET dicipline_temp = CASE
WHEN CHAR_LENGTH(dicipline) = 2
THEN CONCAT('BS(', dicipline, ')')
WHEN CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-'
THEN CONCAT(REPLACE(dicipline, '-', '('), ')')
END
WHERE CHAR_LENGTH(dicipline) = 2 OR (CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-')
If results are acceptable, update the actual column.