Programming language used to interact with SQL Server databases
Hi @Zhu
You may consider using Grouping Sets like this:
SELECT CustomerID
,ISNULL(CAST(ProductID AS VARCHAR(10)),'Total') AS ProductID
,SUM(OrderAmount) AS TotalAmount
FROM Orders
GROUP BY GROUPING SETS ((CustomerID,ProductID)
,(CustomerID))
Best regards,
Cosmog
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".