i'm a newbie on mysql. i want to display the largest number of votes in the table as well as the candidate number, last name, first name, and middle name. but when i use the max() function to select the largest number of votes. the largest vote is selected but the candidate number, last name, first name and middle name are the default first values in the database... here's the example:
candidate table:
candidate no last name first name middle name position votes
038-001 banchero chris ace president 99
038-002 castro jayson texk president 100
what i want to display:
038-002 castro jayson texk president 100
my problem is that the first row is always displayed only the highest votes are display. like this:
(038-001 banchero chris ace 100)
thanks. I would really appreciate any help. :)
select * from tablename
where votes = (select max(votes) from tablename)
Will return the row with max number of votes. (And both rows if it's a tie.)
Related
I have a table like the below
Pet Name
Test
Result
Celine
A
Positive
Diogo
A
Negative
Jordah
A
Positive
Diogo
B
Positive
Jordah
B
Negative
Caesar
A
Negative
I need to come up with a query (or a view) showing the below
Pet Name
Total tests
Positive
Diogo
2
1
Jordah
2
1
Celine
1
1
Caesar
1
0
I tried a query like
SELECT Pet Name, COUNT(*)
WHERE Result=‘Positive’
GROUP BY Pet Name
ORDER BY COUNT(*) DESC
which is ok, but it doesn’t provide me with the number of tests done by each pet.
The CASE WHEN sort of options do not seem to be applicable as the table gets populated with more Pet Names as vet makes new friends.
Thanks in advance.
This has been identified as duplicate of this, but I don't think it's the case. As I mentioned in above, it's the actual query that will provide the pet name, I won't know that name in advance, so no conditions such as IF or CASE WHEN sounds like helpful.
I have managed my db in MySQL. My table looks like this:
.
There are a total of 6 types of ratings for each hospital. Each rating has max score of 10. Users vote each rating total of 10. I want to know how do I structure my query that it should automatically calculates rank of each hospital according to its total of rating values. Suppose hospital A has total (180) & hospital B has total (120) then hospital A given auto ranking as 1 & hospital B as 2. Since I have limited knowledge on sql I am not able to structure proper query.
Types of ratings are (charges, behaviour, admission, properInformation, hygine, treatment)
You will want to use a combination of GROUP BY and SUM.
Here are two good resources to help you with your problem.
MYSQL SUM GROUP BY
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
I have a table like so:
Customer Purchase Date Product
Frank 7/28/2015 Hammer
Bob 7/29/2015 Shovel
Bob 7/29/2015 Pickaxe
Bill 7/30/2015 Pliers
The Purchase Date field records a new entry for every purchase. So, if in one visit a customer purchases four items, my database creates four entries each with the same date.
I'm trying to write a query that displays the numbers of visits for each customer. Output like so:
Frank 1
Bob 1
Bill 1
But when I use the COUNT function on the date in my query, it returns:
Frank 1
Bob 2
Bill 1
I want my query to only count unique dates, but the COUNT function doesn't work. Everywhere I read, it also says that the SQL COUNT (Distinct) doesn't work in Access. Access help says that if I set the Query Properties to Unique Values "Yes", it should only return unique values, but it doesn't work. I tried Unique Record "Yes" also, but that didn't work either.
Please help! Thanks!
Try this:
select Cust, count(cust) as CustomerCount
from (Select Distinct Table1.Customer as cust, Table1.PurchaseDate
from Table1)
group by cust
To make it more clear, If I have this data in MySql:
name | allowance | age
----------------------
khan | 50 | 20
aal | 60 | 22
hyme | 50 | 21
khan | 61 | 20
notice that there are two 'khan' in the database with different allowance. I want to only show the name and the age but if I show it using the mysqli select statement, there would be two 'khan' but I only want to show only 1 'khan'. How can I do it?
You need to use GROUP_CONCAT to see agges of all Khans;
select name, GROUP_CONCAT(age) ages from Table group by name
or for minimum aged khan
select name , min(age) MiniumAge from Table group by name
or for elder khan
select name , max(age) MaxAge from Table group by name
or any khan
select name , age from Table group by name
.
Please try below query:-
SELECT name, age FROM table_name WHERE group by name
If you want any from multiple same record then simply used group by query.
I think you could do this:
SELECT name, age FROM table_name WHERE group by name,age
First thing: if both those "khan"s are the same person with two different allowances then your schema is not properly normalized and it will give you big troubles later - imagine you want to change "khan" to "Khan" - now you have to update it in multiple places instead once. Depending on your actual needs you may want one table of people (person_id, name, age), and table of allowances (person_id, allowance, [..some other parameters?..]).
Second, to really get what you want, either you use group by, to get one "random" row per each name as suggested in other answers, or you can do
SELECT DISTINCT name, age FROM table;
which will give you one row per each name-age combination, so khan-20 will be there only once - but if there were khan-25 then that is probably different person and you would have two khans returned, each with their own age.
You can try this mate:
SELECT DISTINCT
name, age
FROM
<your_table>;
or this one
SELECT
name, age
FROM
<your_table>
GROUP BY
name;
Q: Is there any chance that if there are 2 records of tha same name have difference value of age? If so, kindly update the question so that better answers will be given. Cheers!
I have a table with 3 columns
Column 0: auto inc index
Column 1: Person's Gener
Column 2: The STATE the person lives in (US States and Puerto Rico)
I want to run a query that tells me how many MEN are in each state
so the output would list all 50 states (in 1 column) and the second would be a number to determine the number of MEN in that state
Alaska 1000
New York 85000
(the figures above aren't accurate but i'm illustrating what I am looking for)
Thanks
You need to use a GROUP BY clause and the COUNT aggregate function.
SELECT State, COUNT(*) AS NumberOfMen
FROM your_table
WHERE Gender = 'M'
GROUP BY State
try this
select STATE, count(*) as num from table_name where gender ='MALE' GROUP BY STATE;