finding max value from duplicate value in mysql - mysql

I am having a table structure like this:
Date Count name
2015-03-05 154903 AAA
2015-03-04 153591 AAA
2015-03-03 151277 AAA
2015-03-06 93997 BBB
2015-03-03 93294 BBB
2015-03-02 79006 BBB
2015-03-08 77324 BBB
2015-03-06 144588 AAA
2015-03-07 144119 AAA
2015-03-02 133543 AAA
2015-03-01 124435 AAA
2015-03-08 115227 AAA
2015-03-05 98866 BBB
2015-03-04 96673 BBB
2015-03-07 96298 BBB
2015-03-01 68724 BBB
2015-03-05 55748 CCC
2015-03-01 55322 CCC
I need to find the maximum count from every value
Eg.
AAA is having max value of 154903
BBB is having max value of 98866
CCC is having max value of 55322
how query in mysql to fetch the data like this.

Not much to it -- just use the max aggregate:
select name, max(countfield)
from yourtable
group by name

Like #GordonLinoff said, this is the very very basic of SQL.
A simple group by should do it...
SELECT name, max(Count)
FROM table
GROUP BY name
Please read some about the Group by clause.

Related

how can i sort data by rows? [MYSQL]

my database
id
title
create_by
1
aaa
2
2
bbb
1
3
ccc
3
4
ddd
2
5
eee
2
6
fff
3
what i want. sort by row that have same most value.
id
title
create_by
1
aaa
2
4
ddd
2
5
eee
2
3
ccc
3
6
fff
3
2
bbb
1
Thank you very much.
WITH cte AS ( SELECT id,
title,
create_by,
COUNT(create_by) OVER (PARTITION BY create_by) cnt
FROM src_table )
SELECT id,
title,
create_by
FROM cte
ORDER BY cnt, id;

Skip maximum timestamp and fetch others

Table A has Registration_id and Client_No.
Table B has Last_Login_Email,Last_Login_Mobile and Last_Login_OFX timestamp of each Registration_id.
My requirement is below :-
Fetch all Registration_id's which are tied to multiple Client_no and out of these skip Registration_id with maximum time-stamp and fetch others.
Example:-
Table A
Registration_id Client_No
AAA 111
BBB 111
CCC 111
DDD 444
EEE 555
FFF 666
GGG 666
So in above table AAA,BBB,CCC are tied to same Client_no which is 111.Also FFF,GGG tied to same CLient_no which is 666.
So these Registration_id (AAA,BBB,CCC) and (FFF,GGG) qualified for first part of my condition.
Now out of 1st set of Registration_id which are tied to same Client_no i want to skip the Registration_id with maximum time-stamp and fetch other.And same for 2nd set and so on.
Example: (for typing convinicence i have used Date instead of timestamp in below example)
Table B
Table B DD/MM/YYYY DD/MM/YYYY DD/MM/YYYY
Registration_id Last_Login_Email Last_Login_Mobile Last_Login_OFX
AAA 01/12/2017 02/12/2017 01/11/2017
BBB 01/01/2018 02/01/2018 03/01/2018
CCC 01/11/2017 02/11/2017 03/11/2017
DDD 01/01/2018 02/01/2018 03/01/2018
EEE 21/01/2018 22/01/2018 23/01/2018
FFF 12/01/2018 13/01/2018 14/01/2018
GGG 29/01/2018 28/01/2018 31/01/2018
Note:- In above table B we dont have to do anything with DDD and EEE as they are not qualified under 1st part of condition.I have given in above table just for sake of completeness.
Lets take 1st set here which is AAA,BBB,CCC
From Table B
Maximum-Timestamp out of Last_Login_Email,Last_Login_Mobile
andLast_Login_OFX
AAA 02/12/2017
BBB 03/01/2018
CCC 03/11/2017
Above we can see maximum timestamp is for BBB(out of AAA,BBB,CCC) so i want to skip BBB here and fetch AAA and CCC.
Same goes to other set which is FFF,GGG
From Table B
Maximum-Timestamp out of Last_Login_Email,Last_Login_Mobile and
Last_Login_OFX
FFF 14/01/2018
GGG 31/01/2018
Above we can see maximum timestamp is for GGG so i need to skip GGG and fetch FFF.
So my overall logic should fetch AAA,CCC and FFF.
Hope i am clear with my requiremet.
This is one way to do that.
As you didn't provide test case, I was - in turn - too lazy to type full names. Though, you explained it quite well, thank you for that.
TA and TB are your A and B tables
REG_ID is your REGISTRATION_ID
QUALI_1 and QUALI_2 represent your two qualification steps
Here we go:
SQL> with quali_1 as
2 (select reg_id, client_no
3 from ta
4 where client_no in (select client_no
5 from ta
6 group by client_no
7 having count(distinct reg_id) > 1)
8 ),
9 quali_2 as
10 (select b.reg_id, q.client_no,
11 greatest(b.ll_email, b.ll_mobile, b.ll_ofx) ll_max
12 from tb b join quali_1 q on b.reg_id = q.reg_id
13 )
14 select reg_id
15 from quali_2
16 where (client_no, ll_max) not in
17 (select client_no, max(ll_max) ll_max
18 from quali_2
19 group by client_no)
20 order by reg_id;
REG
---
aaa
ccc
fff
SQL>

