0

I wasted all the day on one query without success , SOS I need a help :) with a given @CustomerId , I need to query all the Products that linked to customer seller can sell but not sold to him before , the Commissions table is indication of what products seller can sell

enter image description here

Thanks in advance

5
  • How do you know which products a seller can sell? By the commissions table perhaps? Commented Sep 8, 2014 at 16:33
  • I can query it from Commissions table with SellerId Commented Sep 8, 2014 at 16:34
  • @DimaRudaev: You are connecting customer and product through sales table? how you determine whether product been sold or not? Commented Sep 8, 2014 at 16:35
  • a "Products" table name is bad choice , you can consider it like "Categories" Commented Sep 8, 2014 at 16:38
  • each record in Sales table indicates a sale , customer can purchase only 1 of each products Commented Sep 8, 2014 at 16:43

3 Answers 3

2
SELECT sellableProduct
FROM (SELECT Comissions.ProductId AS sellableProduct, Sellers.SellerId AS sellableSeller FROM Comissions INNER JOIN Sellers ON Comissions.SellerId=Sellers.SellerId INNER JOIN Customers ON Sellers.SellerId=Customers.SellerId WHERE Customers.CustomerId = @customerid) AS tblSellable
LEFT JOIN (SELECT Sales.ProductId AS soldProduct, Customers.SellerId as soldSeller FROM Customers INNER JOIN Sales ON Customers.CustomerId=Sales.CustomerId WHERE Customers.CustomerId = @customerid) AS tblSold
ON tblSellable.sellableProduct=tblSold.soldProduct AND tblSellable.sellableSeller=tblSold.soldSeller
WHERE tblSold.soldProduct IS NULL

this way you avoid time-consuming IN statements

If a Customer can only have one Seller, then you can omit the seller link:

SELECT sellableProduct
FROM (SELECT Comissions.ProductId AS sellableProduct FROM Comissions INNER JOIN Sellers ON Comissions.SellerId=Sellers.SellerId INNER JOIN Customers ON Sellers.SellerId=Customers.SellerId WHERE Customers.CustomerId = @customerid) AS tblSellable
LEFT JOIN (SELECT Sales.ProductId AS soldProduct FROM Sales WHERE Sales.CustomerId = @customerid) AS tblSold
ON tblSellable.sellableProduct=tblSold.soldProduct
WHERE tblSold.soldProduct IS NULL
Sign up to request clarification or add additional context in comments.

4 Comments

I edited the solution, nevertheless you're welcome to find the word "customers" yourself...
It works , thank you very much i really appreciated it , it is very complicated query !!! You are a SQL-NINJA - RESPECT
i need to enhance it a little bit , your query return ProductId's i want to JOIN them with Products.Name and Products.Description , does it possible somehow to add a "JOIN Products ON ProductId" to this query ?
yes that's possible. In FROM (SELECT add the additional columns and the inner join. Also add the columns in the SELECT at the beginning.
1

Basically, you're looking for products that have a record in commissions, but not in sales. Using :id to denote the specific ID:

SELECT *
FROM   products
WHERE  productid IN (SELECT productid
                     FROM   commissions
                     WHERE  sellerid = :id) AND
       productid NOT IN (SELECT productid
                         FROM   sales
                         JOIN   customers ON sales.customerid = cusomers.customerid
                         WHERE  sellerid = :id)

5 Comments

you mean that i need to query associated SellerId to given customer and then run your query with :id = SellerId ?
"sales.sellerid" there is no sellerid field in sales Table
arg, wasn't paying attention. Fixed.
let me understand , you tell me to query it by SellerId that i can query it from Customer table with @CustomerId ??
your query answers to question "which products a seller can sell that he didn't sold before" and i need an answer to question "which products a seller can sell that he didn't sold before to specific customer @CustomerId" , thanks anyway
0

Would this work?

SELECT sell.*, prod.* FROM 
Sellers sell
INNER JOIN Customers cust ON cust.SellerId = sell.SellerId
LEFT JOIN Commissions comm ON sell.SellerId = comm.SellerId
LEFT JOIN Products prod ON prod.ProductId = comm.ProductId
WHERE prod.ProductId NOT IN (
SELECT ProductId
FROM Products p INNER JOIN 
Sales s ON s.ProductId = p.ProductId
WHERE s.CustomerId = @CustomerId

It get all the sellers and the respective product from commission, where the product Id is NOT associated with any sale of the client

1 Comment

syntax error - Line 7 Ambiguous column name 'ProductId', also only 1 seller associated with each customer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.