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
Related
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;
I want to swap the column contents of same datatype from two different tables in MySQL.
Table 1
id value
1 aaa
2 bbb
3 ccc
Table 2
id value
1 ddd
2 eee
3 fff
And my new tables should be like this.
Table 1 Table 2
id value id value
1 ddd 1 aaa
2 eee 2 bbb
3 fff 3 ccc
Is there any way to do this in MySQL?
Just do this, as commenters suggested:
RENAME TABLE Table_1 TO tmp_table,
Table_2 TO Table_1,
tmp_table TO Table_2;
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>
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?
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