MS Access Group ID with parent and children - ms-access

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

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;

how to MYSQL compare values from same columns

how do i compare 1 table in mysql?
with the following table
name table belajar
id model name
1 AAA AAA
2 BBB BBB
3 CCC CCC
and I want to generate a query
id name model idmodel namemodel modelmodel
1 AAA AAA 2 BBB BBB
1 AAA AAA 3 CCC CCC
2 BBB BBB 1 AAA AAA
2 BBB BBB 3 CCC CCC
3 CCC CCC 1 AAA AAA
3 CCC CCC 2 BBB BBB
It sounds like maybe a self join is what you want here:
SELECT
t1.id,
t1.name,
t1.model,
t2.id AS idmodel,
t2.name AS namemodel,
t2.model AS modelmodel
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.name <> t2.name AND
t1.model <> t2.model;
ORDER BY
t1.id,
t2.id;
The join criteria is basically a cross join, except that you don't want to match two records if the names and models be the same.

Mysql multiple left join on same table for grouping results

Table
This stable stores some history of address changes
Id Name Address Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1
Result I need a result like beleow
Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III
Is it possible to achieve this result with mysql joins
You can try using case when expression
select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename

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?

finding max value from duplicate value in 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.