Hi so I have a question on concatenating a column. I have two tables.
Table 1: Sales Order-> OrderID, ProductsOnHold (Should list product(s) on hold)
Table 2: Products-> ProductID, OnHold (boolean)
- A Sales Order can have many products.
- A product can have a hold, which would make the Sales Order be put on hold if that order has that product.
So a sale order can be on hold for have 1 or more products on hold.
I was able to get one product to display if the order is on hold, but what if it has more products on hold - how could I display them as well? This is for a view I am creating.
This is what I have done so far:
(SELECT ProductName
FROM Products with (NOLOCK)
WHERE (OnHold = 1) AND (EXISTS
(SELECT CASE
WHEN (COUNT(DISTINCT Product)> 1) THEN (Product + ', ' + Product)
ELSE Product END AS ProductName
FROM SalesOrder WITH (NOLOCK)
GROUP BY OrderID ))) AS ProductsOnHold
Desired Output:
OrderID | ProductsOnHold
----------------------------------
1 | P1, P2, P7
2 |
3 | P1
4 | P1, P7, P8, P9, P15, P77
Anything I am missing in my sql query?