如何在SQL查询中制作此数据

时间:2015-01-18 17:07:22

标签: python mysql sql sqlalchemy

我对如何从查询中获取此表感到困惑

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

其中值为平均值,“今天”是今天的平均值,“WTD”表示周至日均值,“MTD”表示月初至今的平均值,“年初至今”表示年初至今的平均值。

老实说,我不太了解SQL,我也使用SQLALchemy(我也不太了解)。 我想从中获取数据的表是:

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

2 个答案:

答案 0 :(得分:1)

在纯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;

答案 1 :(得分:1)

在前一个答案的基础上,我宁愿使用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;