Reputation: 180
I am trying to produce an aggregated output table using aggregates from two different tables. I am unclear on how to join the two outcomes. The two tables, one listing all products in each store, the other the price variation for each product are as presented below.
| product_id | daily_price | date |
|------------|-------------|------------|
| 1 | 1.25$ | 01-01-2000 |
| 1 | ... | ... |
| 1 | 1$ | 31-12-2000 |
| 2 | 4.5$ | 01-01-2000 |
| 2 | ... | ... |
| 2 | 4.25$ | 31-12-2000 |
| store_id | product_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 3 |
| 3 | 2 |
The first aggregation gets the average daily price (it varies) of all products.
SELECT product_id, ROUND((AVG(price)),2) as average_price FROM product_dailyprices
GROUP BY product_id;
| product_id | average_price |
|------------|---------------|
| 1 | 50 |
| 2 | 100 |
| 3 | 250 |
The second query gets me the number of different products available in each store
SELECT store, COUNT(product_id) as product_count FROM products
GROUP BY store;
| store_id | product_count |
|----------|---------------|
| 1 | 200 |
| 2 | 250 |
| 3 | 225 |
I am a bit lost on how to perform a query to produce the following:
| store_id | product_count | average_price_at_store |
|----------|---------------|------------------------|
| 1 | 34 | 6.51$ |
| 2 | 45 | 3.23$ |
| 3 | 36 | 5.37$ |
Thanks for the help!
Upvotes: 0
Views: 29
Reputation: 15663
As you did not provide an SQL for the tables, lets use the following bare bone structure:
CREATE TABLE products
(
id SERIAL NOT NULL,
name text NOT NULL,
CONSTRAINT products_pk PRIMARY KEY (id)
);
CREATE TABLE stores
(
id SERIAL NOT NULL,
name text NOT NULL,
CONSTRAINT stores_pk PRIMARY KEY (id)
);
CREATE TABLE daily_prices
(
product_id INTEGER NOT NULL,
daily_price DOUBLE PRECISION NOT NULL,
date timestamptz,
CONSTRAINT daily_prices_product FOREIGN KEY (product_id) REFERENCES products (id)
);
CREATE TABLE locations
(
store_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
CONSTRAINT products_product_fk FOREIGN KEY (product_id) REFERENCES products (id),
CONSTRAINT products_store_fk FOREIGN KEY (store_id) REFERENCES stores (id)
);
And let enter some sample data to help use verify tthat the query works:
INSERT INTO products(name)
VALUES ('product 1');
INSERT INTO products(name)
VALUES ('product 2');
INSERT INTO products(name)
VALUES ('product 3');
INSERT INTO stores(name)
VALUES ('store 1');
INSERT INTO stores(name)
VALUES ('store 2');
insert into locations (store_id, product_id)
values (1, 1),
(1, 2),
(2, 2),
(2, 3);
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (1, 2.0, '01-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (1, 4.0, '02-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (2, 3.0, '01-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (2, 5.0, '02-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (3, 10.0, '01-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (3, 20.0, '02-01-2020');
Then the query to produce your desired table would look like:
select l.store_id as store_id,
count(distinct l.product_id) as number_of_products,
avg(dp.daily_price) as average_price
from locations l
join daily_prices dp on dp.product_id = l.product_id
group by l.store_id;
And we can manually verify that it calculated the expected result:
+--------+------------------+-------------+
|store_id|number_of_products|average_price|
+--------+------------------+-------------+
|1 |2 |3.5 |
|2 |2 |9.5 |
+--------+------------------+-------------+
Upvotes: 1