select non group by columns with count in Mysql - mysql

I have a table tbl with three columns:
id | fk | dateof
1 | 1 | 2016-01-01
2 | 1 | 2016-01-02
3 | 2 | 2016-02-01
4 | 2 | 2016-03-01
5 | 3 | 2016-04-01
I want to get the results like this
Id count of Id max(dateof)
2 | 2 | 2016-01-02
4 | 2 | 2016-03-01
5 | 1 | 2016-04-01
My try
SELECT id,tbl.dateof dateof
FROM tbl
INNER JOIN
(SELECT fk, MAX(dateof) dateof ,
count(id) cnt_of_id -- How to get this count value in the result
FROM tbl
GROUP BY fk) temp
ON tbl.fk = temp.fk AND tbl.dateof = temp.dateof

This is an aggregation query, but you don't seem to want the column being aggregated. That is ok (although you cannot distinguish the rk that defines each row):
select count(*) as CountOfId, max(dateof) as maxdateof
from t
group by fk;
In other words, your subquery is pretty much all you need.
If you have a reasonable amount of data, you can use a MySQL trick:
select substring_index(group_concat(id order by dateof desc), ',', 1) as id
count(*) as CountOfId, max(dateof) as maxdateof
from t
group by fk;
Note: this is limited by the maximum intermediate size for group_concat(). This parameter can be changed and it is typically large enough for this type of query on a moderately sized table.

You obviously want one result row per fk, so group by it. Then you want the max ID, the row count and the max date for each fk:
select
max(id) as max_id,
count(*) as cnt,
max(date_of) as max_date_of
from tbl
group by fk;

Related

How to find the count of a particular number that can exist in 2 columns

I have a table that consist of a table that describes calls. Hence there is a to column and a from column. The problem is that I want the total messages sent by each number, which can be from or to. Refer to the table above for visuals.
I want the final table to be somethng that shows A : 3 , B: 2 , C:1 and D:1.
How do u count the numbers in 2 columns and sum them up?
One solution would be to first UNION ALL two aggregate queries to gather the count of occurences of each value in the two different columns, and them sum the results in an outer query, like:
SELECT val, SUM(cnt) cnt
FROM (
SELECT `from` val, COUNT(*) cnt FROM mytable GROUP BY `from`
UNION ALL
SELECT `to`, COUNT(*) FROM mytable GROUP BY `to`
) x
GROUP BY val
This demo on DB Fiddle with your sample data returns:
| val | cnt |
| --- | --- |
| A | 3 |
| B | 2 |
| C | 1 |
| D | 1 |
| E | 1 |
You can unpivot the data and aggregate:
select person, count(*) as num_calls
from ((select from as person from t) union all
(select to as person from t
) c
group by person;
Note that from and to are really, really bad names for columns because they are SQL keywords. I haven't escaped them in the query, because that just clutters the query and I assume the real columns have better names.

Get latest record from each group and number of group rows in MySQL

I got a table like this:
id | column_a | column_value
1 | x | 5
2 | y | 7
3 | z | 4,7
4 | x | 3,6
5 | y | 2
6 | w | 5,8,9,11
I would like to get back column_value from latest record in each groups AND a count number of rows in the groups.
So the result should be this:
count(id) | column_value
2 | 3,6
2 | 2
1 | 4,7
1 | 5,8,9,11
I tried to reach this on the following two path:
select count(id), column_value
from table
group by column_a
This version get back the first records from the groups so its not ok for me.
select count(id), column_value
from table
where id in (select max(id)
from table
group by column_a)
This version also wrong because count cannot works well without group by.
I cannot figure it out how can I combine two versions advantages.
Any help is appreciated.
Try this
Select cnt, column_value
from tst t inner join (
Select column_a, count(id) cnt, max(id) as max_id
from tst
group by column_a ) x on (t.column_a= x.column_a and t.id = x.max_id)
order by cnt desc

Limit MySQL Results to One From Each "Group"

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);

Selecting non-group field from query with aggregate function

I have a table tbl with three columns:
id | fk | dateof
1 | 1 | 2012-01-01
2 | 1 | 2012-01-02
3 | 2 | 2012-02-01
4 | 2 | 2012-03-01
5 | 3 | 2012-04-01
id is the ID of the row, fk is a foreign key to another table and dateof is a date column.
What I want is to get the id where the dateof is the latest grouped by fk. What I've tried:
SELECT id, MAX(dateof) FROM tbl GROUP BY fk
But I get results like this:
1 | 2012-01-02
3 | 2012-03-01
5 | 2012-04-01
When I want:
2 | 2012-01-02
4 | 2012-03-01
5 | 2012-04-01
How can I query and get the results I'm looking for? MySQL server if it matters. Thanks.
Personally I would do
SELECT id, tbl.dateof dateof
FROM tbl
INNER JOIN
(SELECT fk, MAX(dateof) dateof
FROM tbl
GROUP BY fk) temp
ON tbl.fk = temp.fk AND tbl.dateof = temp.dateof
Gordon's answer is correct and less code, but I prefer creating a temp table. It's more clear to other developers what I'm doing.
To get what you want:
select t.*
from (select tbl.*,
row_number() over (partition by fk order by dateof desc) as seqnum
from tbl
) t
where seqnum = 1
This assumes that you are using a reasonable database that has window functions. You don't specify the database in your question.

Sort data before using GROUP BY?

I have read that grouping happens before ordering, is there any way that I can order first before grouping without having to wrap my whole query around another query just to do this?
Let's say I have this data:
id | user_id | date_recorded
1 | 1 | 2011-11-07
2 | 1 | 2011-11-05
3 | 1 | 2011-11-06
4 | 2 | 2011-11-03
5 | 2 | 2011-11-06
Normally, I'd have to do this query in order to get what I want:
SELECT
*
FROM (
SELECT * FROM table ORDER BY date_recorded DESC
) t1
GROUP BY t1.user_id
But I'm wondering if there's a better solution.
Your question is somewhat unclear but I have a suspicion what you really want is not any GROUP aggregates at all, but rather ordering by date first, then user ID:
SELECT
id,
user_id,
date_recorded
FROM tbl
ORDER BY date_recorded DESC, user_id ASC
Here would be the result. Note reordering by date_recorded from your original example
id | user_id | date_recorded
1 | 1 | 2011-11-07
3 | 1 | 2011-11-06
2 | 1 | 2011-11-05
5 | 2 | 2011-11-06
4 | 2 | 2011-11-03
Update
To retrieve the full latest record per user_id, a JOIN is needed. The subquery (mx) locates the latest date_recorded per user_id, and that result is joined to the full table to retrieve the remaining columns.
SELECT
mx.user_id,
mx.maxdate,
t.id
FROM (
SELECT
user_id,
MAX(date_recorded) AS maxdate
FROM tbl
GROUP BY user_id
) mx JOIN tbl t ON mx.user_id = t.user_id AND mx.date_recorded = t.date_recorded
Iam just using the technique
"Using order clause before group by inserting it in group_concat clause"
SELECT SUBSTRING_INDEX(group_concat(cast(id as char)
ORDER BY date_recorded desc),',',1),
user_id,
SUBSTRING_INDEX(group_concat(cast(`date_recorded` as char)
ORDER BY `date_recorded` desc),',',1)
FROM data
GROUP BY user_id