我如何在 SQL 查询中伪造此数据

How can I fabricate this data in SQL query

我对如何从查询中得到这个table感到困惑

+-------------+---------+----------+---------+-------+
|Product Name |Today    |WTD       | MTD     |YTD    |
+-------------+---------+----------+---------+-------+
|Name1        |78       |80        |89       |89     |
+-------------+---------+----------+---------+-------+
|Name2        |56       |78        |88       |78     |
+-------------+---------+----------+---------+-------+

其中的值为平均值,'Today' 是今天的平均值,'WTD' 表示本周至今的平均值,'MTD' 表示本月至今的平均值,'YTD' 表示年初至今的平均值。

老实说,我不太了解SQL,而且我正在使用SQL炼金术(我也不太了解)。 我要从中获取数据的 table 是:

class Product(Base):
    __tablename__ = 'product'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(80))

class ProductAssessment(Base):
    __tablename__ = "product_assessment"
    id = Column(Integer, primary_key=True)
    product_id = Column(Integer, ForeignKey('product.id'), nullable=False)
    product_name = relationship(Product, backref=backref('assessments'),
                   cascade="all, delete, delete-orphan")
    score = Column(Integer, nullable=False)
    record_date = Column(Datetime, default=func.now())

目前我通过这样做得到的是产品的平均价值:

result = DBSession.query(Product.name.label("Product name"), func.avg(Product_Assessment.score).label("YTD")).filter(Product.id==Product_Assessment.product_id).group_by(Product.name)

如果有人能帮助我,我将不胜感激

编辑

示例数据: 产品

+-------+--------+
|id     |name    |
+-------+--------+
|1      |Name1   |
+-------+--------+
|2      |Name2   |
+-------+--------+

Product_Assessment

+----------+---------+--------+----------+
|product_id|id       |score   |Date      |
+----------+---------+--------+----------+
|1         |1        |80.16   |2015/1/5  |
+----------+---------+--------+----------+
|2         |2        |85.19   |2015/1/18 |
+----------+---------+--------+----------+
|1         |3        |81.70   |2015/1/18 |
+----------+---------+--------+----------+
|1         |4        |70.11   |2015/1/18 |
+----------+---------+--------+----------+

预期输出:

+------------+--------+-------+-------+------+
|Product name|Today   |WTD    |MTD    |YTD   |
+------------+--------+-------+-------+------+
|Name1       |70.11   |70.11  |77.32  |77.32 |
+------------+--------+-------+-------+------+
|Name2       |85.19   |85.19  |85.19  |85.19 |
+------------+--------+-------+-------+------+

这是数据@mandeep_m19

简单地说 SQL 你可以像这样实现同样的效果(相应地自己指定日期):

select product.name
, avg(case when product_assessment.record_date = <todays date> then product_assessment.score else 0 end) as today
, avg(case when product_assessment.record_date between <start date> and <end date> then product_assessment.score else 0 end) as wtd
, avg(case when product_assessment.record_date between <start date> and <end date> then product_assessment.score else 0 end) as mtd
, avg(case when product_assessment.record_date between <start date> and <end date> then product_assessment.score else 0 end) as ytd
from product, product_assessment
where product.id = product_assessment.product_id
group by product.name;

基于之前的答案,我宁愿使用 INNER JOIN。我同意 CASE WHEN 结构,但由于每个值要考虑的行数不同,我们需要自己计算平均值(AVG 不起作用)。这是一个相当重的结构,但我看不出有什么更简单的。

SELECT 
   P.name
   ,SUM(CASE WHEN PA.record_date = <todays date> THEN PA.score ELSE 0 END) 
   / SUM(CASE WHEN PA.record_date = <todays date> THEN 1 ELSE 0 END) AS today
   ,SUM(CASE WHEN PA.record_date BETWEEN <start date> AND <end date> THEN PA.score ELSE 0 END) 
   / SUM(CASE WHEN PA.record_date BETWEEN <start date> AND <end date> THEN 1 ELSE 0 END) AS wtd
   ,SUM(CASE when PA.record_date BETWEEN <start date> AND <end date> THEN PA.score ELSE 0 END)
   / SUM(CASE WHEN PA.record_date BETWEEN <start date> AND <end date> THEN 1 ELSE 0 END) AS mtd
   ,SUM(CASE WHEN PA.record_date BETWEEN <start date> AND <end date> THEN PA.score ELSE 0 END)
   / SUM(CASE WHEN PA.record_date BETWEEN <start date> AND <end date> THEN 1 ELSE 0 END) AS ytd
FROM product P
INNER JOIN product_assessment PA
ON (P.id = PA.product_id)
GROUP BY P.name;