I was given a requirement where i have to find probability for a specific user ordering specific item in future based on his purchases in previous months so that inventory can be stocked.
Tables:
CREATE TABLE customers (
c_id INT PRIMARY KEY,
c_name VARCHAR(100),
);
CREATE TABLE products (
p_id INT PRIMARY KEY,
p_name VARCHAR(100),
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
c_id INT,
p_id INT,
order_date TIMESTAMP,
FOREIGN KEY (c_id) REFERENCES customers(c_id),
FOREIGN KEY (p_id) REFERENCES products(p_id)
);
I firstly tried to join tables in following way,
SELECT
c.c_id,
p.p_id,
COALESCE(lmo.num_orders_last_month, 0) AS num_orders_last_month,
COALESCE(to.total_orders, 0) AS total_orders
FROM
customers c
CROSS JOIN
products p
LEFT JOIN
(SELECT
o.c_id,
o.p_id,
COUNT(*) AS num_orders_last_month
FROM
orders o
WHERE
o.order_date >= TIMESTAMPADD(MONTH, -1, CURRENT_TIMESTAMP)
AND o.order_date < CURRENT_TIMESTAMP
GROUP BY
o.c_id, o.p_id) lmo ON c.c_id = lmo.c_id AND p.p_id = lmo.p_id
LEFT JOIN
(SELECT
o.c_id,
o.p_id,
COUNT(*) AS total_orders
FROM
orders o
GROUP BY
o.c_id, o.p_id) to ON c.c_id = to.c_id AND p.p_id = to.p_id
WHERE
c.c_id = 14;
I joined tables in above mentioned way, kindly verify if this is correct or not in grid db?
Also, please provide solution for probability prediction from data extracted. give sql query for extracting probability in grid db.