Calculation of percentage of group count(*) - mysql

Select * from Namelist;
Name Age
Sathish 25
Sathish 65
Sathish 55
Sathish 45
Sathish 35
Jana 55
Jana 25
Jana 10
Bala 55
Bala 26
How to get Percentage value for given format;
Name Count Percentege
Sathish 5 50%
Jana 3 30%
Bala 2 20%
Kindly share sql query?

This is a slightly sexier version of some of the other answers - note the use of sum(100) to avoid the longer (and more mundane) count(*) * 100 :)
select name, count(*) as count, sum(100) / total as percentage
from namelist
cross join (select count(*) as total from namelist) x
group by 1

This query(not tested) should work :
SELECT Name,
COUNT(*) AS Count,
(COUNT(*) / _total ) * 100 AS Percentege
FROM Namelist,
(SELECT COUNT(*) AS _total
FROM Namelist) AS myTotal
GROUP BY Name;

select
name,
count(name) as `count`,
count(name)/(select count(*) from namelist)*100 as pct
from namelist
group by name

replace column name and try this:
SELECT iName,
COUNT(iName) AS `Count`,
concat(FORMAT(((COUNT(iName) * 100) / NewPeople.iCount),2),'%') AS `Percentage`
FROM people, (SELECT COUNT(iName) AS iCount FROM people) NewPeople
GROUP BY iName;
Output:
Name Count Percentage
Sathish 5 50.00%
Jana 3 30.00%
Bala 2 20.00%

Related

MySQL group by with max value

Hi I have this table.
id lat lng userId
1 12 23 1
2 45 34 2
3 42 34 3
4 33 34 1
5 36 79 2
6 53 98 2
7 23 90 3
8 23 67 1
Here we have three users. (user ids 1,2,3). I want to get lateset record (id column max value) of each user.
My excepted output is this
userId lat lng
1 23 67
2 53 98
3 23 90
This query will give me group by option
SELECT
*
FROM
covid.locations
GROUP BY userId;
But how do I combine this with MAX(id) function.
One way is to use the following:
SELECT
cl.*
FROM covid.locations cl
INNER JOIN (
SELECT
userid
, MAX( id ) mid
FROM covid.locations
GROUP BY
userId
) g ON cl.userid = g.userid
AND cl.id = cl.mid
Another is to use row_number() over()
SELECT
userId
, lat
, lng
FROM (
SELECT
*
, ROW_NUMBER() OVER (PARTITION BY userid ORDER BY id DESC) rn
FROM covid.locations
GROUP BY
userId
) d
WHERE rn = 1
Both will identify the "most recent" row in the source table based in the id column of that table. Note that the second query requires MySQL version 8+ as this is when row_number() became supported in that database. The first query should run in dbms supporting SQL.
This will do
SELECT
*
FROM
covid.locations
where id in (select max(t.id) from covid.locations t group by t.userId)
order by id desc;
An example of the above query can be found in this SQLFiddle

MySQL: Get total hours, then multiply with rate, then rounding in one SELECT statement

My data looks like this:
UserID Hours BillRate
1 1.50 2.25
1 2.50 3.25
1 3.50 3.25
2 5.50 4.25
2 6.50 5.25
2 7.50 5.25
Is there anyway that I can get total spend for each person in one SELECT statement?
This is what I have. It returns what I want, but I have to do 2 SELECT statements.
SELECT UserID, SUM(OneSpend) AS TotalSpend
FROM (
SELECT UserID, ROUND(SUM(Hours)*BillRate,2) AS OneSpend
FROM mytable
GROUP BY UserID, BillRate
) a
GROUP BY UserID
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
GROUP BY UserID
According to the distributive property of multiplication over addition, SUM(Hours)*BillRate is the same as SUM(Hours * BillRate) when all the BillRate values are the same, as they are when you group by BillRate.

How to get date_of_highest_points

