Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to index match with conditions in sql

I have tables like so

regist table

userID registDate
1 2022-01-22
2 2022-01-23

session tables

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

userID date_key traffic
null 2022-01-02 facebook
1 2021-01-03 facebook
1 2021-01-04 google
1 2021-01-05 linkedin
2 2021-01-15 facebook
2 2021-01-25 facebook
3 2021-01-20 facebook

output

userID date_key traffic regist date
1 2021-01-03 facebook 2022-01-22
1 2021-01-04 google 2022-01-22
1 2021-01-05 linkedin 2022-01-22
2 2021-01-15 facebook 2022-01-23

How do I merge the tables so that I can return the regist date. Do I do a right join?
Is this correct?

select * from
sessiontables st
left join registtable rt 
on st.userID=rt.userID
where st.userID is not null

how to do exist userID exist in regist table statement ?

>Solution :

if I understand correctly, You can try to use self join with an aggregate function.

select rt.userID,
       st.date_key,
       st.traffic,
       rt.registDate
from (
    SELECT userID,min(date_key) date_key,traffic
    FROM sessiontables
    GROUP BY traffic,userID
) st
JOIN registtable rt 
ON st.userID=rt.userID
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading