MYSQL multiply column value by value in previous row - mysql

I hope I can phrase this properly - I'm not sure I've been approaching this correctly.
I am running a MySQLi query for which I need to order the results by the result of a sum (multiplicaiton) of the values in one column. The values are to 2 decimal places and are 'odds' for sporting results. As Such i can not simply sum the values from each row as the result (e.g. 1,1,1 adds to 3 but multiplies to 1) does not give me 'correct' ordering.
At present i am simply performing a sum in my query
SUM(Fav_odds) AS Total
But i'm stumped how i can get 'Total' to be the result of 'Fav_odds * number of rows' in my query.
Fav_odds | Vendor
------------------
1.2 | Name
2.1 | Name
3.2 | Name
So for Vendor called 'Name' i would like to give a multipled value for items in Fav_odds column (e.g. 1.2 * 2.1 * 3.2 = 8.064)

select round(EXP(SUM(LOG(fav_odds))),3) as fav_odds from table;

use a variable
set #miller := 1;
select orderingTotal from
(select #miller:=#miller*Fav_odds as orderingTotal
from mytable) mytotaltable
order by orderingTotal;
or it could be a matter of just saying
sum(Fav_odds) * count(Fav_odds)
if what you are saying at the end is the right way ("fav_total * the number of rows").

Related

Minus the value based on data using MySQL

I've the following data.
What I need like below
I need to minus order by 1 with 2.
Example : (1-2) and I've display the result in order by 3.
If the branch having order_by as 1 - display as it is.
Using MySQL, how can I get this result?
You can get this result with a UNION query. The first part selects all rows from your table, the second uses a self-join to find branches which have order_by values of both 1 and 2, and subtracts their due values to get the new due value:
SELECT *
FROM data
UNION ALL
SELECT 3, d1.branch, d1.due - d2.due
FROM data d1
JOIN data d2 ON d2.branch = d1.branch AND d2.order_by = 2
WHERE d1.order_by = 1
ORDER BY branch, order_by
Demo on dbfiddle

MySQL match area code only when given the full number

I have a database that lists a few area codes, area code + office codes and some whole numbers and a action. I want it to return a result by the digits given but I am not sure how to accomplish it. I have some MySQL knowledge but its not very deep.
Here is a example:
match | action
_____________________
234 | goto 1
333743 | goto 2
8005551212| goto 3
234843 | goto 4
I need to query the database with a full 10 digit number -
query 8005551212 gives "goto 3"
query 2345551212 gives "goto 1"
query 3337431212 gives "goto 2"
query 2348431212 gives "goto 4"
This would be similar to the LIKE selection, but I need to match against the database value instead of the query value. Matching the full number is easy,
SELECT * FROM database WHERE `match` = 8005551212;
First the number to query will always be 10 digits, so I am not sure how to format the SELECT statement to differentiate the match of 234XXXXXXX and 234843XXXX, as I can only have one match return. Basically if it does not match the 10 digits, then it checks 6 digits, then it will check the 3 digits.
I hope this makes sense, I do not have any other way to format the number and it has to be accomplished with just a single SQL query and return over a ODCB connection in Asterisk.
Try this
SELECT match, action FROM mytable WHERE '8005551212' like concat(match,'%')
The issue is that you will get two rows in one case .. given your data..
SELECT action
FROM mytable
WHERE '8005551212' like concat(match,'%')
order by length(match) desc limit 1
That should get the row that had the most digits matched..
try this:
SELECT * FROM (
SELECT 3 AS score,r.* FROM mytable r WHERE match LIKE CONCAT(SUBSTRING('1234567890',1,3),'%')
UNION ALL
SELECT 6 AS score,r.* FROM mytable r WHERE match LIKE CONCAT(SUBSTRING('1234567890',1,6),'%')
UNION ALL
SELECT 10 AS score,r.* FROM mytable r WHERE match LIKE CONCAT(SUBSTRING('1234567890',1,10),'%')
) AS tmp
ORDER BY score DESC
LIMIT 1;
What ended up working -
SELECT `function`,`destination`
FROM reroute
WHERE `group` = '${ARG2}'
AND `name` = 0
AND '${ARG1}' LIKE concat(`match`,'%')
ORDER BY length(`match`) DESC LIMIT 1

SQL stament groups rows and calculate average

