Question

I am trying to figure out the best way to run this query. I basically need to return a list of "login" fields (the login field is basically a customer id / key) of customers who have only placed one order in our system.

Some background on our system...

Multiple orders placed by a customer on the same date is the equalivant of one order. The reason being is we have multiple vendors, anytime someone orders our shopping cart system will split that single order up into multiple orders - 1 order per unique vendor in that order.

Here is the basic mysql query I've come up with so far..

SELECT  
xcart_orders.login,
COUNT(xcart_orders.login) as c1,
FROM_UNIXTIME(xcart_orders.date, '%Y-%m-%d') as d1

 FROM
xcart_orders

 WHERE
INSTR(xcart_orders.email, "marketplace") <= 0

 GROUP BY
xcart_orders.login,
FROM_UNIXTIME(xcart_orders.date, '%Y-%m-%d')

ORDER BY
xcart_orders.login
ASC

Here is a sample of the data returned...

login       c1      d1
01crouse    1   2007-11-30
022336010   2   2009-06-24
022336010   1   2009-08-28
02emzach    1   2011-08-25
02emzach    1   2011-10-18

This is returning me a nice list of customers, unique order counts per date

the problem is - now I need to group this result set and only return customers who had 1 unique record in this result set - i'm not quite sure how to do this

so if you see the sample result set above - I need to return 01crouse out of that list of logins - as that login only has 1 record, 1 order ever, where as the others have multiple orders. I hope this makes sense - please let me know if you have any questions at all

Important Note on using Having

I was recommened to use:

HAVING c1 = 1

Unfortunately Having will not work - the reason being is because c1 can be greater than 1 for the case to be true. Remember, its giving me an order count PER day in the result - thus - if someone places 1 order with 3 vendors in that order all on the same day it will result in 3 orders for that one day - however - that is ONE order they placed - these types of people would be excluded by using HAVING

this is why i need to group and then count the result set - for example i would perform another group on the resultset by the login field and then do a count on login - any record that returns a 1 on this count would be accurate - the problem is i'm not sure how to do a group on this result set

Was it helpful?

Solution

You can do that with another SELECT (outside of your original SELECT), like this:

SELECT login, c1, d1, count(login) as c2 
FROM (...YOUR SELECT...) as orig
GROUP BY login
HAVING c2 = 1

This would group your original SELECT by the login and then show only those logins that are represented there only once, so only those users who have shopped on only one day.

OTHER TIPS

See MySQL's HAVING clause. You are already grouping by the login so you should be able to simply add HAVING COUNT(login) = 1

If the query got more complex, you can always nest this query within another and do another group by, no guarantees on good performance when you do that though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top