I'm relatively new to MySQL, and it's possible that what I want may not be possible to achieve with one single query. I have the following table structure:
name | value1 | value2
I have several records where the names are the same but different values. What I would like to do is to select distinct names, but their values should be added. Here's an example:
john | 1 | 2
jane | 6 | 3
mark | 2 | 5
mark | 3 | 1
So the query (if possible) would return
john | 1 | 2
jane | 6 | 3
mark | 5 | 6
Yes, you can using group by.
Like
select name , sum(value1) as sum1, sum(value2) as sum2 from mytable group by name
You use group by with agregating functions.
SELECT name, sum(value1) as total1,sum(value2) as total2 FROM mytable GROUP BY name
SELECT name, sum(value1) as column1 ,sum(value2) as column2
FROM mytable
GROUP BY name
I recommend to learn a few lessons on MySQL.
Me as a beginner it was very helpful.
$query = "SELECT name, SUM(value1) as v1, SUM(value2) as v2 FROM table GROUP BY name";
Related
Hard to say how is my problem, but What I want to to is here:
SELECT name
, (SELECT count(age) as count)
FROM 'students'
WHERE class = '1c'
;
SELECT name
, (SELECT count(age) as count)
FROM 'students'
WHERE class = '2b'
They are 2 separate mysql sentence, So how do I do that count in one sentence
to show something like:
+--------+-------+
| name | count |
+--------+-------+
| Peeter | 5 |
| Harry | 3 |
+--------+-------+
You can try below - use group by and in operator
SELECT name, count(age) as count
FROM 'students'
WHERE class in ('1c','2b')
group by name
Suppose we have a table like the one below.
Id | Name | Group
-----------------
1 | John | 1
2 | Zayn | 2
3 | Four | 2
4 | Ben_ | 3
5 | Joe_ | 2
6 | Anna | 1
The query below will select all of them.
SELECT `Name` FROM `Table` WHERE 1;
How would I select only one person from each group? Who it is doesn't really matter, as long as there's only one name from group 1 and one name from group 2 etc.
The GROUP BY clause isn't fit for this (according to my error console) because I am selecting non aggregated values, which makes sense.
The DISTINCT clause isn't great here either, since I don't want to select the "Group" and definitely not group by their names.
If is not important the resulting name You can anawy leverage some group functions eg with max or min..
leverage the group functions
select max(name) from your_table
group by Group;
otherwise you can use subquery
select name from your_table
where Id in (select min(Id) from your_table group by Group);
I recently got this question on interview which I failed to answer. The question was to list the number of duplicates that appear in a column employer like from this table
id | employer | employee
1 | isro | dude1
2 | isro | dude 2
3 | cnd | dude 3
4 | df | dsfdef
5 | dfdf | dfdfd
...
so the result should be like
isro = 2
df = 4
dfsf = 6
how do i achieve this?
I know there is count(*) which i could use with select statement with where clause, but how do i achieve the above result.
The HAVING clause can be used to filter on aggregated values:
SELECT employer, COUNT(*)
FROM yourTable
GROUP BY employer
HAVING COUNT(*) > 1
assuming TableName is the name of the table you want to select from, this would be your answer.
SELECT employer, count(employer)
FROM TableName
GROUP BY employer
HAVING COUNT(*) > 1
here is an answer to a very similar question that has some more info for you.
How to count occurrences of a column value efficiently in SQL?
I was wondering how could I get the number of occurrences of a common string from different results (or using OR in my query, as example below).
table example:
id | name | rank
1 | name1 | 1
2 | name1 | 1
3 | name2 | 1
4 | name3 | 1
5 | name1 | 2
6 | name1 | 2
7 | name3 | 2
Now, I need to count number of occurrences for rank = 1 and rank = 2, without duplicating the count.
doing something like this:
SELECT name, COUNT(DISTINCT name)
AS name_num FROM table WHERE rank = 1 GROUP BY name;
results is
name1 | 1
name2 | 1
name3 | 1
perfect, but now I need to include some other result (i.e. rank = 1 OR rank = 2) and get the occurrences from each name, without duplicating it.
the wanted result for query example using table example and rank = 1 OR rank = 2 should be:
name1 | 2
name2 | 1
name3 | 2
I'll try to explain the result I want:
name1 is present when rank = 1 (+1) and when rank=2 (+1);
name 2 is only present when rank=1
name3 is present when rank = 1 (+1) and when rank=2 (+1);
Is it possible?
Select Name,
Count(Distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
Sql Fiddle Demo
You want COUNT(DISTINCT rank), not COUNT(DISTINCT name). Since you're grouping by name, there will only be one distinct name in each group.
SELECT name, COUNT(DISTINCT rank) name_num
FROM table
WHERE rank in (1, 2)
GROUP BY name
Select Name,
Count(distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
This is what you asked for?
assume following data:
Data:
id | date | name | grade
--------+---------------+-----------+---------------
1 | 2010/12/03 | Mike | 12
2 | 2010/12/04 | Jenny | 12
3 | 2010/12/04 | Ronald | 15
4 | 2010/12/03 | Yeni | 11
i want to know who has the best grade in each day, something like this:
Desired Result:
id | date | name | grade
--------+---------------+-----------+---------------
1 | 2010/12/03 | Mike | 12
3 | 2010/12/04 | Ronald | 15
i thought query should look like this:
SELECT name FROM mytable
GROUP BY date
ORDER BY grade DESC
but it returns something like this:
Current Unwanted Result:
id | date | name | grade
--------+---------------+-----------+---------------
1 | 2010/12/03 | Mike | 12
2 | 2010/12/04 | Jenny | 12
i searched and i found the reason:
GROUP BY happens before ORDER BY so it does not see and can't apply ORDER.
so how can i apply ORDER on GROUP BY?
Note: please keep in mind that i need the most simple query, because my query is actually very complex, i know i can achieve this result by some subquery or JOINing, but i want to know how to apply ORDER to GROUP BY. thanks
I used Oracle for this example, but the SQL should work in mysql (you may need to tweak the to_date stuff to work with mysql). You really need a subquery here to do what you are asking.
CREATE TABLE mytable (ID NUMBER, dt DATE, NAME VARCHAR2(25), grade NUMBER);
INSERT INTO mytable VALUES(1,to_date('2010-12-03','YYYY-MM-DD'),'Mike',12);
INSERT INTO mytable VALUES(1,to_date('2010-12-04','YYYY-MM-DD'),'Jenny',12);
INSERT INTO mytable VALUES(1,to_date('2010-12-04','YYYY-MM-DD'),'Ronald',15);
INSERT INTO mytable VALUES(1,to_date('2010-12-03','YYYY-MM-DD'),'Yeni',11);
SELECT id
, dt
, name
, grade
FROM mytable t1
WHERE grade = (SELECT max(grade)
FROM mytable t2
WHERE t1.dt = t2.dt)
ORDER BY dt
Results:
ID DT NAME GRADE
1 12/3/2010 Mike 12
2 12/4/2010 Ronald 15
I know you said you wanted a GROUP / ORDER only solution but you will need to use a subquery in this instance. The simplest way would be something like this:
SELECT id, date, name, grade
FROM mytable t1
WHERE grade =
(SELECT MAX(t2.grade) FROM mytable t2 WHERE t1.id = t2.id)
This would show multiple students if they shared the highest grade for the day.