FredTheDog FredTheDog - 2 months ago 8
SQL Question

SUM of values ignoring GROUP BY - SQL SERVER 2008

Have written a query to pull data and SUM the area values

SELECT
rtrim(fmb0.reg_code) as Region,
RTRIM(FMB0.COUNTRY) AS COUNTRY,
RTRIM(FMB0.BLDGCODE) AS BLDGCODE,
(CASE WHEN FMB0.BLDGSTATUS = '' THEN 'RECORD'
ELSE FMB0.BLDGSTATUS END) AS SOURCE,
(CASE
WHEN FMA0.SPACETYPE = 'U-TLS-AS' THEN round(SUM(fma0.AREA),3)
ELSE round(SUM(fma0.USABLE),3) END) as [CHARGEABLE AREA],
(CASE
WHEN len(rtrim(fma0.group_))<1 THEN '**Unassigned**'
ELSE RTRIM(FMA0.GROUP_) END) AS [COST CODE],
(CASE
WHEN len(rtrim(fma0.group_))<1 THEN '**Unassigned Space**'
WHEN len(rtrim(fma0.group_))>1 AND rtrim(FMG0.GROUPDESC) IS NULL THEN '**Inactive Cost Code**'
ELSE RTRIM(FMG0.GROUPDESC) END) AS [COST CENTRE]

FROM FMA0

LEFT JOIN FMB0 ON FMA0.BLDGCODE = FMB0.BLDGCODE
LEFT JOIN FMG0 ON FMA0.GROUP_ = FMG0.GROUP_

GROUP BY FMB0.BLDGCODE, FMA0.FLOORCODE, FMA0.GROUP_, FMG0.GROUPDESC, FMG0.GROUPDESC3, FMB0.BLDGSTATUS, FMB0.COUNTRY, FMB0.REG_CODE,FMA0.SPACETYPE


Becuase I have to use SPACETYPE to determine which field to take the AREA from I need to include SPACETYPE in the GROUP BY clause. This means I see results split out by SPACETYPE even though I am not directly requesting it in the SELECT statement.

Region COUNTRY BLDGCODE SOURCE FLOORCODE CHARGEABLE AREA COST CODE COST CENTRE
ASP Hong Kong BLDG01 CAD 28 39.016 **Unassigned** **Unassigned Space**
ASP Hong Kong BLDG01 CAD 28 1217.146 **Unassigned** **Unassigned Space**
ASP Hong Kong BLDG01 CAD 28 1506.181 **Unassigned** **Unassigned Space**
ASP Hong Kong BLDG01 CAD 28 458.765 **Unassigned** **Unassigned Space**
ASP Hong Kong BLDG01 CAD 28 1000.711 **Unassigned** **Unassigned Space**


How do I get one line for all the area of all SPACETYPES in a particular BLDGCODE split by COST CODE

Answer

try using a Common Table Expression to get the details then aggregate:

WITH CTE
AS
(
   SELECT rtrim(fmb0.reg_code) as Region,
          RTRIM(FMB0.COUNTRY) AS COUNTRY,
          RTRIM(FMB0.BLDGCODE) AS BLDGCODE,
          (CASE WHEN FMB0.BLDGSTATUS = '' THEN 'RECORD'
                ELSE FMB0.BLDGSTATUS END) AS SOURCE,
          (CASE WHEN FMA0.SPACETYPE = 'U-TLS-AS' THEN round(fma0.AREA,3)
                ELSE round(fma0.USABLE,3) END) as [CHARGEABLE AREA],
          (CASE WHEN len(rtrim(fma0.group_))<1 THEN '**Unassigned**'  
                ELSE RTRIM(FMA0.GROUP_) END) AS [COST CODE], 
          (CASE WHEN len(rtrim(fma0.group_))<1 THEN '**Unassigned Space**' 
                WHEN len(rtrim(fma0.group_))>1 
                   AND rtrim(FMG0.GROUPDESC) IS NULL THEN '**Inactive Cost Code**' 
                ELSE RTRIM(FMG0.GROUPDESC) END) AS [COST CENTRE]
  FROM FMA0 
  LEFT JOIN FMB0 ON FMA0.BLDGCODE = FMB0.BLDGCODE
  LEFT JOIN FMG0 ON FMA0.GROUP_ = FMG0.GROUP_
)
SELECT Region, Country, BLDGCODE, Source, 
       (SUM([CHARGEABLE AREA]) AS [Chargeable Area], [COST CODE],[COST CENTRE]
FROM CTE
GROUP BY BLDGCODE, [Cost Code], [Cost Centre], Source, COUNTRY, Region