Two column (different name, different table) as one row - mysql

is it possible to change these two tables
Employee A B C
Alex G G B
Martin B G G
...
------------------------------
Employee2 AA BB CC
Sam G B B
Max G G B
...
Into one table?
List Employee Mark
AAA Alex G
BBB Alex G
CCC Alex B
AAA Sam G
BBB Sam B
CCC Sam B
...
AA and A are different column in different table but I want to display them in one table with the same name (e.g. "AAA") and it must be in row not in column.

You can use a UNION query to unpivot the data from columns into rows:
SELECT 'AAA' AS List, Employee, A AS Mark
FROM t1
UNION ALL
SELECT 'BBB' AS List, Employee, B AS Mark
FROM t1
UNION ALL
SELECT 'CCC' AS List, Employee, C AS Mark
FROM t1
UNION ALL
SELECT 'AAA' AS List, Employee2, AA AS Mark
FROM t2
UNION ALL
SELECT 'BBB' AS List, Employee2, BB AS Mark
FROM t2
UNION ALL
SELECT 'CCC' AS List, Employee2, CC AS Mark
FROM t2
ORDER BY Employee, List
Output (for your sample data)
List Employee Mark
AAA Alex G
BBB Alex G
CCC Alex B
AAA Martin B
BBB Martin G
CCC Martin G
AAA Max G
BBB Max G
CCC Max B
AAA Sam G
BBB Sam B
CCC Sam B
Demo on SQLFiddle

Related

how to choose distinct values from the pairs of names in sql?

The table f has these values:
name1 name2 count
Harrison Ford Mullah Omar 3
Harrison Ford Michael Caine 6
Michael Caine Mullah Omar 2
Michael Caine Harrison Ford 6
Mullah Omar Michael Caine 2
Mullah Omar Harrison Ford 3
How to choose only the distinct pairs?
select
distinct name1,
distinct name2, count from f group by name1
Given that you have a duplicate for each pair, you can just use:
select f.*
from f
where name1 < name2;
If you did not have the reverse in all cases, you can use:
select f.*
from f
where f.name1 < f.name2 or
not exists (select 1
from f f2
where f2.name1 = f.name2 and f2.name2 = f.name1
);

how to MYSQL compare values from same columns and join table

how do i compare 2 table in mysql?
with the following table
name table ok1
id model name
1 AAA AAA
2 BBB BBB
3 CCC CCC
name table ok2
idok nameok
1 ok1
2 ok2
3 ok3
and I want to generate a query
id name model nameok idmodel namemodel modelmodel okname
1 AAA AAA ok1 2 BBB BBB ok2
1 AAA AAA ok1 3 CCC CCC ok3
2 BBB BBB ok2 1 AAA AAA ok1
2 BBB BBB ok2 3 CCC CCC ok3
3 CCC CCC ok3 1 AAA AAA ok1
3 CCC CCC ok3 2 BBB BBB ok2
the following sql I made, the query does not match what I want
sql query :
SELECT
t1.id,
t1.name,
t1.model,
t3.nameok as nameok1,
t2.id AS idmodel,
t2.name AS namemodel,
t2.model AS modelmodel,
t3.nameok as nameok2
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.name <> t2.name AND
t1.model <> t2.model;
inner join yourTable t1
ON
t1.id = t3.idok and
t2.id = t3.idok
You can use joins:
select ok1.*, ok1_2.*, ok2.* -- or whatever columns you want
from ok1 join
ok1 ok1_2
on ok1.id <> ok1_2.id join
ok2
on ok2.id = ok1_2.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.

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>

Joining 3 Tables

I have 3 tables (CompanyProfile, IndustryTable, MainTable)
CompanyProfile
CompanyCode CompanyName IndustryCode
AAAA Company A 3
BBBB Company B 1
CCCC Company C 4
DDDD Company D 1
EEEE Company E 1
GGGG Company F 2
IndustryTable
IndustryCode IndustryName status
1 Manufacturing ACTIVE
2 Sales ACTIVE
3 Logistics ACTIVE
4 Energy DEACTIVATED
MainTable
CompanyCode field2
AAAAA SampleRecord1
AAAAA SampleRecord2
DDDDD SampleRecord3
CCCCC SampleRecord4
EEEEE SampleRecord5
Now I need a Query to Get all the RecordCount from MainTable Group By IndustryTable.IndustryCode (ACTIVE Only), So the above example should give the following output
IndustryCode IndustryName RecordCount (Explanation)
1 Manufacturing 2 -Record 3 and 5
2 Sales 0
3 Logistics 2 -Record 1 and 2
Record 4 (CCCCC) will not be show since IndustryTable.IndustryCode 4 is DEACTIVATED
Something like
select industrycode, industryname, count(*) as total from companyprofile t1 join industry table t2 join maintable t3 where t1.companycode=t3.companycode and t1.industrycode=t2.industrycode and t2.status="ACTIVE" group by industrycode
?