SQL query to find the most efficient data

I want to write a query to find the most efficient strings. I have the following tables:

Sellers
Id   Name
1    Mark
2    Julia
3    Peter

Stocks
 Id   SellerId   ProductCode   StockCount
 1       1         30A            10
 2       2         20A             4
 3       1         20A             2
 4       3         42B             3

And there sqlfiddle http://sqlfiddle.com/#!6/fe5b1/1/0

My intention is to find the best Seller for stocks.

If the customer wants 30A, 20A and 42B. I need to go back to Mark and Peter because Mark has both products (30A and 20A), so Julia is not needed there.

How can I solve this in sql?

+5
source share
2 answers

I tried using temporary tables

SELECT
  s.SellerId,
  ProductList = STUFF((
                       SELECT ',' + ProductCode FROM Stocks
                        WHERE s.SellerId = Stocks.SellerId
                        ORDER BY ProductCode FOR XML PATH('')
                       )
                      , 1, 1, ''), COUNT(*) AS numberOfProducts
INTO #tmptable
FROM
  Stocks s
WHERE
  s.ProductCode IN ('30A','20A','42B')
  AND s.StockData > 0
GROUP BY s.SellerId;

/*this second temp table is necessary, so we can delete from one of them*/
SELECT * INTO #tmptable2 FROM #tmptable; 

DELETE t1 FROM #tmptable t1
WHERE EXISTS (SELECT 1 FROM #tmptable2 t2
               WHERE t1.SellerId != t2.SellerId
                 AND t2.ProductList LIKE '%' + t1.ProductList + '%'
                 AND t2.numberOfProducts > t1.numberOfProducts)
;

SELECT Name FROM #tmptable t INNER JOIN Sellers ON t.SellerId = Sellers.Id;

UPDATE:

Try static tables:

CREATE TABLE tmptable (SellerId int, ProductList nvarchar(max), numberOfProducts int);

for tmpTable2. Then change the code above to

INSERT INTO tmpTable
SELECT
  s.SellerId,
  ProductList = STUFF((
                       SELECT ',' + ProductCode FROM Stocks
                        WHERE s.SellerId = Stocks.SellerId
                        ORDER BY ProductCode FOR XML PATH('')
                       )
                      , 1, 1, ''), COUNT(*) AS numberOfProducts
FROM
  Stocks s
WHERE
  s.ProductCode IN ('30A','20A','42B')
  AND s.StockData > 0
GROUP BY s.SellerId;

INSERT INTO tmpTable2 SELECT * FROM tmpTable;

DELETE t1 FROM tmptable t1
WHERE EXISTS (SELECT 1 FROM tmptable2 t2
               WHERE t1.SellerId != t2.SellerId
                 AND t2.ProductList LIKE '%' + t1.ProductList + '%'
                 AND t2.numberOfProducts > t1.numberOfProducts)
;

SELECT * FROM tmpTable;
DROP TABLE tmpTable, tmpTable2;
+3
source

I think this might be what you are looking for?

Select name,sum(stockdata) as stockdata from sellers s1 join Stocks s2 on s1.id=s2.sellerid
where ProductCode in ('30A','20A','42B')
group by name
order by sum(stockdata) desc

Hope this helps.

2 ppl.

Select top 2 name,sum(stockdata) as stockdata from sellers s1 join Stocks s2 on s1.id=s2.sellerid
where ProductCode in ('30A','20A','42B')
group by name
order by sum(stockdata) desc

, , , , , , ?

-2

All Articles