I have created a query that join two tables and returns a date of the last log in of a particular user. If a user logged in multiple times, the query returns the most recent date:
CREATE TABLE Table1 (
USERID varchar(255),
NAME varchar(255)
)
CREATE TABLE Table2 (
USERID varchar(255),
LOGIN_TIME varchar(255)
)
INSERT INTO Table1(USERID, NAME)
VALUES ('XYZ_1234', 'RAMBO'),
('XYZ_1235', 'ARNOLD'),
('XYZ_1236', 'BRUCE')
INSERT INTO Table2(USERID,LOGIN_TIME)
VALUES ('1234','17.07.17'),
('1234','20.07.17'),
('1235', '27.08.17')
SELECT T1.NAME, MAX (T2.LOGIN_TIME)
CASE WHEN T2.LOGIN_TIME IS NULL THEN 5 ELSE T2.LOGIN_TIME
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.USERID='XYZ_' + T2.USERID
GROUP BY T1.NAME
http://www.sqlfiddle.com/#!18/c119d/2
What I'm struggling with is to replace "null" values that are returned every time when a user exist in table1 and does not exist in table2. For that I was trying to write a "CASE" function (line 2 in the link above).