I need to create a VIEW that shows a TOP 3 best selling products in June 2022, the total number of products sold in the month, the total profits and all customers who bought the specific product in June 2022. For now I tried with this:
CREATE VIEW INVOICE_VIEW AS
SELECT TOP 3 P.ProductName,
SUM(F.Quantity) AS TotalQty,
SUM(F.Price) AS TotalProfit,
C.Name + ' ' + C.LastName AS Customers
FROM PRODUCT P
INNER JOIN INVOICE F
ON P.idProduct = F.Det_Products
LEFT JOIN CUSTOMER C
ON F.Num_Invoice = C.Num_Invoice
WHERE F.Date BETWEEN '20220601' AND '20220630'
GROUP BY ProductName, Name, LastName
ORDER BY TotalProfit ASC
Using SELECT * FROM INVOICE_VIEW
creates this:
ProductName | TotalQty | TotalProfit | Customers |
---|---|---|---|
Cheetos | 10 | 3 | Andrea G. |
Ruffles | 15 | 7.5 | Kevin T. |
DeTodito | 55 | 33 | NULL |
As you can see, it shows most of what I require, but in the last row it shows NULL instead of the Customer's Name. And that happens whenever a product has more than one customer. I would like to display something like this:
ProductName | TotalQty | TotalProfit | Customers |
---|---|---|---|
Cheetos | 10 | 3 | Andrea G. |
Ruffles | 15 | 7.5 | Kevin T. |
DeTodito | 55 | 33 | Carlos Z., Robin C. |
It should show the name of all customers/clients, which could be more than 1, in the same cell. Or at least something like this:
ProductName | TotalQty | TotalProfit | Customer1 | Customer2 | Customer3 |
---|---|---|---|---|---|
Cheetos | 10 | 3 | Andrea G. | NULL or EMPTY | NULL or EMPTY |
Ruffles | 15 | 7.5 | Kevin T. | NULL or EMPTY | NULL or EMPTY |
DeTodito | 55 | 33 | Carlos Z. | Robin C. | NULL or EMPTY |