Good afternoon, please help create a SQL query for a sample of such a database of information only about those buyers who bought goods from more than half of the categories. My attempts end with Feil: (

SELECT [Customers].CompanyName FROM [Customers] WHERE [Customers].CustomerId IN ( SELECT [Orders].CustomerId FROM [Orders] INNER JOIN [Order Details] ON [Orders].OrderId = [Order Details].OrderId GROUP BY [Orders].CustomerId, [Order Details].ProductId WHERE COUNT([Order Details].ProductId) IN ( SELECT (COUNT([Categories].CategoryId))/2 FROM [Categories] ) )

123

    1 answer 1

    I can not vouch for 100%, there is no time to drive in data for verification, but syntactically my version is correct)

     SELECT C.COMPANYNAME FROM ORDERS O LEFT JOIN CUSTOMERS C ON C.CUSTOMERID = O.CUSTOMERID WHERE O.ORDERID IN ( SELECT O.ORDERID FROM ORDERS O LEFT JOIN ORDER_DETAILS OD ON OD.ORDERID = O.ORDERID LFET JOIN PRODUCTS P ON P.PRODUCTID = OD.PRODUCTID LEFT JOIN CATEGORIES C ON C.CATEGORYID = P.CATEGORYID GROUP BY O.ORDERID HAVING COUNT(DISTINCT P.CATEGORYID) > (SELECT COUNT(1) from CATEGORIES) / 2 ) 
    • Thank! I'll check - accomplish your goal :) - stck
    • Everything is correct, but the only moment - says that Orders is an invalid object :) - stck
    • Perhaps the Cyrillic symbol somewhere wormed. But with this, I think, you will cope. - Nofate ♦ pm
    • Yes, yes, all problems have already been solved :) Thank you once again - stck