Skip maximum timestamp and fetch others - mysql

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>

Related

Swapping contents of columns of same datatype from different tables in MySQL

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;

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?

Where clause alternative in SSRS

I've got my brain squeezed out trying to olve following issue. Hope you may help.
My datasource:
JrNo JrLineNo Employee LineDescription
-----------------------------------------------
1 1 BBB Target Description 01
1 2 AAA Target Description 02
2 1 BBB Target Description 03
2 2 DDD Target Description 04
2 3 AAA Target Description 05
2 4 CCC Target Description 06
2 5 DDD Target Description 07
3 1 AAA Target Description 08
3 2 AAA Target Description 09
3 3 BBB Target Description 10
4 1 AAA Target Description 11
4 2 DDD Target Description 12
4 3 AAA Target Description 13
4 4 CCC Target Description 14
I have created report which is grouped by Employees (AAA, BBB, CCC, DDD)
On each group footer I want to see Description field of "LineDescription" column, filtered by the highest journal number and highest journal line number (within that journal) for every Employee.
For example: The highest journal number in above table is 4, but the highest journal line number is 5 (in jounal 2). If I roughly take Max of JrNo and Max of JrLineNo, it simply will not give expected result.
Expected result:
AAA Target Description 13
BBB Target Description 10
CCC Target Description 14
DDD Target Description 12
If I'd use SQL my query would look like:
SELECT LineDescription
FROM table
WHERE JrNo = (SELECT Max(JrNo) from table WHERE Employee = 'AAA')
AND JrLineNo = (SELECT Max(JrLineNo) FROM table
WHERE JrNo = (SELECT Max(JrNo) from table WHERE Employee = 'AAA'))
But I need to develop this in ssrs Expression way. And Iif function does not help much.
Any ideas - how to build expression which selects only one record based on multiple value filters and subqueries? What is alternative of WHERE clause?

Getting only 1 record from each category for each page in pagination

My table structure is as follows
id int
name varchar 50
catid int
Sample data
id name catid
---------------------------------------------------------
1 AAA 1
2 BBB 1
3 CCC 1
4 DDD 2
5 EEE 2
6 FFF 1
7 GGG 2
8 HHH 2
9 III 1
I want query such as it get me 1 row from each category for each page in pagination
Now for 1st Page I need data as
id name catid
---------------------------------------------------------
1 AAA 1
4 DDD 2
Now for 2nd Page I need data as
id name catid
---------------------------------------------------------
2 BBB 1
5 EEE 2
Now for 3rd Page I need data as
id name catid
---------------------------------------------------------
3 CCC 1
7 GGG 2
and so on.
How can I achieve this.
SELECT * FROM table WHERE catid = 1 LIMIT :pageno 1
SELECT * FROM table WHERE catid = 2 LIMIT :pageno 1
I think, this will be simplest query to get required result.
This worked for me (MySQL 5.1 on Linux)
SELECT id, name, catid FROM
(SELECT id, name, catid FROM t WHERE catid=1 LIMIT 2,1) AS t1
UNION (SELECT id, name, catid FROM t WHERE catid=2 LIMIT 2,1)
ORDER BY catid
The 2 in the limit is the page number. Hence the output of this one is for the second page.
Yes, as Pradeep said, a LIMIT clause would be the simplest way to do it in MySQL. You'll still need a little bit of coding in PHP to produce clickable page numbers for your users.