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年
表包含从1月到12月的所有12个月

Day表只包含1-31之间的整数

日期表虽然在星型模式中没有清楚显示,但只包含日期、月份和年份表的外键

产品表仅包含随机产品和价格

现在,我想实现的是查询给定时间/日期的理想价格,该价格是购买给定产品时的价格,而不是查看/添加到购物篮中的价格。我的意思是,每种产品都有一个默认价格,这是由产品表中的“产品成本”列定义的。篮子表中的产品会话价格包含不同的价格,以表示出于任何原因的降价。虽然我上面的输出显示product_session_price列实际上比原始成本高,但想象一下这些会更低

假设上述内容有意义,我想为每种产品交互类型的每种产品选择最常见的产品价格

以下是我希望返回数据的方式:

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的一部分”,但这是你的设计,你知道项目的细节