【问题标题】:Select average number for the latest date in Google Query在 Google Query 中选择最近日期的平均数
【发布时间】:2018-01-10 11:09:18
【问题描述】:

我需要计算每种农业最近日期的平均价格。我使用此 Google 查询从 Google 表格加载表格。

var queryString = encodeURIComponent("select max(A), D, avg(E) where B = 'cpt-novo' group by D");

但我得到不正确的结果。此外,如果我更改任何内容,我经常会收到诸如 "ADD_COL_TO_GROUP_BY_OR_AGG""CANNOT_GROUP_WITHOUT_AGG" 之类的错误。

这是表格:

date,       basis,    trader,   culture,       price
10.10.2017, cpt-novo, one,      wheat-5-class, 8000
31.10.2017, cpt-novo, one,      wheat-5-class, 8500
17.10.2017, cpt-novo, one,      wheat-11.5,    9100
31.10.2017, cpt-novo, one,      wheat-11.5,    9200
17.10.2017, cpt-novo, one,      wheat-12,      9300
31.10.2017, cpt-novo, one,      wheat-12,      9400
17.10.2017, cpt-novo, one,      wheat-12.5,    9500
31.10.2017, cpt-novo, one,      wheat-12.5,    9600
17.10.2017, cpt-novo, one,      wheat-13,      9750
31.10.2017, cpt-novo, one,      wheat-13,      9850
17.10.2017, cpt-novo, one,      wheat-13.5,    10000
31.10.2017, cpt-novo, one,      wheat-13.5,    10100
27.07.2017, cpt-novo, two,      barley,        8600
05.08.2017, cpt-novo, two,      barley,        9000
02.09.2017, cpt-novo, two,      wheat-11.5,    8300
10.10.2017, cpt-novo, two,      wheat-11.5,    9000
10.10.2017, cpt-novo, two,      wheat-12,      9300
01.12.2017, cpt-novo, two,      wheat-12,      9200
10.10.2017, cpt-novo, two,      wheat-12.5,    9600
01.12.2017, cpt-novo, two,      wheat-12.5,    9500
10.10.2017, cpt-novo, two,      wheat-13,      9800
01.12.2017, cpt-novo, two,      wheat-13,      9700
10.10.2017, cpt-novo, two,      wheat-13.5,    10000
10.10.2017, cpt-novo, two,      wheat-13.5,    10100
06.12.2017, cpt-novo, three,    wheat-13,      9800
06.12.2017, cpt-novo, three,    wheat-12,      9400
06.12.2017, cpt-novo, three,    wheat-11.5,    9200

结果应该是这些(自己计算):

wheat-5-class,  8500
wheat-11.5,     9133,333333
wheat-12,       9333,333333
wheat-12.5,     9550
wheat-13,       9783,333333
wheat-13.5,     10100
barley,         9000

【问题讨论】:

  • 您的查询似乎有 3 列。您的结果似乎有 2 列。

标签: mysql sql google-api google-docs-api google-query-language


【解决方案1】:

此答案适用于 MySQL:

第一步:计算每种文化的 MAX(日期)...

    SELECT culture, MAX(date) max_date FROM my_table GROUP BY culture;
    +---------------+------------+
    | culture       | max_date   |
    +---------------+------------+
    | barley        | 2017-08-05 |
    | wheat-11.5    | 2017-10-31 |
    | wheat-12      | 2017-12-01 |
    | wheat-12.5    | 2017-12-01 |
    | wheat-13      | 2017-12-01 |
    | wheat-13.5    | 2017-10-31 |
    | wheat-5-class | 2017-10-31 |
    +---------------+------------+

第二步:找出所有满足上述条件的行...

SELECT a.* 
  FROM my_table a 
  JOIN
     ( SELECT culture
            , MAX(date) max_date 
         FROM my_table 
        GROUP 
           BY culture
     ) b
    ON b.culture = a.culture
   AND b.max_date = a.date;
+------------+----------+--------+---------------+-------+
| Date       | basis    | trader | culture       | price |
+------------+----------+--------+---------------+-------+
| 2017-08-05 | cpt-novo | two    | barley        |  9000 |
| 2017-10-31 | cpt-novo | one    | wheat-11.5    |  9200 |
| 2017-12-01 | cpt-novo | two    | wheat-12      |  9200 |
| 2017-12-01 | cpt-novo | two    | wheat-12.5    |  9500 |
| 2017-12-01 | cpt-novo | two    | wheat-13      |  9700 |
| 2017-10-31 | cpt-novo | one    | wheat-13.5    | 10100 |
| 2017-10-31 | cpt-novo | one    | wheat-5-class |  8500 |
+------------+----------+--------+---------------+-------+

第三步:找出上述商品的平均价格(按文化)。

留给读者作为练习。

编辑:我对 gql 一无所知。我怀疑你可以写类似下面的东西,但我不知道它是否正确或有效......

for b in db.GqlQuery("SELECT culture , MAX(date) max_date GROUP BY culture"):
for a in db.GqlQuery("SELECT * WHERE culture=:1 AND date=:2", b.culture,b.max_date):
print a.price

同样,万一上述情况正确,我已省略第 3 步。

【讨论】:

  • 否,例如对于大麦,它将是 9000,因为我们应该只取最后一个日期。对于小麦 11.5:(9200+9000+9200)/3=9133,333333
  • 这不是您的查询所说的。
  • 遗憾的是,谷歌查询语言中没有 fromjoin 运算符。有没有其他办法?
猜你喜欢
  • 2014-09-17
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多