Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SUM Function using fields accross tables

Author  Topic 

matta0990
Starting Member

44 Posts

Posted - 2011-09-07 : 04:19:37
Hi, i'm trying to SUM up the combined results from a field. BUT the field is based on a number of diffrent INNER JOINS from 11 tables. My query returns the results i want e.g.

NAME LineType Volume

BIOMASS BUNKER 1 OpeningBalance 100.00
BIOMASS BUNKER 1 Consumption
BIOMASS BUNKER 1 InterimBalance1
BIOMASS BUNKER 1 TotalReceipts
BIOMASS BUNKER 1 InterimBalance2
BIOMASS BUNKER 1 TotalSales
BIOMASS BUNKER 1 TotalTransfers
BIOMASS BUNKER 1 InterimBalance3
BIOMASS BUNKER 1 TotalReclaim
BIOMASS BUNKER 1 InterimBalance4
BIOMASS BUNKER 1 TotalConsumption
BIOMASS BUNKER 1 ClosingBalance
BIOMASS BUNKER 2 OpeningBalance 2000.000
BIOMASS BUNKER 2 Consumption
BIOMASS BUNKER 2 InterimBalance1
BIOMASS BUNKER 2 TotalReceipts
BIOMASS BUNKER 2 InterimBalance2
BIOMASS BUNKER 2 TotalSales
BIOMASS BUNKER 2 TotalTransfers
BIOMASS BUNKER 2 InterimBalance3
BIOMASS BUNKER 2 TotalReclaim
BIOMASS BUNKER 2 InterimBalance4
BIOMASS BUNKER 2 TotalConsumption
BIOMASS BUNKER 2 ClosingBalance
BIOMASS BUNKER 3 OpeningBalance 500.00
BIOMASS BUNKER 3 Consumption
BIOMASS BUNKER 3 InterimBalance1
BIOMASS BUNKER 3 TotalReceipts
BIOMASS BUNKER 3 InterimBalance2
BIOMASS BUNKER 3 TotalSales
BIOMASS BUNKER 3 TotalTransfers
BIOMASS BUNKER 3 InterimBalance3
BIOMASS BUNKER 3 TotalReclaim
BIOMASS BUNKER 3 InterimBalance4
BIOMASS BUNKER 3 TotalConsumption
BIOMASS BUNKER 3 ClosingBalance
BIOMASS BUNKER 4 OpeningBalance 4000.000
BIOMASS BUNKER 4 Consumption
BIOMASS BUNKER 4 InterimBalance1
BIOMASS BUNKER 4 TotalReceipts
BIOMASS BUNKER 4 InterimBalance2
BIOMASS BUNKER 4 TotalSales
BIOMASS BUNKER 4 TotalTransfers
BIOMASS BUNKER 4 InterimBalance3
BIOMASS BUNKER 4 TotalReclaim
BIOMASS BUNKER 4 InterimBalance4
BIOMASS BUNKER 4 TotalConsumption
BIOMASS BUNKER 4 ClosingBalance
BIOMASS BUNKER 5 OpeningBalance
BIOMASS BUNKER 5 Consumption
BIOMASS BUNKER 5 InterimBalance1
BIOMASS BUNKER 5 TotalReceipts
BIOMASS BUNKER 5 InterimBalance2
BIOMASS BUNKER 5 TotalSales
BIOMASS BUNKER 5 TotalTransfers
BIOMASS BUNKER 5 InterimBalance3
BIOMASS BUNKER 5 TotalReclaim
BIOMASS BUNKER 5 InterimBalance4
BIOMASS BUNKER 5 TotalConsumption
BIOMASS BUNKER 5 ClosingBalance
BIOMASS BUNKER 6 OpeningBalance
BIOMASS BUNKER 6 Consumption
BIOMASS BUNKER 6 InterimBalance1
BIOMASS BUNKER 6 TotalReceipts
BIOMASS BUNKER 6 InterimBalance2
BIOMASS BUNKER 6 TotalSales
BIOMASS BUNKER 6 TotalTransfers
BIOMASS BUNKER 6 InterimBalance3
BIOMASS BUNKER 6 TotalReclaim
BIOMASS BUNKER 6 InterimBalance4
BIOMASS BUNKER 6 TotalConsumption
BIOMASS BUNKER 6 ClosingBalance

ETC.....

rite, i need to sum up the combined total of VOLUME, for each BIOMASS BUNKER when the LINE TYPE is OpeningBalance. I carnt get it to work as the fileds, BIOMASS BUNKER 1,2,3 etc..., volume are fields within a table but OpeningBalance are results produced by the query.....

Any Ideas?

J Smith

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-07 : 04:23:45
do like

select NAME ,SUM(VOLUME) as totlvol
from
(
your current query
)t
where LineType ='OpeningBalance'
GROUP BY Name


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -