SELECT 
  A.AttributeID, 
  ElementID, 
  0 ArrayIndex, 
  ValidFor, 
  NULL StringValue, 
  FALSE IsDefault, 
  CASE A.AttributeID WHEN 122517 THEN ROUND(A_122516 / A_122515, 2) WHEN 123387 THEN A_1891150 END wrongNumberOfDecimals, 
  ROUND(A_122516 / A_122515, 2) as correctNumberOfDecimals
FROM 
  (
    SELECT 
      ElementID, 
      ValidFor, 
      0 K, 
      MAX(
        CASE WHEN AttributeID = 122516 THEN Value END
      ) A_122516, 
      IFNULL(
        MIN(
          CASE WHEN AttributeID = 122516 THEN IsDefault END
        ), 
        -1
      ) D_122516, 
      MAX(
        CASE WHEN AttributeID = 122515 THEN Value END
      ) A_122515, 
      IFNULL(
        MIN(
          CASE WHEN AttributeID = 122515 THEN IsDefault END
        ), 
        -1
      ) D_122515, 
      MAX(
        CASE WHEN AttributeID = 1891150 THEN Value END
      ) A_1891150, 
      IFNULL(
        MIN(
          CASE WHEN AttributeID = 1891150 THEN IsDefault END
        ), 
        -1
      ) D_1891150 
    FROM 
      (
        SELECT 
          AttributeID, 
          ElementID, 
          ValidFor, 
          CASE WHEN AttributeID IN (122515) THEN CT_ECIOAccessCount WHEN AttributeID IN (122516) THEN CT_ECIOAccessSum END Value, 
          FALSE IsDefault 
        FROM 
          (
            SELECT 
              ElementID, 
              TIMESTAMP(Date) ValidFor, 
              SUM(CT_ECIOAccessCount) CT_ECIOAccessCount, 
              SUM(CT_ECIOAccessSum) CT_ECIOAccessSum, 
              0 K 
            FROM 
              UMTS_ALL_SECTOR_HOURLY 
            WHERE 
              ElementID IN (26805) 
              AND Date = DATE('2014-04-19') 
            GROUP BY 
              ElementID, 
              ValidFor
          ) V 
          JOIN (
            SELECT 
              122515 AttributeID, 
              0 K 
            UNION ALL 
            SELECT 
              122516, 
              0
          ) A ON V.K = A.K 
        HAVING 
          Value IS NOT NULL 
        UNION ALL 
        SELECT 
          AttributeID, 
          ElementID, 
          ValidFor, 
          SUM(Count) Value, 
          FALSE IsDefault 
        FROM 
          (
            SELECT 
              AttributeID, 
              ElementID, 
              ValidFor, 
              DimCount Count, 
              DimValue Value 
            FROM 
              (
                SELECT 
                  ElementID, 
                  TIMESTAMP(Date) ValidFor, 
                  HistType, 
                  DimValue, 
                  SUM(DimCount) DimCount 
                FROM 
                  UMTS_HIST_SECTOR_HOURLY 
                WHERE 
                  ElementID IN (26805) 
                  AND Date = DATE('2014-04-19') 
                  AND (
                    HistType IN ('CT_PropDelay', 'CT_RSCPValues')
                  ) 
                GROUP BY 
                  ElementID, 
                  ValidFor, 
                  HistType, 
                  DimValue
              ) V 
              JOIN (
                SELECT 
                  1891150 AttributeID, 
                  'CT_PropDelay' HistType 
                UNION ALL 
                SELECT 
                  1891147, 
                  'CT_RSCPValues' 
                UNION ALL 
                SELECT 
                  1891147, 
                  'CT_RSCPValues'
              ) A ON V.HistType = A.HistType 
            HAVING 
              Value IS NOT NULL
          ) T 
        GROUP BY 
          AttributeID, 
          ElementID, 
          ValidFor
      ) V 
    GROUP BY 
      ElementID, 
      ValidFor
  ) P 
  JOIN (
    SELECT 
      123387 AttributeID, 
      0 K 
    UNION ALL 
    SELECT 
      122517, 
      0 
    UNION ALL 
    SELECT 
      122920, 
      0 
    UNION ALL 
    SELECT 
      1892901, 
      0
  ) A ON P.K = A.K 
WHERE 
  CASE A.AttributeID WHEN 122517 THEN GREATEST(D_122515, D_122516) WHEN 123387 THEN D_1891150 END > -1;
