【发布时间】:2021-12-22 12:04:10
【问题描述】:
我有一些这样的数据:
| Product | Price | Date |
|---|---|---|
| Product A | 1.00 | 2021-11-01 |
| Product A | 2.00 | 2021-11-02 |
| Product B | 3.00 | 2021-11-01 |
| Product B | 4.00 | 2021-11-02 |
我需要一张表中每种产品的平均价格和最新价格,所以它应该如下所示:
| Product | Avg(Price) | Latest(Price) |
|---|---|---|
| Product A | 1.50 | 2.00 |
| Product B | 3.50 | 4.00 |
总而言之,我只需要将这些组合到查询中:
SELECT Price FROM MyTable
where Date = (select max(Date) FROM MyTable)
group by Product
和
SELECT Avg(Price) FROM MyTable
group by Product
如何编写才能正常工作?
【问题讨论】:
标签: sql database google-bigquery