sql query to calculate sum of agroup counts - php

fields id ,party,votes
my query getting o/p like this
partyname bjp aap congress
votes 1 2 2
but i also need sum of votes. below is my expected output.
BJP aap congress total
1 2 2 5
SELECT RES.PARTYNAME, COUNT( RES.CONSTITUENCY )
AS VOTESCOUNT FROM voter_count RES JOIN (SELECT CONSTITUENCY, MAX( VOTES )
AS VOTES FROM voter_count GROUP BY CONSTITUENCY)MAXS USING ( VOTES, CONSTITUENCY )
GROUP BY PARTYNAME LIMIT 0 , 1000";

Take a look at this
SELECT SUM(something) AS something, SUM(else) AS else FROM stuff
Just an idea how you can get the SUM

According to your table,
SELECT SUM(partyname) AS total_parties, SUM(votes) AS total_votes FROM table_name;
is this what you want?? Your question is not clear & query is irrelavant to description...

Related

how should i do this query in mysql

I want to do the following. I have a table in the database, I am working on a table called asistencia and this table has 3 columns
id_asistencia as a int AUTOINCREMENT
nro_matricula as an int which I took it from another table called
alumnos
fecha as a date
This is a sketch of the database
id_asistencia | nro_matricula | fecha
1 | 0001| 2015-01-10
2 | 0002| 2015-01-10
3 | 0002| 2015-02-10 (another date )
The thing is I have to do a percentage
select all id_1 records in my nro_matricula column and see how many times its repeated in my rows and do a percentage respect all the dates in my database
EG : id_1 came to class day(whatever day) and he/she did not came to class the next day so id_1 has 50% assistance
Expected result
nro_matricula | percentage
0001| 50
0002| 100
The question is how can I make this query. If can be done in PHP its even better but i feel that this can be done in SQL
PS : The Database wasn't created by me
And excuse my English is not the better and i expect it to be understandable for you to help me
You can use sql statement like this:
select (
sum (if nro_matricula = '001' ,1,0 )
/ count(*)
from asistencia
--where nro_matricula = '001'
Maybe just simply:
select al.nro_matricula,
100 * count(distinct al.fecha) / (select count(distinct al1.fecha) from alumnos al1) as percentage
from alumnos al
group by al.nro_matricula
I did found the answer to my question. Thank you all for helping me out
SELECT
asistencia.nro_matricula as matricula,
COUNT( DISTINCT asistencia.fecha)* 100 /
COUNT( DISTINCT asistencia.nro_matricula) / (SELECT COUNT(DISTINCT asistencia.fecha)
FROM asistencia
ORDER BY COUNT(*) DESC
LIMIT 1 )
as porcentaje_asistencia
FROM asistencia
JOIN alumno
WHERE asistencia.nro_matricula = alumno.nro_matricula AND alumno.id_curso = 'basica6a'
Tried this in Oracle. Should work in MySQL too.
SELECT aa.NRO_MATRICULA , days_present/total_count* 100 FROM
(SELECT DISTINCT NRO_MATRICULA,
COUNT(*) as days_present FROM ASISTENCIA GROUP BY NRO_MATRICULA ) AA
,
(SELECT COUNT(*) as total_count FROM (SELECT DISTINCT(FECHA) FROM ASISTENCIA GROUP BY FECHA)) BB
Ouptut
nro_matricula percentage
0001 50
0002 100
The query (SELECT COUNT(*) FROM (SELECT DISTINCT(FECHA) FROM ASISTENCIA AA GROUP BY FECHA)) will give count of distinct date (2 in your case). Then we are getting distinct nro_matricula group by nro_matricula to get its count which will give the days it was present. Then divide both values from above steps to get percentage.

Is that possible to count all row base on one value, and get the column name with largest count (mysql)?

I want to count all rows base on the category column, example below, category apple has two rows, so get the the category name apple. Thank you.
id user_id category
1 2 apple
2 4 banana
3 6 apple
4 7 berry
//Count all rows, apple has two row, the largest count, so then get the the category name apple
Use Group by to count the category.
Then order the result set in descending order of the count and select the top 1.
Query
select t.category
from
(
select category,
count(category) as cat_count
from fruits
group by category
)t
order by t.cat_count desc limit 1;
SQL Fiddle
If multiple category having same max count. Then,
Query
select t.category
from
(
select category,
count(category) as cat_count
from fruits
group by category
)t
where t.cat_count =
(
select count(category) as cat_count
from fruits
group by category
order by count(category) desc
limit 1
);
SQL Fiddle
Add limit if you need top 1,2,3 like that limit X x is 1,2,3
Select count(*) as total,category from table group by category from table order by count(*) desc

PHP/MYSQL: highscores get rank of current player with a lot of scores. (multiple per player)

For each game the user play's i save the score (and not overwrite the old score)
ID Name UserId Score
1 tom 1 8
2 john 2 12
3 hank 3 13
4 hank 3 1
5 tom 1 5
ranks will be:
1 hank:13, 2 john:12, 3tom:8
Therefore to reach the top 100 of the players i do the following:
SELECT id, name, date, userId, MAX(score) as score
FROM scores
GROUP BY userId
ORDER BY MAX(score) DESC LIMIT 100
The rank of these 100 people i know because of the order of the returned data.
It gets harder however when i want to get the rank of the current userId, now i use:
SELECT id, name, score, date, userId, FIND_IN_SET( score, (
SELECT GROUP_CONCAT( score
ORDER BY score DESC )
FROM
(
SELECT MAX( score ) AS score
FROM scores
GROUP BY userId
ORDER BY MAX( score )
) AS T
)) AS rank
FROM scores
WHERE userId = '$_POST[userId]'
ORDER BY score DESC
LIMIT 0 , 1
This all seemed to work out fine first however now i tested it with a lot of players. The players in the lower sement all seem to have a rank of 0 (rank not found)
What i found out is that the GROUP_CONCAT function does not fully return all the results because the result is to big. (it does not fit in its CONCAT string or something) Does anyone know a way to get the rank of a person with the same effect that works on a data set as big as i want?
You can do fairly simple:
SELECT
score,
FIND_IN_SET(score,
(SELECT GROUP_CONCAT(DISTINCT score ORDER BY score DESC) FROM scores)
) rank
FROM
scores
WHERE
userId = 3
ORDER BY score DESC
The subquery is only executed once because it's not correlated

Get a rank, based on score, from an unordered MySql Database when given a Username

Okay so I have a table that has the following
KEY username password score
The above columns are not in any specific order.
I want to send my Database a username and have it send me back what rank that user name is based on its score. So for example if I had 10 people in there and the 3rd person in has the highest score. When I pass the 3rd persons username in I want it to send back 1.
Is this possible?
I have been trying things like this
$result = mysql_query("SELECT * FROM tablename where username='$username' ORDER BY score DESC");
but it doesnt seem to give me the row number
This will handle ranks that have the same score.
SELECT d.*, c.ranks
FROM
(
SELECT Score, #rank:=#rank+1 Ranks
FROM
(
SELECT DISTINCT Score
FROM tableName a
ORDER BY score DESC
) t, (SELECT #rank:= 0) r
) c
INNER JOIN tableName d
ON c.score = d.score
// WHERE d.username = 'Helen'
SQLFiddle Demo (Ranking with duplicates)
SQLFiddle Demo (with filtering)
for example
KEY username password score Ranks
1 Anna 123 5 3
2 Bobby 345 6 2
3 Helen 678 6 2
4 Jon 567 2 4
5 Arthur ddd 8 1
for better performance, add an INDEX on column Score,
ALTER TABLE tableName ADD INDEX (Score)
SELECT
(SELECT COUNT(*)+1 FROM tablename WHERE score > t.score) as rank,
*
FROM
tablename t
where
username='$username'
The ORDER BY in your query is useless since you're only returning one row.

MySQL query: generate a report of the top votes for the each food type

I have a user table with id, username and food_id columns. The idea is the user stored their favorite food and we come up with a league table of foods and I want to generate a report of the top votes for the each food type. I am using MySQL and PHP.
For clarity, here is an example of the table:
id food_id username
1 1 Bob
2 100 Jane
3 200 Andy
4 1 Maggy
5 100 Rich
6 100 Mick
7 1 Kevin
How do I write the query to list the foods that have the most votes. I want to limit the result to x number, say top 100. In this case I want the result to be as follows:
food_id score
1 3
100 4
I hope the question is clear enough. The query is beyond me but I am sure it must be possible to do it using DISTINCT and COUNT in some way or other. Perhaps it's sub queries?
select food_id, count(*) score
from myTable
group by food_id
order by score desc limit 100
SELECT F.food_name,
COUNT(*) AS score
FROM myTable AS M
INNER JOIN food_table AS F
ON F.food_id = M.food_id
GROUP BY F.food_name
ORDER BY score DESC limit 100
select count(*) as top100 from table group by food_id order by top100 desc limit 100
SELECT f.`food_id`, COUNT(*) AS `count`
FROM `fav_food_table` f
GROUP BY f.`food_id`
ORDER BY `count` DESC
LIMIT 100;

Categories