How to merge same values in a column and rows

All we need is an easy explanation of the problem, so here it is.

How can i merge same Stock item value and get the sum of both value in the table

This is Query:

select Invoice_TBL.InvoiceNo
    ,Invoice_TBL.ClientNo
    ,Invoice_TBL.ClientFullName
    ,InvoiceItems_TBL.StockID
    ,Stock_TBL.ItemCode
    ,Stock_TBL.ItemCategory
    ,Stock_TBL.ItemDescription
    ,Stock_TBL.Unit
    ,Stock_TBL.NoOfUnitsInACarton
    ,SUM(InvoiceItems_TBL.Quantity) as TotalQuantity
    ,InvoiceItems_TBL.SellingPrice
    ,SUM(InvoiceItems_TBL.TotalAmount) as TotalAmount
from Stock_TBL
inner join InvoiceItems_TBL on Stock_TBL.StockID = InvoiceItems_TBL.StockID
inner join Invoice_TBL on InvoiceItems_TBL.InvoiceNo = Invoice_TBL.InvoiceNo
where (Invoice_TBL.IsTableStatus = 1)
    and (InvoiceItems_TBL.IsTableStatus = 1)
    and (Stock_TBL.IsTableStatus = 1)
    and Invoice_TBL.ClientNo = 5 --AND (Invoice_TBL.ClientNo > 0)
group by Invoice_TBL.InvoiceNo
    ,Invoice_TBL.ClientNo
    ,Invoice_TBL.ClientFullName
    ,InvoiceItems_TBL.StockID
    ,Stock_TBL.ItemCode
    ,Stock_TBL.ItemCategory
    ,Stock_TBL.ItemDescription
    ,Stock_TBL.Unit
    ,Stock_TBL.NoOfUnitsInACarton
    ,InvoiceItems_TBL.SellingPrice
order by Invoice_TBL.ClientNo asc

The result is

enter image description here

But i need to display like this (Middle Table):

enter image description here

from the bottom table 402 Invoice No Merge with 335 Invoice No… i don’t need to display Invoice No column and stock id column… but Same Item values should be merge and Added (Total Quantity and Total amount).. i wanted, one client in each stock item how many took (total quantity and amount should be calculated) in his/her all invoices

Please Help me… Thank you

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Removal as suggested in comments above. Will this work for you?

select 
    Invoice_TBL.ClientFullName
    ,InvoiceItems_TBL.StockID
    ,Stock_TBL.ItemCode
    ,Stock_TBL.ItemCategory
    ,Stock_TBL.ItemDescription
    ,Stock_TBL.Unit
    ,Stock_TBL.NoOfUnitsInACarton
    ,SUM(InvoiceItems_TBL.Quantity) as TotalQuantity
    ,SUM(InvoiceItems_TBL.TotalAmount) as TotalAmount
from Stock_TBL
inner join InvoiceItems_TBL on Stock_TBL.StockID = InvoiceItems_TBL.StockID
inner join Invoice_TBL on InvoiceItems_TBL.InvoiceNo = Invoice_TBL.InvoiceNo
where (Invoice_TBL.IsTableStatus = 1)
    and (InvoiceItems_TBL.IsTableStatus = 1)
    and (Stock_TBL.IsTableStatus = 1)
    and Invoice_TBL.ClientNo = 5 --AND (Invoice_TBL.ClientNo > 0)
group by Invoice_TBL.ClientNo
    ,Invoice_TBL.ClientFullName
    ,InvoiceItems_TBL.StockID
    ,Stock_TBL.ItemCode
    ,Stock_TBL.ItemCategory
    ,Stock_TBL.ItemDescription
    ,Stock_TBL.Unit
    ,Stock_TBL.NoOfUnitsInACarton
order by Invoice_TBL.ClientNo asc

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply