Complex Query

From: The Magnet <art_at_unsu.com>
Date: Tue, 15 Feb 2011 07:32:04 -0800 (PST)
Message-ID: <eed5cb9b-f686-4ef9-8742-1358f680f00b_at_o14g2000prb.googlegroups.com>



Hi,

I am using this query. It returns information on existing orders the customer has, and ranks them based on the status.

We have 25 products defined in a separate table. Rather than having to keep doing a COUNT to check for a given product, I am hoping to somehow modify this query such that it returns a record for ALL products. If the customer does not have that product, it will give NULL values for the status column. right now I cannot find any way to do that join, I know it is some outer join.

It may be a WITH query, which I always have trouble creating, but maybe someone can help, I've been at this for a few days.

So, for a customer with 4 orders, this query returns 4 records. I'd like it to return 25 (total orders in products table) and just a NULL or 'N' for products the customer does not have.

SELECT email, customer_id, subproduct_id, description, status, rnum, status_rank
FROM (SELECT email, customer_id, subproduct_id, status, rnum, status_rank, description

           FROM (SELECT email, c.customer_id, subproduct_id, status, description,

                       CASE status
                        WHEN 'Active' THEN 1
                        WHEN 'Pending' THEN 2
                        WHEN 'Complete' THEN 3
                        WHEN 'Cancelled' THEN 4
                      END status_rank,
                         ROW_NUMBER() OVER (PARTITION BY subproduct_id
ORDER BY subproduct_id) rnum
                     FROM customer c, customer_account ca,
customer_address cd, customer_order_vw co
                     WHERE c.customer_id = ca.customer_id(+)
                          AND c.customer_id = cd.customer_id(+)
                          AND c.customer_id = co.customer_id(+)
                          AND c.customer_id = 312039939)
             ORDER BY status_rank, rnum)
WHERE rnum = 1; Received on Tue Feb 15 2011 - 09:32:04 CST

Original text of this message