PostgreSQL-用于确定在vs查看/添加到购物篮时购买产品的理想价格的查询
我目前正在学习一些课程,其中一个问题让我有些困扰。本质上,这个想法是创建一个虚拟数据仓库并创建一些有价值的查询。我的第一个问题是确定一个产品的理想价格,而不是用户是否只是查看它或将它添加到他们的购物篮中 下面是我对此的星型模式的图像,尽管对于我目前编写的查询,只有篮子(事实表),产品,产品交互,年,月和日期 这是我目前的查询:PostgreSQL-用于确定在vs查看/添加到购物篮时购买产品的理想价格的查询,postgresql,Postgresql,我目前正在学习一些课程,其中一个问题让我有些困扰。本质上,这个想法是创建一个虚拟数据仓库并创建一些有价值的查询。我的第一个问题是确定一个产品的理想价格,而不是用户是否只是查看它或将它添加到他们的购物篮中 下面是我对此的星型模式的图像,尽管对于我目前编写的查询,只有篮子(事实表),产品,产品交互,年,月和日期 这是我目前的查询: SELECT p.product_name, p.product_cost, b.product_session_price,
SELECT p.product_name,
p.product_cost,
b.product_session_price,
pi.interactiontype,
y.yearid,
m.month_name,
m.monthid
FROM product p
LEFT JOIN basket b
ON p.productid = b.productid
INNER JOIN product_interaction pi
ON b.interactionid = pi.interactionid
LEFT JOIN date d
ON b.dateid = d.dateid
LEFT JOIN year y
ON d.year = y.yearid
LEFT JOIN month m
ON d.month = m.monthid
WHERE d.month BETWEEN 1 AND 12
GROUP BY pi.interactiontype,
p.product_cost,
b.product_session_price,
p.product_name,
y.yearid,
m.month_name,
m.monthid
ORDER BY y.yearid,
m.monthid ASC
目前正在生产以下产品:
product_name | product_cost | product_session_price | product_interaction | yearid | month_name | monthid
Panadol (x16) $4.28 $46.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $66.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $70.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $84.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $224.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $304.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $384.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $456.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $671.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $708.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $719.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $745.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $847.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $915.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $918.00 Add to Basket 2017 January 1
// ... and thousands more records
产品交互表包含三个值:
- 看法
- 买
- 加入篮子
product_name | product_cost | product_session_price | product_interaction | yearid | month_name | monthid
Panadol (x16) $4.28 $46.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $66.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $70.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $84.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $224.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $304.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $384.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $456.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $671.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $708.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $719.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $745.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $847.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $915.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $918.00 Add to Basket 2017 January 1
// ... and thousands more records
- 2017年
- 2018年
- 2019年
product_name | product_cost | product_session_price | product_interaction | yearid | month_name | monthid
Panadol (x16) $4.28 $4.00 Add to Basket 2017 January 1
Panadol (x16) $4.28 $4.28 View 2017 January 1
Panadol (x16) $4.28 $2.55 Buy 2017 January 1
Garden Hose $19.99 $19.99 Add to Basket 2017 January 1
Garden Hose $19.99 $18.95 View 2017 January 1
Garden Hose $19.99 $16.75 Buy 2017 January 1
// and so on...repeating the same for the dates specified with no duplicated records unless dates are different or the product_interaction type is different.
在此方面的任何帮助都将不胜感激 在模式中,“Date(day,Month,year)”表和表“time”被表“Basket”中的timestamp类型列冷替换,以了解项目放置在Basket中的确切时间,然后不需要检查月份中是否存在月天数。如果我了解您需要什么,那么您必须使用聚合功能,如产品上的聚合功能,与group by一起获得最低价格,或使用group by and子句来获得最普通的价格。谢谢,日期/时间如此分割的原因是为了强调作为CW一部分的正常化。它也应该是一个数据仓库而不是一个生产数据库:)我所看到的max是独立的日期和时间列。我认为存储数据变量并不是任何3NF冲突。没有得到“作为CW的一部分”,但这是你的设计,你知道项目的细节