   NormalisedCpuDuration BIGINT UNSIGNED NOT NULL,
   NumberOfJobs BIGINT UNSIGNED NOT NULL,
 
-  PRIMARY KEY (SiteID, Month, Year, GlobalUserNameID, VOID, VORoleID, VOGroupID, 
-               SubmitHostId, Infrastructure, NodeCount, Processors)
+  PRIMARY KEY (SiteID, Month, Year, GlobalUserNameID, VOID, VORoleID, VOGroupID,
+               SubmitHostId, Infrastructure, ServiceLevelType, ServiceLevel,
+               NodeCount, Processors)
 );
 
+
 DROP PROCEDURE IF EXISTS SummariseJobs;
+
 DELIMITER //
 CREATE PROCEDURE SummariseJobs()
 BEGIN
-    REPLACE INTO NormalisedSuperSummaries(SiteID, Month, Year, GlobalUserNameID, VOID, 
-        VOGroupID, VORoleID, SubmitHostID, Infrastructure, 
-        NodeCount, Processors, EarliestEndTime, 
-        LatestEndTime, WallDuration, CpuDuration, NormalisedWallDuration, NormalisedCpuDuration,
-	NumberOfJobs)
-    SELECT SiteID, 
-    EndMonth AS Month, EndYear AS Year, 
-        GlobalUserNameID, VOID, VOGroupID, VORoleID, SubmitHostID, InfrastructureType, 
-        NodeCount, Processors,
-    MIN(EndTime) AS EarliestEndTime, 
-    MAX(EndTime) AS LatestEndTime,
-    SUM(WallDuration) AS SumWCT, 
-    SUM(CpuDuration) AS SumCPU, 
-    ROUND(SUM(IF(WallDuration > 0, WallDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250))) AS NormSumWCT, 
-    ROUND(SUM(IF(CpuDuration > 0, CpuDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250))) AS NormSumCPU, 
-    COUNT(*) AS Njobs
-    FROM JobRecords 
-    GROUP BY SiteID, VOID, GlobalUserNameID, VOGroupID, VORoleID, EndYear, EndMonth, InfrastructureType, 
-             SubmitHostID, NodeCount, Processors;
-END //
-DELIMITER ;
+  REPLACE INTO HybridSuperSummaries(SiteID, Month, Year, GlobalUserNameID, VOID,
+    VOGroupID, VORoleID, SubmitHostID, Infrastructure, ServiceLevelType,
+    ServiceLevel, NodeCount, Processors, EarliestEndTime, LatestEndTime,
+    WallDuration, CpuDuration, NormalisedWallDuration, NormalisedCpuDuration,
+    NumberOfJobs)
+  SELECT SiteID,
+         EndMonth AS Month,
+         EndYear AS Year,
+         GlobalUserNameID,
+         VOID,
+         VOGroupID,
+         VORoleID,
+         SubmitHostID,
+         InfrastructureType,
+         ServiceLevelType,
+         ServiceLevel,
+         NodeCount,
+         Processors,
+         MIN(EndTime) AS EarliestEndTime,
+         MAX(EndTime) AS LatestEndTime,
+         SUM(WallDuration) AS SumWCT,
+         SUM(CpuDuration) AS SumCPU,
+         ROUND(SUM(IF(WallDuration > 0, WallDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250))) AS NormSumWCT,
+         ROUND(SUM(IF(CpuDuration > 0, CpuDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250))) AS NormSumCPU,
+         COUNT(*) AS Njobs
+  FROM JobRecords
+  GROUP BY SiteID, VOID, GlobalUserNameID, VOGroupID, VORoleID, EndYear,
+           EndMonth, InfrastructureType, SubmitHostID, ServiceLevelType,
+           ServiceLevel, NodeCount, Processors;
+END //
+DELIMITER ;
+
 
 DROP PROCEDURE IF EXISTS NormaliseSummaries;
+
 DELIMITER //
 CREATE PROCEDURE NormaliseSummaries()
 BEGIN
