I'm developing a new analyst feature for an internal tool my company will (hopefully if I do well) use.
For simplicity sake, let's say we have
CREATE TABLE Products (
ProductID varchar,
Description varchar,
....
);
and
CREATE TABLE Orders (
ProductID varchar,
Bought date,
Returned date,
....
);
The tables would look something like this:
Products
| ProductID | Description |
|---|---|
| SPO00 | Sports product 1 |
| SPO01 | Sports product 2 |
| SPO02 | Sports product 3 |
| ELE00 | Electronics product 1 |
| ELE02 | Electronics product 2 |
Orders
| ProductID | Bought | Returned |
|---|---|---|
| ELE00 | 2021-01-05 | 2021-01-07 |
| SPO00 | 2021-01-01 | NULL |
| SPO00 | 2021-01-05 | 2021-01-08 |
| SPO00 | 2021-01-08 | NULL |
| SPO01 | 2021-01-10 | NULL |
| SPO01 | 2021-01-15 | NULL |
| SPO02 | 2021-01-18 | 2021-01-20 |
I'd like to make a request to our DB and retrieve the description of specific products, and the percentage of bought products that are eventually returned.
I'd would also like to add specific parameters to the query, for example select only orders from beginning of the year as well as only the products from a specific department, for example.
So, it would looks something like this:
| Description | ratio returned |
|---|---|
| Sports product 1 | 0.33 |
| Sports product 2 | 0.00 |
| Sports product 3 | 1.0 |
So, the products table might have product lines of electronics and sports and ProductID would be ELE00-ELE05 and SPO00-SPO03, respectively.
The above table is grabbing all products that have ProductID with SPO prefix and getting that specific products bought and returned ratio.
I've only been able to get the specific products, but the returned ratio is the same for each row. I think because its not doing the ratio calculation for each distinct product. I think its doing one overall ratio calculation and displaying that for each product.
Here is the query I've tried.
SELECT DISTINCT Product.Description, (CAST((SELECT DISTINCT COUNT(*) FROM Orders WHERE(ProductID like 'SPO%' AND Returned > '2021-01-01') AS FLOAT)) / (CAST((SELECT DISTINCT COUNT(*) FROM Orders WHERE (ProductID like 'SPO%' AND Bought > '2021-01-01') AS FLOAT)) AS returnedRatio
FROM Product INNER JOIN
Orders ON Orders.ProductID = Product.ProductID
I'm thinking I might need to do a nested query to get the ratios for each product and then get the description?
All help would be greatly appreciated because I've never done very complex queries so I'm still learning.