I have still problem with select date_of_highest_points:
$qry1 = mysql_query("INSERT INTO bbdnes_hraci (nick, sumfrags, sumpoints,
sumhours, lastdate1, highest_points, date_of_highest_points)
SELECT DISTINCT nick, SUM(frags), SUM(points), SUM(hours),
MAX(lastdate), MAX(points), ??????
FROM hraci
GROUP BY nick ");
I asked about that here https://stackoverflow.com/questions/18817900/select-in-select , but i have not solved it, despite council. Can somebody help me with code?
TABLE: hraci
nick frags points hours lastdate
Gamer1 20 100 1 2013-09-17 22:16:08
Gamer1 30 150 3 2013-09-18 20:17:15
Gamer1 25 125 0.5 2013-09-18 23:16:06
Gamer2 50 250 4 2013-09-17 21:11:30
Gamer2 5 25 2 2013-09-17 23:13:59
Need get:
TABLE: bbdnes_hraci
nick sumfrags sumpoints sumhours lastdate1 highest_points date_of_highest_points
Gamer1 75 375 4.5 2013-09-18 23:16:06 150 2013-09-18 20:17:15 ??
Gamer2 55 275 6 2013-09-17 23:13:59 250 2013-09-17 21:11:30 ??
You can do something like this:
select nick, SUM(frags) sumfrags, SUM(points) sumpoints, SUM([hours]) sumhours, max(lastdate) lastdate, max(points) highest_points
into #t1
from hraci
group by Nick
select t1.*, x.lastdate date_of_highest_points
from #t1 t1
outer apply
(select top 1 lastdate
from hraci t
where t.Nick = t1.Nick and t.points = t1.highest_points
order by t.lastdate desc) x
try this
select * from table_name where highest_points = (select max(highest_points) from table_name)
Untested:
SELECT DISTINCT nick, SUM(frags), SUM(points), SUM(hours),
MAX(lastdate), MAX(highest_points),
(
SELECT lastdate As date_of_highest_points
FROM hraci
WHERE points =(SELECT MAX(points) FROM hraci)
)
(rest of query. i.e. FROM and ORDER By's)

how to select Count of Ranges from mysql table?

i have a table t_points in mySql like example in below.
Name Surname Point
Joe Arnold 120
Michale Black 250
Masha Petrova 300
Natalie Jackson 120
John Turo 200
Bona Meseda 250
Zeyda Nura 150
Zanura Bohara 60
Shaheen Boz 360
Abbas Murat 160
Keira Black 230
Tom Robinson 480
Fred Balka 490
Semia Hudovi 90
Sona Bahari 60
i want to write a query which will display the count of point ranges. Point ranges are like this: point between 0 and 100, 101 and 200, 201 and 300, 301 and 400.
Result must be like below
0_100 101_200 201_300 301_400
3 5 4 3
i think u understand what i want to say. So which query i have to use for this result?
Thanks.
select
count(CASE WHEN point BETWEEN 0 AND 100 THEN 1 END) as count0_100,
count(CASE WHEN point BETWEEN 101 AND 200 THEN 1 END) as count101_200,
count(CASE WHEN point BETWEEN 201 AND 300 THEN 1 END) as count201_300,
...
from
t_poits
Something like that:
select count(*) as count, abs(point/100) as range
from t_poits
group by abs(point/100)
set #range = 500;
select floor(field1/#range)*#range as `from`,(ceil(field1/#range)+if(mod(field1,#range)=0,1,0))*#range as `to`, count(field1)
from table1
group by 1,2
order by 1,2;
You can group points column and do some math operation to specify range. Based on that you can count number of records.
Like..
SELECT concat( 101 * round( Point /101 ) , '-', 101 * round( Point /101 ) +100 ) AS `range` , count( * ) AS `result`
FROM t_points
GROUP BY 1
ORDER BY Point
I hope it will work for you.
select
concat(floor(Point/100),'01_',ceil(Point/100),'00') as ind,
count(*) as cnt
from t group by ind order by ind asc;

MySQL Max Count without Order By

I have the following MySQL line:
SELECT age, count(*) AS total FROM pacient WHERE age BETWEEN 20 AND 40 GROUP BY age ORDER BY age and I need to add an additional column to it that shows ONLY the max value of the count(*) for every row. For example:
13 2 7
18 2 7
23 5 7
24 7 7
26 6 7
32 3 7
38 1 7
41 1 7
46 4 7
This would be 3 columns and the 3rd column shows 7 since 7 was the highest number in the second column where the count(*) is made.
Here the solution:
select age,
count(*),
(select max(c) from
(select count(*) as c from pacient where age between 20 and 40 group by age) as x) as t
from pacient
where age between 20 and 40
group by age
order by age;
Have you tried to wrap your query with another query? something like
SELECT A.age, A.total, MAX(A.total) as max_value FROM (
SELECT age, count(*) AS total
FROM pacient
WHERE age BETWEEN 20 AND 40
GROUP BY age ORDER BY age) as A
GROUP BY A.age, A.total
select
p.Age,
count(*) CountPerAge,
max(ar.AllRecs) AllRecs
from
pacient p,
( select count(*) AllRecs
from pacient p2
where p2.age between 20 and 40 ) ar
where
p.age between 20 and 40
group by
p.age
By doing a join to the second "subselect" with no join condition, it will give a Cartesian result... Since it is a count with no group by, it will always return a single record and thus be joined to all age rows otherwise. The MAX() of the value is no problem since it is the only record will just be returned as-is.
It is always good to use SQL VIEWS instead of using sub queries. Because VIEW will be having already compiled result.
CREATE VIEW subqueryView
SELECT age, count(*) AS total
FROM pacient
WHERE age BETWEEN 20 AND 40
GROUP BY age ORDER BY age
SELECT A.age, A.total, MAX(A.total) as max_value FROM (SELECT FROM subqueryView) as A
GROUP BY A.age, A.total