Changes between Version 4 and Version 5 of AdvancedReports


Ignore:
Timestamp:
01/23/26 20:19:41 (7 days ago)
Author:
231136
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReports

    v4 v5  
    146146
    147147
    148 == 4. Детален извештај за најпопуларниот артист според месечни слушања за изминатата година
     148=== 4. Детален извештај за најпопуларниот артист според месечни слушања за изминатата година
    149149{{{
    150150SET search_path TO 'project';
     
    204204ORDER BY m.month_num
    205205}}}
     206
     207
     208=== 5. Детален извештај за процентуалната промена на бројот на слушања на артистите за изминатите 30 дена
     209
     210{{{
     211SET search_path TO 'project';
     212
     213WITH one_month AS (
     214    SELECT
     215        CURRENT_DATE - INTERVAL '60 days' AS prev_period_start,
     216        CURRENT_DATE - INTERVAL '30 days' AS prev_period_end,
     217        CURRENT_DATE - INTERVAL '29 days' AS this_period_start,
     218        CURRENT_DATE AS this_period_end
     219),
     220last_period AS (
     221    SELECT
     222        a.user_id,
     223        count(l.timestamp) AS last_period_count
     224    FROM listens l
     225    JOIN one_month om ON l.timestamp between om.prev_period_start AND om.prev_period_end
     226    JOIN musical_entities me ON l.song_id = me.id
     227    RIGHT JOIN artists a ON a.user_id = me.released_by
     228    GROUP BY a.user_id
     229    HAVING count(l.timestamp) > 0
     230),
     231this_period AS (
     232    SELECT
     233        a.user_id,
     234        count(l.timestamp) AS this_period_count
     235    FROM listens l
     236    JOIN one_month om ON l.timestamp between om.this_period_start AND om.this_period_end
     237    JOIN musical_entities me ON l.song_id = me.id
     238    right JOIN artists a ON a.user_id = me.released_by
     239    GROUP BY a.user_id
     240),
     241stats AS (
     242    SELECT
     243        u.full_name,
     244        CONCAT(om.prev_period_start::DATE, ' | ',om.prev_period_end::DATE) AS previous_period,
     245        last_period_count,
     246        CONCAT(om.this_period_start::DATE, ' | ',om.this_period_end::DATE) AS this_period,
     247        this_period_count,
     248        ROUND(((CAST(this_period_count AS DOUBLE PRECISION) - last_period_count) / NULLIF(last_period_count, 0)) * 100) AS pct_change
     249    FROM last_period lp
     250    LEFT JOIN this_period tp ON lp.user_id=tp.user_id
     251    JOIN users u ON lp.user_id = u.user_id
     252    CROSS JOIN one_month om
     253)
     254SELECT
     255    s.full_name,
     256    s.previous_period,
     257    s.last_period_count,
     258    s.this_period,
     259    s.this_period_count,
     260    CONCAT(s.pct_change, '%')
     261FROM stats s
     262ORDER BY pct_change DESC
     263
     264}}}