1

table employee

log_number last_update_date user id
1 2024-12-11 10:49:18.087 121
2 2024-12-12 15:49:18.087 131
2 2024-12-17 16:49:18.087 131
select *
FROM   employee e1
where  e1.last_update_date IN(
         SELECT max(e2.last_update_date)
         FROM   fusion.employee e2
         WHERE  1=1
         AND    e2.user_id = e1.user_id
         AND    e1.log_number=2.  -- E1
       )
and    e1.user_id=131

this giving output as No Data Available

select *
FROM   employee e1
where  e1.last_update_date IN(
         SELECT max(e2.last_update_date)
         FROM   fusion.employee e2
         WHERE  1=1
         AND    e2.user_id = e1.user_id
         AND    e2.log_number=2      -- E2
       )
and e1.user_id=131

this one is giving the recored result that is

log_number last_update_date user id
2 2024-12-17 16:49:18.087 131

could someone explain how this discrepancy is happening as we are joining the sub query with user_id.

could someone explain how this discrepancy is happening as we are joining the sub query with user_id. but in one case is giving correct result and other case it is not working. along side how the main table(E1) and sub query(E2) table gets join?

5
  • Neither query finds any data - fiddle - since user 121 doesn't have a log 2 entry, whether you're looking for it in e1 or 2. Please provide a minimal reproducible example that demonstrates the behaviour you are seeing and confused about.
    – Alex Poole
    Commented Feb 10 at 17:07
  • by mistake it will be 131 Commented Feb 10 at 17:08
  • Both queries do find data for user 131 - fiddle - so again, please provide a minimal reproducible example demonstrating the problem.
    – Alex Poole
    Commented Feb 10 at 17:17
  • could you brief me about the diff happen due to E1 and E2 in the subquery? Commented Feb 10 at 17:28
  • As i said in the answer: The first query is looking for rows where the row with the latest date out of all the rows also has log_number=2. The second query is looking for the row that has the latest date out of the rows that have log_number=2.
    – MT0
    Commented Feb 10 at 18:47

1 Answer 1

1

The first query is looking for rows where the row with the latest date out of all the rows also has log_number=2 (effectively filtering after finding the maximum).

The first query could be, equivalently, be written as:

SELECT *
FROM   (
  SELECT e1.*,
         RANK() OVER (
           PARTITION BY e1.user_id
           ORDER BY e1.last_update_date DESC
         ) AS rnk
  FROM   employee e1
  WHERE  e1.user_id=131
)
WHERE  rnk = 1
AND    log_number=2

The second query is looking for the row that has the latest date out of the rows that have log_number=2 (effectively filtering before finding the maximum).

The second query could be, equivalently, be written as:

SELECT *
FROM   (
  SELECT e1.*,
         RANK() OVER (
           PARTITION BY e1.user_id
           ORDER BY e1.last_update_date DESC
         ) AS rnk
  FROM   employee e1
  WHERE  e1.user_id=131
  AND    e1.log_number=2
)
WHERE  rnk = 1

They are looking for different things.

fiddle

4
  • hi , i don't wanna change the logic of the sql, i want to know what difference comes due E1 and E2 change. and what kinda of join it made internally Commented Feb 10 at 17:26
  • @biswajeetSARKAR In the answer, I've told you what the difference is in the logic (one filters after finding the maximum and the other filters before finding the maximum) and I've given you equivalent SQL logic which shows the difference in the logic more clearly (while giving results identical to yours). I don't know what more you want because the answer already tells you what the difference is.
    – MT0
    Commented Feb 10 at 18:45
  • Thanks i missed the explanation part, issue is resolved. Commented Feb 11 at 4:20
  • @biswajeet SARKAR: Please accept this answer with the tick/check icon next to it. Thus we see that the request is closed. Commented Feb 11 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.