SQL get count of one column values and group by another - mysql

I have a table where I have 3 columns like so:
NAME ZONE_ID GROUP_ID
Mark 11 1
Mary 11 1
Mart 12 1
Mike 11 2
Kent 13 2
Now I want to count all the zone_id-s for a specific group. So the output for group 1 would be that there are 2 entries in zone 11 and 1 entry in zone 12 and for group 2 that there is 1 entry for zone 11 and one entry in zone 13.
It doesn't seem like it would be a very difficult query but I have hard time searching for it the right way and haven't found anything useful.

You need to group by group_id, zone_id:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id

Related

What should be the MySQL query having dynamic group by cluase?

Need MySQL query for below problem
Consider a table having student and their marks in a particular subject
Schema
std_id int(11)
marks int(11)
Sample data
std_id marks
1 10
2 15
3 90
4 120
5 25
6 29
7 121
8 122
Now I have an web app in which a form will take a input (int) from user.
For eg 12
then I am required to show total number of student ids (std_id) and their corresponding marks group.
Eg
std_total (tot no of students) group (marks range we got from form)
1 0-11
1 12-23
2 24-35
1 84-95
3 120-131
#Barmar Your answer was almost correct, I made few changes to clean the output. Your query gives output as below :
0-11 2
1-12 2
2-13 1
3-14 1
4-15 1
6-17 1
7-18 2
My query return Outout as
0-11 2
12-23 2
24-35 1
36-47 1
48-59 1
72-83 1
84-95 2
SELECT CONCAT(FLOOR(marks/12)*12, '-', FLOOR(marks/12)+11*(FLOOR(marks/12))+11) AS `group`, COUNT(*) as `std_total`
FROM yourTable
GROUP BY `group`
Use division and FLOOR() to get the beginning of each range.
SELECT CONCAT(FLOOR(marks/12), '-', FLOOR(marks/12)+11) AS `group`, COUNT(*) as `std_total`
FROM yourTable
GROUP BY `group`

How to calculate count of records by 2 columns

In MySQL I have a table.
Example:
id name type
1 Thomas 2
2 Thomas 2
3 Thomas 1
4 Paul 3
5 Paul 4
6 Paul 4
I need calculate same records by 2 columns.
Result for this example should be:
name type countOfRecords
Thomas 2 2
Thomas 1 1
Paul 3 1
Paul 4 2
Could you help me with this request?
Since you want records in your result set for each name and type <name,type> pair
you need to group by name and type.
SELECT
name,
type,
COUNT(*) countOfRecords
FROM your_table
GROUP BY name,type;
Note:
Group BY <some column> would generate a result set where number of rows = number of distinct / different / unique <some column>.
Same holds for multiple columns in GROUP BY clause.
This may be right solution:
SELECT name, type, COUNT(*) as countOfRecords
FROM your_table
GROUP BY name,type;

How to get value and count if it appears next 3 consecutive times

I have table with 3 columns, now how find value if it appears next 3 times immediately
i.e 1st trnas_value appears in next 3 consecutive times (repeaded 4 times)
and 2nd and 6th also rows also repeated the same
date tran_val name
23mar 22 mark
24mar 22 mark
25mar 22 mark
26mar 22 mark
27mar 22 mark
28jan 99 john
29jan 99 john
30jan 99 john
31jan 99 john
output
name trans_value consecutive_count
mark 22 2
john 99 1
Basic way to do it is to add a sequence number, ordering by the field that contains the relevant order.
I am not sure about the counts, but the easiest way seems to be to just subtract 3 from the consecutive count.
SELECT name,
tran_val,
MAX(cnt - 3) AS consecutive_count
FROM
(
SELECT date,
tran_val,
name,
#cnt:=IF(#tran_val=tran_val AND #name=name, #cnt + 1, 1) AS cnt,
#tran_val:=tran_val,
#name:=name
FROM some_table
CROSS JOIN (SELECT #cnt:=0, #tran_val:=0, #name:='') sub0
ORDER BY `date`
) sub1
GROUP BY name,
tran_val

How to filter rows from an ordered table?

Here's the table. It's ordered by points (desc) and id
id name points
1 ed 10
1 ed 9
2 jim 14
2 jim 8
2 jim 4
3 mike 11
Here's the results i'm looking for:
id name points
1 ed 10
2 jim 14
3 mike 11
How can this be done? basically, i want to list only the highest point row for each name and filter other rows away.
You can try something like this: use the MAX() function
SELECT id, name, MAX(points)
FROM your_table
GROUP BY id, name
ORDER BY points desc
Try this:
select id,name,max(points) from table1 group by id

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.