I have an orders table that stores orders from multiple sites within our company. In the table we have the fields userid, ordernumber and sitename. What I’d like to be able to get is the number of users who have orded from 2 or more of our sites.
This is what I started with:
SELECT
o.ordernumber,
o.sitename,
o.userdbid,
o2.sitename,
o2.userdbid,
o2.ordernumber
FROM
orders o
INNER JOIN
orders o2
ON
o.userdbid = o2.userdbid
WHERE
o.sitename != o2.sitename
ORDER BY
o.userdbid;
This isn’t close to correct but this as close as I could think to get. Any help or direction would be very much appreciated.
You will need a
HAVINGclause to compare the aggregateCOUNT()to 2, wherein you have grouped byuserbid. Be sure to useDISTINCTin your aggregateCOUNT()so that you get multiple sites rather than just multiple orders.