MySql/Rails - Group by two columns with different value in one

i have problem group query
cmpid = [222,232,343,232,232]
ProductAttribute.select(:txt,:val).where("cmpid IN (?)", cmpid).group(:txt)
Tabel looks
-----------------------
id | txt | val | cmpid
-----------------------
1 aaa ddd 222
2 aaa eee 232
3 aaa ddd 343
4 bbb ded 232
5 vvv ddd 232
But when I group id by txt its not return txt=>"vvv" because it's grouped by "ddd" value - and its correct (sql sense) but how to group items with check whether :txt is also same or different?

How to get row occurrence value in MySQL?

I have a following table
id name amount
1 aaa 1000
2 bbb 1500
3 ccc 1700
4 ddd 2000
5 aaa 1400
6 aaa 1700
7 bbb 1800
What I need is one more column to display the occurrence value based on the name as follows
id name amount occurrence
1 aaa 1000 1
2 bbb 1500 1
3 ccc 1700 1
4 ddd 2000 1
5 aaa 1400 2
6 aaa 1700 3
7 bbb 1800 2
You can do it with a correlated subquery:
SELECT id, name, amount,
(SELECT COUNT(*)
FROM mytable AS t2
WHERE t2.name = t1.name AND t2.id <= t1.id) AS occurrence
FROM mytable AS t1
Demo here

MS Access Group ID with parent and children

I have a question regarding MS ACCESS.
I have a table with multiple columns and about 1600 records.
The first two columns are sort of the names of the record.
They can be exactly the same or they can be different. However I need to group them with a GroupID.
The grouping should give the same GroupID to the same family. a goes with b then all the names which goes with b are also in the same group as a and so on...
I try to illustrate it in the below table
GroupID NAme1 Name2 Varia
aaa aaa lkj
aaa aaa kjkjl
aaa bbb lkjlkj
ccc ccc kljlkjl
ddd ccc kajlsjf
bbb eee ljlkjlj
eee eee lkjlkj
fff fff kljl
fff ggg jlkjlj
iii ccc jlkjlkj
jjj jjj alsjlfj
This would have the following ids
GroupID NAme1 Name2 Varia
1 aaa aaa lkj
1 aaa aaa kjkjl
1 aaa bbb lkjlkj
2 ccc ccc kljlkjl
2 ddd ccc kajlsjf
1 bbb eee ljlkjlj
1 eee eee lkjlkj
3 fff fff kljl
3 fff ggg jlkjlj
2 iii ccc jlkjlkj
4 jjj jjj alsjlfj