Changes between Version 4 and Version 5 of AdvancedDatabaseDevelopment


Ignore:
Timestamp:
06/05/26 14:03:07 (7 days ago)
Author:
232012
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedDatabaseDevelopment

    v4 v5  
    4040
    4141{{{#!div style="text-align: justify; width: 100%;"
    42 Combines the entire metadata of a music release into one detailed overview. For any given release, it aggregates its main artists, features, tracks, total track duration, and provides a real-time summary breakdown of available warehouse stock and pricing for all physical formats simultaneously.
     42**Description:** Combines the entire metadata of a music release into one detailed overview. For any given release, it aggregates its main artists, features, tracks, total track duration, and provides a real-time summary breakdown of available warehouse stock and pricing for all physical formats simultaneously.
     43}}}
     44**Implementation:
     45{{{
     46DROP VIEW IF EXISTS project.detailed_release_view;
     47
     48CREATE VIEW project.detailed_release_view AS
     49WITH main_artists AS (
     50    SELECT
     51        ra.release_id,
     52        string_agg(a.artist_name, ', ' ORDER BY ra.release_ordinal) AS main_artists
     53    FROM project.release_artists ra
     54    JOIN project.artists a ON a.artist_id = ra.artist_id
     55    WHERE ra.type = 'MAIN'
     56    GROUP BY ra.release_id
     57),
     58album_tracks AS (
     59    SELECT
     60        al.release_id,
     61        jsonb_agg(
     62            jsonb_build_object(
     63                'song_name', s.song_name,
     64                'duration', s.song_duration,
     65                'features', COALESCE(sf.features, ' ')
     66            )
     67            ORDER BY s.song_id
     68        ) AS tracks,
     69        SUM(
     70            split_part(s.song_duration, ':', 1)::INT * INTERVAL '1 minute'
     71            + split_part(s.song_duration, ':', 2)::INT * INTERVAL '1 second'
     72        ) AS total_duration
     73    FROM project.albums al
     74    JOIN project.album_songs als ON als.album_id = al.release_id
     75    JOIN project.songs s ON s.song_id = als.song_id
     76    LEFT JOIN (
     77        SELECT
     78            sa.song_id,
     79            string_agg(a.artist_name, ', ' ORDER BY sa.song_ordinal) AS features
     80        FROM project.song_artists sa
     81        JOIN project.artists a ON a.artist_id = sa.artist_id
     82        WHERE sa.song_ordinal > 1
     83        GROUP BY sa.song_id
     84    ) sf ON sf.song_id = s.song_id
     85    GROUP BY al.release_id
     86),
     87single_tracks AS (
     88    SELECT
     89        sr.release_id,
     90        jsonb_build_array(
     91            jsonb_build_object(
     92                'song_name', r.title,
     93                'duration', sr.duration,
     94                'features', COALESCE(sf.features, 'No features')
     95            )
     96        ) AS tracks,
     97        split_part(sr.duration, ':', 1)::INT * INTERVAL '1 minute'
     98        + split_part(sr.duration, ':', 2)::INT * INTERVAL '1 second' AS total_duration
     99    FROM project.single_releases sr
     100    JOIN project.releases r ON r.release_id = sr.release_id
     101    LEFT JOIN (
     102        SELECT
     103            ra.release_id,
     104            string_agg(a.artist_name, ', ' ORDER BY ra.release_ordinal) AS features
     105        FROM project.release_artists ra
     106        JOIN project.artists a ON a.artist_id = ra.artist_id
     107        WHERE ra.type = 'FEATURE'
     108        GROUP BY ra.release_id
     109    ) sf ON sf.release_id = sr.release_id
     110),
     111product_summary AS (
     112    SELECT
     113        p.release_id,
     114        jsonb_agg(
     115            jsonb_build_object(
     116                'format', p.format,
     117                'price', p.price,
     118                'stock', p.stock,
     119                'description', p.product_description
     120            )
     121            ORDER BY p.format
     122        ) AS physical_formats
     123    FROM project.products p
     124    GROUP BY p.release_id
     125)
     126SELECT
     127    r.title,
     128    r.record_label,
     129    r.genre,
     130    r.release_date,
     131    r.cover_photo,
     132    CASE
     133        WHEN al.release_id IS NOT NULL THEN 'ALBUM'
     134        ELSE 'SINGLE'
     135    END AS release_type,
     136    ma.main_artists,
     137    COALESCE(at.tracks, st.tracks) AS tracks,
     138    COALESCE(at.total_duration, st.total_duration) AS total_track_duration,
     139    ps.physical_formats
     140FROM project.releases r
     141LEFT JOIN project.albums al ON al.release_id = r.release_id
     142LEFT JOIN main_artists ma ON ma.release_id = r.release_id
     143LEFT JOIN album_tracks at ON at.release_id = r.release_id
     144LEFT JOIN single_tracks st ON st.release_id = r.release_id
     145LEFT JOIN product_summary ps ON ps.release_id = r.release_id;
    43146}}}
    44147
     
    46149
    47150{{{#!div style="text-align: justify; width: 100%;"
    48 A detailed analytical view that uses a the previous 7-day period to show the Top 10 best-selling products. It combines data from multiple tables, calculates the total quantity sold, excludes cancelled orders, and sorts the products from highest to lowest selling.
     151**Description:** A detailed analytical view that uses a the previous 7-day period to show the Top 10 best-selling products. It combines data from multiple tables, calculates the total quantity sold, excludes cancelled orders, and sorts the products from highest to lowest selling.
    49152}}}
     153**Implementation:**
     154{{{
     155DROP VIEW IF EXISTS project.top_10_highest_selling_products;
     156
     157CREATE VIEW project.top_10_highest_selling_products AS
     158SELECT
     159    p.format,
     160    p.product_description,
     161    r.title AS release_title,
     162    r.genre,
     163    r.record_label,
     164    r.release_date,
     165    SUM(op.quantity) AS total_quantity_sold,
     166    SUM(op.quantity * op.price_at_purchase) AS total_revenue
     167FROM project.order_products op
     168JOIN project.orders o ON o.order_id = op.order_id
     169JOIN project.products p ON p.product_id = op.product_id
     170JOIN project.releases r ON r.release_id = p.release_id
     171WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '7 days'
     172  AND o.purchase_date < CURRENT_DATE
     173  AND o.status <> 'CANCELLED'
     174GROUP BY
     175    p.product_id,
     176    p.format,
     177    p.product_description,
     178    r.title,
     179    r.genre,
     180    r.record_label,
     181    r.release_date
     182ORDER BY total_quantity_sold DESC, total_revenue DESC
     183LIMIT 10;
     184}}}