Question : SQL Query Problem

I have the following SQL
1:
2:
3:
4:
5:
6:
7:
8:
SELECT     InvMovements.TrnYear, InvMovements.TrnMonth, InvMovements.StockCode, InvMaster.Description, InvMovements.Customer, InvMovements.TrnQty, 
                      InvMovements.TrnValue, InvMovements.CostValue, InvMaster.MaterialCost * InvMovements.TrnQty AS Material, 
                      InvMaster.LabourCost * InvMovements.TrnQty AS Labor, InvMaster.SubContractCost * InvMovements.TrnQty AS OSP, 
                      InvMaster.VariableOverhead * InvMovements.TrnQty AS VOH, InvMaster.FixOverhead * InvMovements.TrnQty AS FOH, 
                      InvMovements.TrnValue - InvMovements.CostValue AS Margin
FROM         InvMaster InvMaster INNER JOIN
                      InvMovements InvMovements ON InvMaster.StockCode = InvMovements.StockCode
WHERE     (InvMovements.MovementType = 'S') AND (InvMovements.TrnYear = $2010) AND (InvMovements.TrnMonth = $6)


Which brings back the data required but in many rows.
Is it possible to group the data so that it is grouped by StockCode with the calculated values all summed and only show the top 50 items with the greatest TrnValue ?

At the moment I get the following which I need on one row with the values added together.

YEAR    MONTH STOCKCODE            DESCRIPTION                    CUSTCODE     QTY  COST
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      3      99      0      57.59541      30.60567      0      46.83294      0      99
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      1      33      0      19.19847      10.20189      0      15.61098      0      33
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      2      66      0      38.39694      20.40378      0      31.22196      0      66
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      2      66      0      38.39694      20.40378      0      31.22196      0      66
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      4      132      0      76.79388      40.80756      0      62.44392      0      132
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      2      66      0      38.39694      20.40378      0      31.22196      0      66
2010      6      PD30740PLXB004      GA ENGINE COVER PLAIN      B-004      2      66      0      38.39694      20.40378      0      31.22196      0      66


Thanks

Answer : SQL Query Problem

correction

SELECT TOP 50 * FROM (
SELECT     InvMovements.TrnYear, InvMovements.TrnMonth, InvMovements.StockCode,
InvMaster.Description, InvMovements.Customer, sum(InvMovements.TrnQty) TrnQty,
Sum(InvMovements.TrnValue) TrnValue, Sum(InvMovements.CostValue) CostValue,
Sum(InvMaster.MaterialCost * InvMovements.TrnQty) AS Material,
Sum(InvMaster.LabourCost * InvMovements.TrnQty) AS Labor,
Sum(InvMaster.SubContractCost * InvMovements.TrnQty) AS OSP,
Sum(InvMaster.VariableOverhead * InvMovements.TrnQty) AS VOH,
Sum(InvMaster.FixOverhead * InvMovements.TrnQty) AS FOH,
sum(InvMovements.TrnValue - InvMovements.CostValue) AS Margin,
FROM         InvMaster InvMaster INNER JOIN
      InvMovements InvMovements ON InvMaster.StockCode = InvMovements.StockCode
WHERE     (InvMovements.MovementType = 'S') AND (InvMovements.TrnYear = 2010) AND (InvMovements.TrnMonth = 6)
GROUP BY InvMovements.TrnYear, InvMovements.TrnMonth, InvMovements.StockCode,
InvMaster.Description, InvMovements.Customer
) SQ
ORDER BY TrnValue desc
Random Solutions  
 
programming4us programming4us