I am stuck with the following issue. I have 1 table that looks like this:
field_number.. Value
````````````````````````````````
1 ......................... 1
2 ..........................1
3 ......................... 2
4 ..........................2
etc.
I want to group different fieldnumbers and have an average for the value column. So the output should be:
field_number................Value
name(1,2)...................... 1.............. ((1+1)/2)
name(3,4)...................... 2.............. ((2+2)/2)
I have checked previous questions but cannot find any question that covers this issue (I might search on the wrong keywords though). So if this has already been covered my appologies, but any help or a point to a previous answer would be appreciated.
** =============UPDATE============= **
I went through your suggestions but did not get it right. So I am trying to be more specific. I almost have the result I want apart from the fact I want to have a fixed value in one of my columns. I have the following query:
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (15, 17, 24) A
UNION
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (16, 108, 18)
etc.
This gives me a table with two columns
wp_rg_lead_detail.value................field_number
4.3 (average)..............................15 (first value of av calculation)
What I want is to change the field number (15 in this case) in a fixed value (text). What and how should I add this to the query?
SELECT avg(value) FROM table WHERE field_number in (1,2)
SELECT avg(value) FROM table WHERE field_number in (3,4)
If your table is really this simple, you can also get away with:
select distinct
Value,
count(Value) as '#'
from table_name
group by Value
If you acctually want to group by a range, than you can put the logic of the range in your grouping clause (see this fiddle)
select distinct
avg(Value) as average,
floor(Value),
count(Value) as '#'
from table_name
group by floor(Value)
In the fiddle I used grouping on whole integers, but you can make that as complex as you like (see, for instance, this example)
If you are actually also interested in your corresponding fields, use group_concat() like so
select
Value,
group_concat(
distinct field_number
order by Value
) as fields
from table_name tn1
group by Value
order by Value
output:
Value | fields
---------------------------------
1 | 1,2
2 | 3,4
See this fiddle implemented from this blog post
For a generalized answer.
SELECT CONCAT('name','(',GROUP_CONCAT(field_number),')') AS field_number,
AVG(Value) as Value
FROM table_name
group by table_name.`Value`
Hope this helps.

MySQL database resultset with values as close to a number "x" as possible

Im trying to get a result set that contains the 10 values that are closest to, in this case, the number 3.
I have a database that has values in a column named rated which can be 1,2,3,4 or 5. What im trying to do is query the database and return the first 10 rows that have the values closest to 3. The values can be above 3 or below 3. I should note that these values in the rated column are floats.
I then need to sort these rows in order so that rows with value of 3 are first and then the row with lowest offset (+/-) from 3.
Is there any SQL query that can return atleast the result set of values closest to 3 ? or am i going to have to return the whole db and sort it myself?
To get the first 10 rows with highest value down i used the statement
SELECT * FROM tabs ORDER BY 5 DESC LIMIT 10";
5 refers to the column rated
Is there some way to modify this to do what i want ?
Thanks
If I understand your problem correctly, this should do the trick:
select *
from tabs
order by abs(`rated` - 3) asc
limit 10
Note that it sorts by the difference in ascending order, so those with a difference of 0 will come first.
SELECT * FROM tabs ORDER BY ABS(3 - Rate) ASC LIMIT 10
If I got right what you need try:
select *
from (
select
case when -(3-rated) > 0 then -(3-rated) else (3-rated) end as distance,
tabs.*
from tabs
) subsel
order by distance
limit 10

What is MySQL equivalent of MS Access LAST()?

I have no idea what the following query means as I do not have any exposure to MS Access. I need to convert it into MySQL and I can't figure out what does the LAST() does
SELECT
Containers.Container_No,
Max(Containers.LastOfContainer_Date),
Max(Containers.LastOfContainer_Time),
Last(Containers.LastOfETD),
Last(Containers.LastOfContainer_Status),
Last(Containers.LastOfPickup_From),
Last(Containers.LastOfPickup_To),
Last(Containers.LastOfConsignee_Name),
Last(Containers.LastOfContract_No),
Last(Containers.LastOfSeaLNo)
FROM
Containers
WHERE
Containers.Customer_Name = 'value here"
AND
Containers.LastOfContainer_Date >='Date here'
AND
Containers.LastOfContainer_Date <= 'Date here'
GROUP BY
Containers.Container_No
Can some one explain me what does this LAST() actually do?
UPDATE
It seems the query can be changed to following:
SELECT
Containers.Container_No,
Max(Containers.LastOfContainer_Date),
Max(Containers.LastOfContainer_Time),
Containers.LastOfETD,
Containers.LastOfContainer_Status,
Containers.LastOfPickup_From,
Containers.LastOfPickup_To,
Containers.LastOfConsignee_Name,
Containers.LastOfContract_No,
Containers.LastOfSeaLNo
FROM
Containers
WHERE
Containers.Customer_Name = '".$customername."'
AND
Containers.LastOfContainer_Date >='".$fromdate."'
AND
Containers.LastOfContainer_Date <= '".$todate."'
GROUP BY
Containers.Container_No
order by
Containers.LastOfETD,
Containers.LastOfContainer_Status,
Containers.LastOfPickup_From,
Containers.LastOfPickup_To,
Containers.LastOfConsignee_Name,
Containers.LastOfContract_No,
Containers.LastOfSeaLNo
The Last function returns the last value on a given query, for instance I assume that in the query you are using it would return the data of the last transaction made by that certain customer,
for instance lets say Mike Jagger bought 2things today, he will have 2 transactions with the same date, if you use the last function you would get the latest information that was inserted.
ID | Date | Product
1 12/12/2011 socks
2 12/12/2011 shirt
select last(product)
from buys;
it would output : shirt
BTW to emulate this behavior you might want to use
ORDER BY product DESC LIMIT 1
According to this page, "The Last function returns the last value from the result set of a query."
You might have to do some kind of ORDER BY x DESC LIMIT 1.