Hi, I am trying to resolve a problem using SQL and I am getting stuck.
Here is the situation
I have two fields
Item ID Seller
10345 Marketplace
10345. Independent
10345 Others
10223 Independent
10224 Marketplace
Here Item ID is ID for an Item sold at a store and Seller is the type of Seller for this Item.
We have 3 Sellers - Marketplace, Independent and Others.
An Item can have all 3 types of sellers or 2 of them or just one.
The question is, If an item is sold by all the 3 types of sellers or 2 types of sellers I want it be consolidated saying 'Shared' else if an Item has just 1 seller type return the Seller Type.
i am doing this for my stats , my timing , my performance ( cognition , meta cognition etc etc )
my tracking for myself .. my noodles
it may help performance wise
; with cte as
( select Item_ID , min(seller) as mins , max(seller ) as maxs from #MyTable group by Item_ID )
select
Item_ID
, case when mins = maxs then Mins else 'Shared' end
from
cte
Just want to advice another possible solution. Using STRING_AGG you can return All Sellers in one row
SELECT ItemID, STRING_AGG(Seller, ',') WITHIN GROUP (ORDER BY Seller)
FROM Sales
GROUP BY ItemID
-- Look live example below
https://sqlize.online/sql/mssql2019/92bcfdca37bac02ce71f1e269529cba8/
Hi Thanks for the solution .
I have a question,apart from trying to avoid the case function to be grouped finally, why are you using max function here where as we can just return the seller type ?