Learn
Learn

Reputation: 548

SQL select record right after a particular date, compare NULL with date

I like to keep all records in tableA that are right after my targeted date,

Main table A

enter image description here

Table B

enter image description here

SELECT *
FROM tableA a
LEFT JOIN tableB b on b.customerID  = a.customerID and b.target_date = a.sell_date
WHERE a.sell_date > b.target_date 

Unfortunately my code above doesn't work since SQL can't compare NULL with date.

My expected output is

enter image description here

Upvotes: 0

Views: 32

Answers (1)

SteveC
SteveC

Reputation: 6015

The inequality between target_date and sell_date could go in the join condition of the FROM clause. This way the WHERE clause could be eliminated.

SELECT *
FROM tableA a
     LEFT JOIN tableB b on b.customerID=a.customerID
                           and b.target_date <= a.sell_date;

Upvotes: 1

Related Questions