The objective of this code is to deduct ringgit value and sen value in respective column and allows the value to be negative.
Assuming the price of item to purchase are RM1.50, and the users' balance are RM 0.80, RM 2.40 and RM -1.00.
The table should look like this :
Before purchase was made
Id
user
ringgit
sen
1
User A
0
80
2
User B
2
40
3
User C
-1
0
After purchase was made.
Id
user
ringgit
sen
1
User A
0
-70
2
User B
0
90
3
User C
-2
-50
I used code below to deduct user's balance. But the code only can deduct correctly if the user's balance is sufficient to purchase the item. (no negative value allowed)
UPDATE users SET ringgit = CASE
WHEN (sen - ".$senToDeduct." < 0) THEN (ringgit - ".$ringgitToDeduct." - 1)
WHEN (sen - ".$senToDeduct." >= 0) THEN (ringgit - ".$ringgitToDeduct.")
END,
sen =
CASE
WHEN (sen - ".$senToDeduct." < 0) THEN (100 - (".$senToDeduct." - sen))
WHEN (sen - ".$senToDeduct." >= 0) THEN (sen - ".$senToDeduct.") END WHERE Id = 1
I try to use code as follows :
UPDATE users SET ringgit =
CASE
WHEN (((ringgit * 100) + sen) - 150 >= 0) THEN LEFT(((ringgit * 100) + sen) - 150,char_length(((ringgit * 100) + sen) - 150)-2)
WHEN (((ringgit * 100) + sen) - 150 < 0) THEN - LEFT(((abs(ringgit) * 100) + abs(sen) - 150),char_length(((abs(ringgit) * 100) + abs(sen) - 250))-2)
END,
sen = CASE
WHEN (((ringgit * 100) + sen) - 150 >= 0) THEN RIGHT(((ringgit * 100) + sen) - 150,2)
WHEN (((ringgit * 100) + sen) - 150 < 0) THEN - RIGHT(((abs(ringgit) * 100) + abs(sen) - 150),2)
END
WHERE Id = 1
..but it does not work as the ringgit column already got affected after the query run SET ringgit =
Related
I wrote a formula using the values of other textboxes in a textbox, but the system does not accept it. The formula is as below
=IIF(ReportItems!Textbox66.Value>=1,IIF(ReportItems!Textbox257.Value>=500,CInt((ReportItems!Textbox66.Value*100-100)/5),IIF(CInt((ReportItems!Textbox66.Value*100-100)/5)>=4,4,CInt((ReportItems!Textbox66.Value*100-100)/5))),IIF(ReportItems!Textbox257.Value<500,IIF(CInt((100-ReportItems!Textbox66.Value*100)/5)>-4,-4,IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))),(CInt((100-ReportItems!Textbox66.Value*100)/5*(-1)))))
It looks like there's an IIF statement with only one of the three arguments.
IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))
It should have an argument for the value when the IIF expression is True of False.
IIF((100-ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
It's easier to find if you use returns and tabs to break up the expression. A few spaces don't hurt either.
=IIF(ReportItems!Textbox66.Value >= 1,
IIF(ReportItems!Textbox257.Value >= 500,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
IIF(CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4,
4,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5)
)
),
IIF(ReportItems!Textbox257.Value<500,
IIF(CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
IIF((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
),
CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)
)
It might be a bit more tedious to use a SWITCH but it may be easier to read and make it work the way you want.
=SWITCH(ReportItems!Textbox66.Value >= 1 AND ReportItems!Textbox257.Value >= 500, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox66.Value >= 1 AND CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4, 4,
ReportItems!Textbox66.Value >= 1, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox257.Value < 500 AND CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
ReportItems!Textbox257.Value < 500 AND (100 - ReportItems!Textbox66.Value * 100) / 5 * (-1) = ?????, ?????,
ReportItems!Textbox257.Value < 500, CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)
I have the following table:
id (integer, primary key)
amount_low (integer)
amount_high (integer)
fixedprice (decimal 4,2 Null)
percentadjust (decimal 4,2 Null)
itemname (varchar 50)
A record will have a value in either the "fixedprice" or "percentadjust" field, but not both. One will be NULL, and the other will have a value.
I need to get records based on a single input amount, "X":
If the "fixedprice" field has a value, I need to get the record if X is >= (fixedprice * amount_low) AND X is <= (fixedprice * amount_high).
If the "percentadjust" field has a value, I need to get the record if X is >= ((((percentadjust / 100) + 1) * 3.5) * amount_low) AND X is <= ((((percentadjust / 100) + 1) * 3.5) * amount_high).
The "3.5" is a value that changes on occasion and I'm not too concerned about that part.
What is a good way to do this in MySQL?
Sample data: (also see http://sqlfiddle.com/#!9/922a0 )
id amount_low amount_high fixedprice percentadjust itemname
-----------------------------------------------------------------
1 20 25 2.25 NULL A
2 50 75 2.38 NULL B
3 23 32 NULL 9.75 C
4 14 22 NULL 9.12 D
5 96 112 2.58 NULL E
Assuming your X was entered as 111 it would be
select * from tblItems
where (fixedprice is not null and 111>=(fixedprice * amount_low) and 111 <= (fixedprice * amount_high) )
OR (percentadjust is not null and 111>=((((percentadjust / 100) + 1) * 3.5) * amount_low) AND 111<=((((percentadjust / 100) + 1) * 3.5) * amount_high))
Note you can always write it as where xyz between A and B to simplify somethings slightly.
Remember that a lot of time can be wasted debugging logic operators when AND and OR are used and safe wrappers with parentheses are not used. So, if you intermingle AND with OR, wrap things well.
Assume a service is billed in the following manner:
The first 60 seconds is charged at $1.00
Subsequent charges are billed at $0.25 per 10 second
The following are example computations:
32 seconds = $1.00
59 seconds = $1.00
60 seconds = $1.00
61 seconds = $1.25
69 seconds = $1.25
70 seconds = $1.25
71 seconds = $1.50
Is it possible to do this kind of computation in MySQL alone?
EDIT 1:
Does something like this work:
SELECT `call_length`,
( 1.00 + ( Round(( `call_length` - 30 ) / 10) * .25 ) ) AS `cost`
FROM `service`
SqlFiddleDemo
CREATE TABLE sec(val INT);
INSERT INTO sec
VALUES (32), (59), (60), (61), (69), (70), (71);
SELECT
val,
1.0 + CASE
WHEN val <= 60.0 THEN 0
WHEN val MOD 10 = 0 THEN 0.25 *((val - 60) DIV 10)
ELSE 0.25 * (((val - 60) DIV 10) + 1)
END AS charge
FROM sec;
EDIT:
Without CASE:
SqlFiddleDemo2
SELECT
call_length,
1.0 + IF( call_length <= 60, 0, 0.25 * CEIL((call_length - 60)/10)) AS cost
FROM service;
This is not much of a MySQL problem, unless the setting in which you need to perform the calculation is somehow difficult(?).
UPDATE ... SET cost_cents = 100 + CEIL(GREATEST(0, duration - 60)/10) * 25;
As a SELECT to match your edit,
SELECT `call_length`,
100 + CEIL(GREATEST(0, `call_length` - 60)/10) * 25 AS `cost`
FROM `service`
Note that this returns cents. For dollars, divide the result by 100...
SELECT `call_length`,
(100 + CEIL(GREATEST(0, `call_length` - 60)/10) * 25) / 100 AS `cost`
FROM `service`
I have a MySQL table which looks like this:
id load_transit load_standby hours_transit hours_standby
1 40 20 8 4
2 30 15 10 10
3 50 10 3 9
I need to do the following calculations:
(intermediate calculations)
hours_transit_total = 8+10+3 = 21
hours_standby_total = 4+10+9 = 23
(desired result)
load_transit_weighted_mean = 40*(8/21) + 30*(10/21) + 50*(3/21) = 36.667
load_standby_weighted_mean = 20*(4/23) + 15*(10/23) + 10*(9/23) = 13.913
Is it possible to do this in a single query? What would the best design be?
Note that
40*(8/21) + 30*(10/21) + 50*(3/21) =
(40*8)/21 + (30*10)/21 + (50*3)/21 =
(40*8 + 30*10 + 50*3)/21
and
20*(4/23) + 15*(10/23) + 10*(9/23) =
(20*4)/23 + (15*10)/23 + (10*9)/23 =
(20*4 + 15*10 + 10*9)/23
Which allows you to get the results you want using
SELECT sum(hours_transit * load_transit) / sum(hours_transit),
sum(hours_standby * load_standby) / sum(hours_standby)
FROM your_table
I just had this same question and built this little query I think makes it clear how to find the weighted average in a single query:
select sum(balance), sum(rate * balance / 5200) as weighted_rate, -- what I want
-- what you cannot do: sum(rate * balance / sum(balance))
sum(balance * rate) / sum(balance) as weighted_rate_legit -- ah thank you transitive math properties
from (
select '4600' as balance, '2.05' as rate from dual
union all
select '600' as balance, '2.30' as rate from dual
) an_alias;
Can anyone help me write SQL script for the following formula?
If DEP = 1
If DROP 1
PLV = 334.86 * exp(0.3541 * ACTIVE_DAYS) + 0.25 * DROP + 20 * DEP
Else
If DROP < 0
PLV = DROP + 70 * ACTIVE_DAYS
Else
PLV = 0.25 * DROP + 70 * ACTIVE_DAYS
The SQL script which I have is the following
SELECT IF(dep=1, if(dep=1, (334.86 * exp(0.3541 * act_days)) +
(0.25 * 'drop') + (20 * dep),
if('drop'<0, 'drop' + (70 * act_days), (0.25 * 'drop') + (70 * act_days))),'0')
as PLV
But the above query is not right as something is missing where the formula says
Else
PLV = 0.26 * DROP
Thanks,
How about this:
CASE "DEP"
WHEN 1
THEN CASE
WHEN "DROP" = 1 THEN 334.86 * exp(0.3541 * "ACTIVE_DAYS") + 0.25 * "DROP" + 20 * "DEP"
WHEN "DROP" < 0 THEN "DROP" + 70 * "ACTIVE_DAYS"
ELSE 0.25 * "DROP" + 70 * "ACTIVE_DAYS"
END
END