I am trying to return a query that will only show Customer orders where this is the first order that the customer has placed.
All orders are held in the SALESTABLE along with the CUSTACCOUNT field that represents the corresponding customer.
I need to select all records where the CUSTACCOUNT field is unique in the table. i.e. I don't want to see any customers that have had multiple orders with us already.
For example here are some customer accounts:
CUST1
CUST2
CUST2
CUST2
CUST3
From the above I only want to see CUST1 and CUST3 as they only appear in the list once.
SELECT DISTINCT CUSTACCOUNTFROM dbo.SALESTABLEORDER BY CUSTACCOUNT
Thanks narlasridhar,
That is giving me the resuilt I need. I did forget to mention that I do need to add in a few other colums with stuff such as the Sales Order number etc. but when I add these columns I can see customer accounts that are not unique within the list. Here is your code with the extra columns I added in.
SELECT DISTINCT CUSTACCOUNT, SALESID, FEB_DESPATCHSTATUSFROM dbo.SALESTABLEGROUP BY CUSTACCOUNT, SALESID, FEB_DESPATCHSTATUSHAVING (COUNT(CUSTACCOUNT) = 1)ORDER BY CUSTACCOUNT
Open in new window