finding the percentage using case statement - mysql

The following SQL Query :
SELECT
SUM(aol_int) AS AOL,
SUM(android_phone_int) AS Android_Phone,
SUM(androidTablet_int) AS Android_Tablet
FROM mytable;
Is giving me the total values under AOL, Android_Phone and Android_Tablet columns
However, I am trying to get the percentage of AOL, Android_Phone and Android_Tablet and hence I decided to write the above query as follows:
SELECT
SUM(CASE WHEN 'aol_int' THEN 100 END) / Count(aol_int) AS AOL,
SUM(CASE WHEN ‘android_phone_int’ THEN 100 END) /Count(android_phone_int) AS Android_Phone,
SUM(CASE WHEN ‘androidTablet_int’ THEN 100 END)/Count(androidTablet_int) AS Android_Tablet
FROM mytable;
And I am getting NULL in each column. What am I doing wrong here?

If those 3 add up to 100 percent, then you can sumply divide each sum by the total sum to get the percentages. I get the idea that you want to see 20% as "20.00" as opposed to "0.2", so in my example I'll multiple the ration by 100 and round it off at a decimal precision of 2, but it's easy for you to change those as you see fit.
select
round((sum(aol_int) /
(sum(aol_int + android_phone_int + androidtablet_int))) *
100,2) as aol_percentage,
round((sum(android_phone_int) /
(sum(aol_int + android_phone_int + androidtablet_int))) *
100,2) as android_phone_percentage,
round((sum(androidtablet_int) /
(sum(aol_int + android_phone_int + androidtablet_int))) *
100,2) as android_tablet_percentage
from mytable

Related

SQL Calculate accumulated total but filter out certain rows

