Sql sum up based on parent table

53 views Asked by At

My sql looks like:


create table ad(
  ad_id int,
  ad_name varchar(10)
);


insert into ad(ad_id, ad_name) values
  (1,'ad1'),
  (2,'ad2'),
  (3,'ad3');
 
 
 create table ad_insight(
   id int,
   ad_id int,
   date date, 
   clicks int
 );


insert into ad_insight(id, ad_id, date, clicks) values
    (1, 1, '2021-04-25', 1),
    (2, 1, '2021-04-24', 4),
    (3, 1, '2021-04-23', 2),
    (4, 2, '2021-04-25', 6),
    (5, 2, '2021-03-03', 7);


create table product(
    product_id int,
    ad_id int,
    product_name varchar(10)
);

insert into product(product_id, ad_id, product_name) values
    (1, 1, 'prod1'),
    (2, 1, 'prod2'),
    (3, 2, 'prod3'),
    (4, 2, 'prod4');
    (1, 3, 'prod1');
    
create table product_insight(
    id int,
    product_id int, 
    sale int, 
    date date
);
insert into product_insight(id, product_id, sale, date) values
    (1, 1, 12, '2021-04-25'),
    (2, 1, 11, '2021-04-24'),
    (3, 1, 13, '2021-04-23'),
    (4, 1, 14, '2021-04-22'),
    (5, 1, 17, '2021-04-21'),
    (6, 1, 15, '2021-04-20'),
    (7, 1, 13, '2021-04-19'),
    (8, 2, 19, '2021-04-25');

Here you have fiddle

A quick explanation of schema: I have ads:

  1. each ad has insights, which tell us when a certain ad was active.
  2. each ad has products. Each product has product_insight which tells us how many sales that product generated on a certain day.

And now I want to get the following tables:

  1. which will sum up clicks from ad_insight table and sum up product_sale from product_insight in 2021-04-23 to 2021-04-25 inclusive.
+----------+--------+--------------+--------------+
| ad_name  | clicks | product_sale |   products   |
+----------+--------+--------------+--------------+
| ad1      |      7 |           55 | prod1, prod2 |
| ad2      |      6 |            0 | prod3, prod4 |
| ad3      |      0 |           36 | prod1        |
+----------+--------+--------------+--------------+
  1. The summary row which will sum up everything in the above table:
+------------+--------------+--------------------+----------------------------+
| total_ads  | total_clicks | total_product_sale |    unique_all_products     |
+------------+--------------+--------------------+----------------------------+
|          3 |            13|                 91 | prod1, prod2, prod3, prod4 |
+------------+--------------+--------------------+----------------------------+

What I have tried?

# 1) table
select ad_name, SUM(ad_insight.clicks) as clicks 
from ad 
left join ad_insight on ad.ad_id = ad_insight.ad_id 
where ad_insight.date >= '2021-04-23' and ad_insight.date <= '2021-04-25'
group by ad.ad_id;

# 2) table
select count(distinct ad_insight.ad_id) as total,  SUM(ad_insight.clicks) as clicks
from ad_insight
left join ad on ad.ad_id = ad_insight.ad_id
where ad_insight.date >= '2021-04-23' and ad_insight.date <= '2021-04-25'

But I do not know how select product_sale table and products separated by comma!

1

There are 1 answers

3
Gordon Linoff On

If I understand correctly, you want to aggregate along two different dimensions (clicks and sales) for each ad. Aggregate before joining:

select ad.ad_name, ai.clicks, p.sales, p.products
from ad left join
     (select ai.ad_id, sum(ai.clicks) as clicks
      from ad_insight ai
      where ai.date >= '2021-04-23' and ai.date <= '2021-04-25'
      group by ai.ad_id
     ) ai
     on ad.ad_id = ai.ad_id left join
     (select p.ad_id, sum(pi.sale) as sales,
             group_concat(distinct p.product_name) as products
      from product p join
           product_insight pi
           on pi.product_id = p.product_id 
      where pi.date >= '2021-04-23' and pi.date <= '2021-04-25'
      group by p.ad_id
     ) p
     on p.ad_id = ad.ad_id ;

The second query just aggregates this again.