I have a database which looks like this :
NUM / CNT
3 / 1
5 / 0
100 / 1
300 / 0
320 / 1
And I am looking for the query that will allow me to sort them by range and make sum of their count so I will have something like this:
NUM / CNT
0-100 / 2
100-400 / 1
I am wondering if this is possible using mysql querys .
Use SUM with conditional aggregation on the value of each NUM:
SELECT '0-100' AS NUM,
SUM(CASE WHEN NUM BETWEEN 0 AND 100 THEN CNT ELSE 0 END) AS CNT
FROM yourTable
UNION
SELECT '100-400' AS NUM,
SUM(CASE WHEN NUM BETWEEN 100 AND 400 THEN CNT ELSE 0 END) AS CNT
FROM yourTable
try this it will work same as you want.
SELECT '0-100' AS NUM,
SUM(CASE WHEN NUM <= 100 THEN CNT ELSE 0 END) AS CNT
FROM tableName
UNION
SELECT '100-400' AS NUM,
SUM(CASE WHEN NUM > 100 THEN CNT ELSE 0 END) AS CNT
FROM tableName
Related
I have a table like this
Now my output would like to be
total_rows | completed | incomplete
------------------------------------
7 2 5
How can I achieve that.
You could use condition aggregation
select count(*) total ,
sum(completed = 1) completed ,
sum(completed = 0) incompleted
from your_table
Try the following.
select
count(*) as total_rows,
sum(case when completed = 1 then 1 else 0 end) as completed,
sum(case when completed = 0 then 1 else 0 end) as incomplete
from myTable
With conditional aggregation:
select
count(*) as total_rows,
sum(completed) completed,
sum(1 - completed) incomplete
from tablename
or:
select
count(*) as total_rows,
sum(completed) completed,
sum(not completed) incomplete
from tablename
I think this will help you
select count(id) as total_rows ,
sum(completed = 1) as completed ,
sum(completed = 0) as incompleted from sales_call_task_jo_iformation;
please try this if you need I will help you
I want to count occurrences of Number 3 from multiple columns with group by Primary Key.
I have a table like this.
And I have tried with this.
But my output is
But expected output is something like this
With this:
select id,
(s1 = 3) + (s2 = 3) + (s3 = 3) + (s4 = 3) + (s5 = 3) valcount
from tablename
Each of the boolean expressions:
s? = 3
evaluates to 0 or 1.
Your query only counts rows with multiple threes one time.
You could use a union:
select id
, sum(case when val = 3 then 1 else 0 end)
from (
select id, s1 as val from t1
union all select id, s2 from t1
union all select id, s3 from t1
union all select id, s4 from t1
union all select id, s5 from t1
) sub
group by
id
Example at db-fiddle.com
Try Below Query..
select id,(count(s1)+count(s2)+count(s3)+count(s4)+count(s5))valcount from(
select id, case when s1=3 then 1 end as s1,
case when s2=3 then 1 end as s2,
case when s3=3 then 1 end as s3,
case when s4=3 then 1 end as s4,
case when s5=3 then 1 end as s5
from test) group by id
and Try another way
select id,
count(decode(s1,3,1))+
count(decode(s2,3,1))+
count(decode(s3,3,1))+
count(decode(s4,3,1))+
count(decode(s5,3,1))valcount
from test
group by id
SELECT id, ( SUM(CASE WHEN s1 =3 THEN 1 ELSE 0 END ) + SUM(CASE WHEN s2 =3 THEN 1 ELSE 0 END ) +
SUM(CASE WHEN s3 =3 THEN 1 ELSE 0 END ) +
SUM(CASE WHEN s4 =3 THEN 1 ELSE 0 END ) +
SUM(CASE WHEN s5 =3 THEN 1 ELSE 0 END ) ) AS val FROM t1 GROUP BY id
I think it will be helpful for you
I am fiddling on SQL fiddle with group by clause and finding the percentage based on the temp table "A" result.
Here is the fiddle.
http://sqlfiddle.com/#!9/faf2f/6959
I did come across that there are different ways of achieving this like by using union all but my question is why this query brings back only one row instead of two rows.
List item
Data:
TotalCost
230
200
100
1254
Query:
SELECT Category, Cnt , Cnt/sum(Cnt) as percent from (
SELECT
Case When TotalCost < 301 Then '0-300'
Else ' > 300' End as Category,
count(*) as cnt
FROM Cars
group by Category
) A
;
Expected Result:
Category Cnt percent
0-300 3 75
> 300 1 25
Actual Result:
Category Cnt percent
> 300 1 25
you can try to group by case when instead of an alias name.
seconde your total count need to do all count so you can do a subquery.
look like this.
SELECT Category, Cnt , Cnt/(select count(*) from Cars) * 100 as percent
from (
SELECT
(Case When TotalCost < 301 Then '0-300'
Else ' > 300' End) as Category,
count(*) as cnt
FROM Cars
GROUP BY (Case When TotalCost < 301 Then '0-300'
Else ' > 300' End)
) A
ORDER BY 3 DESC
or you can use CROSS JOIN to get the total.
SELECT Category, Cnt , Cnt/v.totle * 100 as percent
from (
SELECT
(Case When TotalCost < 301 Then '0-300'
Else ' > 300' End) as Category,
count(*) as cnt
FROM Cars
GROUP BY (Case When TotalCost < 301 Then '0-300'
Else ' > 300' End)
) A CROSS JOIN (select count(*) totle from Cars) v
sqlfiddle
Results:
| Category | Cnt | percent |
|----------|-----|---------|
| 0-300 | 3 | 75 |
| > 300 | 1 | 25 |
I have a table with list of employees and the number of units that they have sold.
I want to get the top 25 percentile Avg units sold and Bottom 25 percentile Avg units sold.
I have created a representation of my data SLQ Fiddle
I really have no idea how to start on this? All the examples i see are for SQL Server and not MySQL. Here is what i am thinking.
I want 25 percentile and cant limit to 25 items. Basically it would involve:
1) #_of_employees = The number of total employees.
2) #_of_employees_in_25_percentile = #_of_employees*0.25
3) Calculate the sum of the units sold by the top/bottom 25 percentile (limit #_of_employees_in_25_percentile)
4) Divide the sum by #_of_employees_in_25_percentile to get the average.
How can all this be done efficiently in MySQL?
This is a solution that uses a devious trick I learned from this question.
SELECT id, unit_sold, n * 100 / #total AS percentile
FROM (
SELECT id, unit_sold, #total := #total + unit_sold AS n
FROM mydata, (SELECT #total := 0) AS total
ORDER BY unit_sold ASC
) AS t
SQL Fiddle.
What about this?
SELECT
SUM(unit_sold) AS sum_tot, SUM(unit_sold)/count(id) AS average,
SUM(CASE WHEN percentile<25 THEN unit_sold ELSE 0 END) AS sum_top25,
SUM(CASE WHEN percentile<25 THEN 1 ELSE 0 END) AS count_top25,
SUM(CASE WHEN percentile<25 THEN unit_sold ELSE 0 END)/SUM(CASE WHEN percentile<25 THEN 1 ELSE 0 END) AS average_top25,
SUM(CASE WHEN percentile>75 THEN unit_sold ELSE 0 END) AS sum_bottom25,
SUM(CASE WHEN percentile>75 THEN 1 ELSE 0 END) AS count_bottom25,
SUM(CASE WHEN percentile>75 THEN unit_sold ELSE 0 END)/SUM(CASE WHEN percentile>75 THEN 1 ELSE 0 END) AS average_bottom25
FROM
(SELECT
id, unit_sold, c * 100 / #counter AS percentile
FROM
(SELECT
m.*, #counter:=#counter+1 AS c
FROM
(SELECT #counter:=0) AS initvar, mydata AS m
ORDER BY unit_sold desc
) AS t
WHERE
c <= (25/100 * #counter)
OR c >= (75/100 * #counter)
) AS t2
Output:
SUM_TOT AVERAGE SUM_TOP25 COUNT_TOP25 AVERAGE_TOP25 SUM_BOTTOM25 COUNT_BOTTOM25 AVERAGE_BOTTOM25
850 283.3333 500 1 500 350 2 175
See SQL Fiddle.
The idea is to use the MySQL: LIMIT by a percentage of the amount of records? solution to get the percentiles. Based on that (and on pdw answer) we create an output in which we just show the top 25% and bottom 75%.
Finally, we count and sum to get the values you requested.
Note this runs on top of the command:
SELECT
id, unit_sold, c * 100 / #counter AS percentile
FROM
(SELECT
m.*, #counter:=#counter+1 AS c
FROM
(SELECT #counter:=0) AS initvar, mydata AS m
ORDER BY unit_sold desc
) AS t
WHERE
c <= (25/100 * #counter)
OR c >= (75/100 * #counter)
Whose output is:
ID UNIT_SOLD PERCENTILE
d 500 20
a 250 80
e 100 100
How about going with this logic:
Select all, order by percentile (DESC), limit to 25
Select all, order by percentile (ASC), limit to 25
Is this the type of logic you're looking for ?
Sample queries:
$q1 = mysql_query(SELECT * FROM table_name ORDER BY percentile DESC LIMIT 25)
$q2 = mysql_query(SELECT * FROM table_name ORDER BY percentile ASC LIMIT 25)
have a table like this
empid questionid options
1 1 A
2 1 A
3 1 B
4 1 C
now i need result like this
questionid responseA responseB responseC
1 50% 25% 25%
You could PIVOT;
SELECT questionid, (A / total) * 100 responseA, (B / total) * 100 responseB, (C / total) * 100 responseC FROM (
SELECT T1.questionid, T1.options, T2.total
FROM the_tbl T1
INNER JOIN (SELECT questionid, CAST(COUNT(*) AS MONEY) AS total FROM the_tbl GROUP BY questionID) T2 ON T2.questionid = T1.questionid
) T
PIVOT (
COUNT(options) FOR options IN ([A], [B], [C])
) AS pvt
ORDER BY questionid
T-SQL:
SELECT
questionid,
SUM(CASE options WHEN 'A' THEN 100.0 ELSE 0.0 END) / COUNT(options) AS responseA,
SUM(CASE options WHEN 'B' THEN 100.0 ELSE 0.0 END) / COUNT(options) AS responseB,
SUM(CASE options WHEN 'C' THEN 100.0 ELSE 0.0 END) / COUNT(options) AS responseC
FROM
answers
GROUP BY
questionid
Note: To avoid casting and multiplying 100, I used 100.0 and 0.0 values in CASE ... END expressions.
SELECT CAST(100/
( SELECT COUNT(*)
FROM your_Table as t2
WHERE t2.questionid = t1.questionid )
* COUNT(*) AS VARCHAR) + '%' AS 'Percent', OPTIONS, questionid
FROM your_Table as t1
--WHERE questionid = 2
GROUP BY OPTIONS, questionid
ORDER BY questionid;
This is one possible way you could do it
(works on SQL-Server but not sure if it does in MySql)