I have a table that looks like this:
ID
Date
Transition
45
01/Jan/09
1
23
08/Jan/09
1
12
02/Feb/09
1
77
14/Feb/09
0
39
20/Feb/09
1
33
02/Mar/09
1
I would like a query that returns a running total of the "somedate" column, BUT I want to ignore the transitions from where it goes from "0" to "1" or "0" to "0". (IE: the time between ID: 77 and ID: 39 should not be calculated in the running total). The other transitions ("1" to "0", "1" to "1") should counted as usual.
I want my table to look something like this:
ID
Date
Running_Total
45
01/Jan/09
0
23
08/Jan/09
timeDiff(Jan1, Jan8)
12
02/Feb/09
timeDiff(Jan1, Jan8) + timediff(Jan8, Feb2)
77
14/Feb/09
timeDiff(Jan1, Jan8) + timediff(Jan8, Feb2) + timeDiff(Feb2, Feb14)
39
20/Feb/09
timeDiff(Jan1, Jan8) + timediff(Jan8, Feb2) + timeDiff(Feb2, Feb14) + 0
33
02/Mar/09
timeDiff(Jan1, Jan8) + timediff(Jan8, Feb2) + timeDiff(Feb2, Feb14) + 0 + timeDiff(Feb20, Mar2)
Something like that. I know how to calculate the accumulated cost, but I was wondering if there's any way to get the accumulated cost but also filter it out based on "transitions".
I've tried looking online and on other window functions, but not sure how to incoporate the fact of "looking" at future rows and comparing to initial row with window function.
Any help would be greatly appreciated.
I rather like #LajosArpad solution but here is one way of doing it with window functions -
SELECT *, SUM(`Diff`) OVER (ORDER BY `Date` ASC)
FROM (
SELECT
*,
IF (
LAG(`Transition`) OVER (ORDER BY `Date` ASC) = 1,
TIMESTAMPDIFF(DAY, LAG(`Date`) OVER (ORDER BY `Date` ASC), `Date`),
0
) AS `Diff`
FROM temp
) t;
This assumes that Date contains valid dates ('2009-01-01') and not the strings ('01/Jan/09') you have in your example. I have used TIMESTAMPDIFF() instead of TIMEDIFF()
SELECT
SUM(
CASE
WHEN previous.Transition = 1 THEN TIMEDIFF(current.`Date`, previous.`Date)
ELSE 0
END
)
FROM yourtable current
JOIN yourtable previous
ON previous.`Date` < current.`Date`
LEFT JOIN yourtable inbetween
ON previous.Date < inbetween.Date AND inbetween.Date < current.Date
WHERE inbetween.ID IS NULL
The above adds the difference between current and previous to a grand total (in terms of TIMEDIFF) where previous.Transition is 1 and there is no item between previous and current.

How to implement score points for each WHERE clause in SELECT statement

how can i create column with information about how much condition was passed for each field?
eg. I have client who find property with max price to 500000, 3th floor and living area between 45 meters. Now when i use "AND" for each condidtions i will get rows with 100% compatibility. But What abaout to find rows with the same condidtions as before but without living area in range. There will be 66% copatibility because 2/3 of my conditions is passed.
There is my sqlfiddle http://sqlfiddle.com/#!9/1ef60c/5
Simple way to solve your problem is:
SELECT *,
(
CASE WHEN `property_max-price` < 550000 THEN 1 ELSE 0 END
+
CASE WHEN property_additional LIKE "%hot_water%" THEN 1 ELSE 0 END
+
CASE WHEN `property_floor-from` >= 2 AND `property_floor-to` <=5 THEN 1 ELSE 0 END
) / 3 * 100 AS `%`
FROM client

Using SQL to work out % of times a specific event occurrs

Hopefully you can help me, typically I do analysis in R and basic querying in SQL and have tripped myself up over what seems like a basic problem.
I have a basic table: table
ID product % discount (int)
1 a 10
2 a 0
3 a 5
4 b 0
5 b 5
6 b 5
7 c 0
I want to calculate the % of times a discount if applied by product. I have tried a number of approaches all either show incorrect values or error and I am unsure if it's syntax or approach driven.
Examples of erroneous code below, although please let me know if there is an easier way.
I want to calculate the % of times a discount if applied by product. I have tried a number of approaches all either show incorrect values or error and I am unsure if it's syntax or approach driven.
SELECT count(discount) as discountN where discount > 0,
count(discountN) * 100.0 / (select count(product_id) from table) as percent
FROM table
group by ID
with total as select count(*) as total from table
select count(discount) / total
WHERE discount > 0
FROM table
group by ID
I am expecting:
a: 66%
b: 66%
c: 0%
You can try below using case when
SELECT product,
(count(case when discount > 0 then discount end) * 100.0) / count(product_id) as percent
FROM table
group by product
If you want the ratio with discounts greater than 0, then I recommend avg():
SELECT product,
AVG(CASE WHEN discount > 0 THEN 1.0 ELSE 0 END) as ratio
FROM table
GROUP BY product;
If you want values from 0 to 100 rather than 0 to 1, then use 100.0 rather than 1.0.
In MySQL, this can be simplified to:
SELECT product,
AVG( discount > 0 ) as ratio
FROM table
GROUP BY product;

how to fix query mysql with multiple sum

I have a query data from sum function:
ROUND(((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2)/100,2) AS nominal_persentasi,
ROUND((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100)/100,2) AS tambah_persentasi,
ROUND((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))+((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100))/100,2) AS total_penyesuaian
And the results are:
nominal_persentasi | tambah_persentasi | total_penyesuaian
12.000 3.000 1.203.000
The results produced should be 15,000 , why did it happen ?
I tried to sum the variable nominal_persentasi + tambah_persentasi but the result is 0.
You are missing a division by 100 in your total. Hence, instead of adding 12,000 and 3,000 to get 15,000 you were actually adding 12,000,000 and 3,000 to get 12,003,000.
SELECT ROUND(( (nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2)/100,2) AS nominal_persentasi,
ROUND((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100)/100,2) AS tambah_persentasi,
ROUND((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2)/100) + ((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100))/100, 2) AS total_penyesuaian
FROM yourTable -- your query was missing this division by 100 ^^^

How to compute the standard deviation with a "number" column?

I have this table in MySQL :
value number_ads
1 3
2 1
3 1
3 1
4 1
I would like to compute the standard deviation of the column value, but taking into account that the value 1 for example should be counted 3 times.
The result should be :
AVG = 2.1429 STD = 1.124858267715973
I tried with this following request, but I don't have the good result:
SELECT
SUM(value * number_ads) / SUM(number_ads) AS avg,
SQRT((SUM(POW(value, 2)) - POW(2.1429, 2))/SUM(number_ads))
FROM `test`
Calculate the square root of variance. Variance is the difference between mean of (squares of values) and square of mean i.e, Sum(xx)/Count(n) - MeanMean.
SELECT
SUM(value * number_ads) / SUM(number_ads) AS avg,
SQRT((SUM(POW(value ,2) * number_ads)/SUM(number_ads)) - avg * avg)
FROM `test`
Source