I have the data like this
I want below result.
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityTry this.
Select [Commodity], [Business Entities], [Quantity]
from MyTable
UNION ALL
SELECT [Commodity], 'TOTAL', SUM([Quantity])
from MyTable
group by [Commodity]
order by [Commodity]
You can use GROUPING SETS
for this
SELECT
Commodity,
[Business Entities] = CASE WHEN GROUPING([Business Entities]) = 1 THEN 'Total' ELSE [Business Entities] END,
Quantity = SUM(Quantity)
FROM YourTabe
GROUP BY GROUPING SETS (
(Commodity, [Business Entities]),
(Commodity)
)
ORDER BY
[Commodity],
GROUPING([Business Entities]),
[Business Entities];
The GROUPING
function tells us if a column has been grouped or not.
Quantity
?...such a thing is generally not possible except for in an adhoc query. What is your table definitions for this dataset?