-    REPLACE INTO NormalisedSuperSummaries(SiteID, Month, Year, GlobalUserNameID, VOID, 
-        VOGroupID, VORoleID, SubmitHostID, Infrastructure, 
-        NodeCount, Processors, EarliestEndTime, 
-        LatestEndTime, WallDuration, CpuDuration, NormalisedWallDuration, NormalisedCpuDuration,
-	NumberOfJobs)
-    SELECT SiteID, 
-    Month, Year, GlobalUserNameID, VOID, VOGroupID, VORoleID, SubmitHostID, InfrastructureType, 
-        NodeCount, Processors, EarliestEndTime, LatestEndTime, WallDuration, CpuDuration, 
-    ROUND(SUM(IF(WallDuration > 0, WallDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250))) AS NormSumWCT, 
-    ROUND(SUM(IF(CpuDuration > 0, CpuDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250))) AS NormSumCPU, 
-    NumberOfJobs
-    FROM Summaries
-    GROUP BY SiteID, VOID, GlobalUserNameID, VOGroupID, VORoleID, Year, Month, InfrastructureType, 
-             SubmitHostID, NodeCount, Processors;
-END //
-DELIMITER ;
-
-
-
-DROP PROCEDURE IF EXISTS CopySummaries;
-DELIMITER //
-CREATE PROCEDURE CopySummaries()
-BEGIN
-    REPLACE INTO NormalisedSuperSummaries(SiteID, Month, Year, GlobalUserNameID, VOID,
-        VOGroupID, VORoleID, SubmitHostID, Infrastructure, 
-        NodeCount, Processors, EarliestEndTime, LatestEndTime, WallDuration,
-        CpuDuration, NormalisedWallDuration, NormalisedCpuDuration, NumberOfJobs)
-    SELECT SiteID, Month, Year, GlobalUserNameID, VOID,
-        VOGroupID, VORoleID, SubmitHostID, Infrastructure, 
-        NodeCount, Processors, EarliestEndTime, LatestEndTime, WallDuration,
-        CpuDuration, NormalisedWallDuration, NormalisedCpuDuration, NumberOfJobs
-    FROM NormalisedSummaries;
+  REPLACE INTO HybridSuperSummaries(SiteID, Month, Year, GlobalUserNameID, VOID,
+    VOGroupID, VORoleID, SubmitHostID, Infrastructure, ServiceLevelType,
+    ServiceLevel, NodeCount, Processors, EarliestEndTime, LatestEndTime,
+    WallDuration, CpuDuration, NormalisedWallDuration, NormalisedCpuDuration,
+    NumberOfJobs)
+  SELECT SiteID,
+         Month,
+         Year,
+         GlobalUserNameID,
+         VOID,
+         VOGroupID,
+         VORoleID,
+         SubmitHostID,
+         InfrastructureType,
+         ServiceLevelType,
+         ServiceLevel,
+         NodeCount,
+         Processors,
+         EarliestEndTime,
+         LatestEndTime,
+         WallDuration,
+         CpuDuration,
+         ROUND(IF(WallDuration > 0, WallDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250)) AS NormSumWCT,
+         ROUND(IF(CpuDuration > 0, CpuDuration, 0) * IF(ServiceLevelType = "HEPSPEC", ServiceLevel, ServiceLevel / 250)) AS NormSumCPU,
+         NumberOfJobs
+  FROM Summaries
+END //
+DELIMITER ;
+
+
+DROP PROCEDURE IF EXISTS CopyNormalisedSummaries;
+
+DELIMITER //
+CREATE PROCEDURE CopyNormalisedSummaries()
+BEGIN
+  REPLACE INTO HybridSuperSummaries(SiteID, Month, Year, GlobalUserNameID, VOID,
+    VOGroupID, VORoleID, SubmitHostID, Infrastructure, NodeCount, Processors,
+    EarliestEndTime, LatestEndTime, WallDuration, CpuDuration,
+    NormalisedWallDuration, NormalisedCpuDuration, NumberOfJobs)
+  SELECT SiteID,
+        Month,
+        Year,
+        GlobalUserNameID,
+        VOID,
+        VOGroupID,
+        VORoleID,
+        SubmitHostID,
+        Infrastructure,
+        NodeCount,
+        Processors,
+        EarliestEndTime,
+        LatestEndTime,
+        WallDuration,
+        CpuDuration,
+        NormalisedWallDuration,
+        NormalisedCpuDuration,
+        NumberOfJobs
+  FROM NormalisedSummaries;
 END //
 DELIMITER ;
 

        WallDuration, 
        CpuDuration, 
        NumberOfJobs
    FROM SuperSummaries, 
         Sites site, 
         DNs userdn, 
         VORoles vorole, 

        AND VOID = vos.id
        AND VOGroupID = vogroup.id
        AND SubmitHostID = submithost.id;


-- -----------------------------------------------------------------------------
-- View on HybridSuperSummaries
-- This view excludes the ServiceLevelType and ServiceLevel fields.

DROP VIEW IF EXISTS VNormalisedSuperSummaries;

CREATE VIEW VNormalisedSuperSummaries AS
  SELECT UpdateTime,
         site.name Site,
         Month,
         Year,
         userdn.name GlobalUserName,
         vos.name VO,
         vogroup.name VOGroup,
         vorole.name VORole,
         submithost.name SubmitHost,
         Infrastructure,
         NodeCount,
         Processors,
         EarliestEndTime,
         LatestEndTime,
         SUM(WallDuration),
         SUM(CpuDuration),
         SUM(NormalisedWallDuration),
         SUM(NormalisedCpuDuration),
         NumberOfJobs
  FROM HybridSuperSummaries,
       Sites AS site,
       DNs AS userdn,
       VORoles AS vorole,
       VOs AS vos,
       VOGroups AS vogroup,
       SubmitHosts AS submithost
  WHERE SiteID = site.id
    AND GlobalUserNameID = userdn.id
    AND VORoleID = vorole.id
    AND VOID = vos.id
    AND VOGroupID = vogroup.id
    AND SubmitHostID = submithost.id
  GROUP BY SiteID, Month, Year, GlobalUserNameID, VOID, VORoleID, VOGroupID,
           SubmitHostId, Infrastructure, NodeCount, Processors;


-- -----------------------------------------------------------------------------
-- View on NormalisedSuperSummaries
-- useful form of data from NormalisedSuperSummaries
