Question : can't select distinct records with case statement

I have two tables, a customer table with unique records, and an orders table linked to the customer table by the customer id and therefore containing mutliple customer orders.  I am trying to select all the customers from the customer table who placed an order yesterday and assign the an 'A'  to a field in the output, and assign a 'U' if the last edit date in the customers table is from yesterday but they did NOT place an order.  The problem is that I am getting all those having placed an order and the fieldoutput with 'A' but am getting them with a 'U' as well.

Here's the code:

select distinct customers.customer_id,

case when orders.order_date > getdate()-2 and
then 'A'
else 'U'
end as 'flag'

FROM         customers LEFT OUTER JOIN
                      orders ON customers.CUSTOMER_ID = orders.CUSTOMER_ID

where CAST(SUBSTRING(customers.last_edit, CHARINDEX(',', customers.last_edit) + 1, 8) AS datetime)> getdate()-2

order by customers.customer_id


Answer : can't select distinct records with case statement

I found a solution.  I assign the 'U' to all the edited records, and then update with 'A'.

here iit is:

delete from sf_cust_billing

insert into sf_cust_billing SELECT distinct customers.customer_id, first_name,last_name, company_name,address_line1,address_line2,
city,state,zipcode,country,email1,
'U' as 'flag'




FROM         customers LEFT OUTER JOIN
                      orders ON customers.CUSTOMER_ID = orders.CUSTOMER_ID

where CAST(SUBSTRING(customers.last_edit, CHARINDEX(',', customers.last_edit) + 1, 8) AS datetime)> getdate()-2

order by customers.customer_id

update sf_cust_billing

set flag = 'A'

FROM         SF_cust_billing LEFT OUTER JOIN
                      orders ON SF_cust_billing.CUSTOMER_ID = orders.CUSTOMER_ID

where order_date > getdate()-2


It may not be elegant, but since the query is driopping everything from the destination table first, it works just as well and is fast.
Random Solutions  
 
programming4us programming4us