How to write an SQL query that subtracts INNER JOIN results from LEFT JOIN results?

This is an example: I want to see how good my marketing efforts are for a product that I am trying to sell in a store. For example, I want to know how many people bought my product within a month after they received a coupon for it in their email 12/1/2014, compared with how many people bought my product for the same period of time, not after receiving the coupon. Here is a sample table of my client:

CUSTOMER_NUMBER        PURCHASE_DATE
---------------        -------------
1                      2014-12-02
2                      2014-12-05 
3                      2014-12-05 
4                      2014-12-10 
5                      2014-12-21 

Here is an example of my email table

CUSTOMER_NUMBER        EMAIL_ADDR         SEND_DATE
---------------        ------------       ----------
1                      john@abc.com       2014-12-01
3                      mary@xyz.com       2014-12-01
5                      beth@def.com       2014-12-01

, , : . , , , - ( , ..), , , , . , . 2 5 , , , . IBM Netezza DB. !

+4
3

Left Outer Join NULL check

SELECT C.*
FROM   customer C
       LEFT OUTER JOIN email e
                    ON C.customer_Number = E.Custmer_Number
WHERE  E.customer_Number IS NULL

Not Exists

SELECT *
FROM   customer C
WHERE  NOT EXISTS (SELECT 1
                   FROM   email e
                   WHERE  c.customer_number = e.customer_number) 
+4
select from customers c left outer join email e
on c.customer_number = e.customer_number
    where e.customer_number is null
        or C.purchase_date < e.send_date
0
SELECT
   C.*,
   PurchasedWithin30DaysOfEmailedCoupon =   
      CASE WHEN EXISTS (
         SELECT *
         FROM
            Email E
         WHERE
            C.CustomerID = E.CustomerID
            AND C.Purchase_Date <= E.Send_Date
            AND E.Send_Date < DateAdd(day, 30, C.Purchase_Date)
      ) THEN 1 ELSE 0 END
FROM
   Customer C
WHERE
   C.PurchaseDate IS NOT NULL
;

Please forgive me for not knowing the correct syntax for adding 30 days to a date in your DBMS. I am sure this will be a simple fix for you.

Then you can simply group the value PurchasedWithin30DaysOfEmailedCouponand get your score.

0
source

All Articles