Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Aggregation von Daten

Dies ist die Antwort auf Ihre Frage:

SELECT
   program_id, 
   reporting_day, 
   CAST((CAST(total AS FLOAT)/CAST(total_seconds AS FLOAT)*100*weight) AS DECIMAL(5,2))
     AS viewing_percentage
   FROM table_b
        OUTER APPLY (
             SELECT
                  SUM(seconds_viewed) AS total
                  FROM table_a
                  WHERE table_a.program_id = table_b.program_id
                  AND table_a.viewing_day <= table_b.reporting_day
         ) AS t

Die von mir verwendete Umgebung ist Microsoft SQL Server 2014