简体   繁体   English

sql 查询每天平均价格和每天分组

[英]sql query with avg price per day and group by day

I got this table我有这张桌子

+------+------------------+-----------+-----------+
| Name |       Time       | LowPrice  | HighPrice |
+------+------------------+-----------+-----------+
| #AAA | 12/13/2021 17:12 |    383.12 |     393.9 |
| #BBB | 12/13/2021 17:13 |   1110.34 |    1114.1 |
| #AAA | 12/13/2021 17:13 |    384.15 |     399.2 |
| #BBB | 12/13/2021 17:14 |   1112.34 |    1119.1 |
+------+------------------+-----------+-----------+

and this query:这个查询:

SELECT "Name", "Time", "LowPrice", "HighPrice"
FROM rp_prices
WHERE "Time" > NOW() - INTERVAL '10 day';

I need to get only one price, with avg I think, and grouped by day, something like this我只需要得到一个价格,我认为是平均价格,并按天分组,像这样

+------+-------------+-----------+-----------+
| Name |    Time     | LowPrice  | HighPrice |
+------+-------------+-----------+-----------+
| #AAA | 12/13/2021  |    383.12 |     393.9 |
| #BBB | 12/13/2021  |   1110.34 |    1114.1 |
+------+-------------+-----------+-----------+

Thanks for your help谢谢你的帮助

SELECT "Name", date_trunc('day', "Time"), avg("LowPrice"), avg("HighPrice")
  FROM rp_prices
 WHERE "Time" > now() - interval '10 day'
 GROUP BY "Name", date_trunc('day', "Time")

Something like就像是

SELECT "Name", "Time", 
       min("LowPrice") as min_lp, 
       max("HighPrice") as max_hp, 
       avg("LowPrice") as avg_lp,
       avg("HighPrice") as avg_hp,
       avg(("HighPrice" - "LowPrice")/2) as avg_middle 
FROM rp_prices
where "Time" > now() - interval '10 day'
group by "Name", "Time";

might give you the answer you are looking for.可能会给你你正在寻找的答案。 It is allowed to use functions in the Group By clause.允许在 Group By 子句中使用函数。 You can use those if the column "Time" has a higher precision than days如果“时间”列的精度高于天数,则可以使用它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM