Below are the list data.
Code ItemCount Type Amount
----------------------------------------
B001 1 Dell 10.00
B001 1 Dell 10.00
B001 1 Apple 10.00
B001 2 Apple 20.00
B001 2 Apple 20.00
B114 1 Apple 30.50
B114 1 Apple 10.00
I need a result to group by code and by type and total the ItemCount
and get the grand total of the Amount
in every row.
Is this possible?
Code ItemCount Type Amount
----------------------------------------
B001 2 Dell 20.00
B001 5 Apple 50.00
B114 2 Apple 40.50
You can try this query:
This will give you proper result. You need to group by
Code
andType
and notItemCount
Please try:
You can try this simpler solution:
Understanding the 'group by' would come handy
This looks like homework.
(I swear I thought this was tagged as MySQL when I first looked at the question, but the title clearly shows MS SQL)
For MySQL,this query will return the specified resultset:For other databases, remove For MySQL the backticks from around the column aliases.If you need to preserve case, for Oracle, identifiers are enclosed in doublequotes. For SQL Server, identifiers are enclosed in square brackets. For MySQL identifiers are enclosed in backticks.
If my understanding is correct that the actual total of each row is the product of the itemcount and the amount then you can use the code below. If not use @Abu's code.
The amounts you give differ from the sample data but this works for the sample data values: