如何获得具有最大值的行?
How to get row with largest value?
我认为可行的是:
SELECT *
FROM customer_sale
WHERE sale_date < '2019-02-01'
GROUP BY customer_id
HAVING sale_date = MAX(sale_date)
但是运行这会导致错误
HAVING clause expression references column sale_date
which is
neither grouped nor aggregated
在 Spanner 中还有其他方法可以实现这一点吗?更一般地说,为什么不允许以上内容?
编辑
customer_sale
table中的数据示例:
customer_id sale_date
-------------------------------
1 Jan 15
1 Jan 30
1 Feb 2
1 Feb 4
2 Jan 15
2 Feb 2
预期结果:
customer_id sale_date
-------------------------------
1 Jan 30
2 Jan 15
A HAVING clause in SQL specifies that an SQL SELECT statement should
only return rows where aggregate values meet the specified conditions.
It was added to the SQL language because the WHERE keyword could not
be used with aggregate functions
这是测试table我正在使用:
index, customer_id, sale_date
1 1 2017-08-25T07:00:00Z
2 1 2017-08-26T07:00:00Z
3 1 2017-08-27T07:00:00Z
4 1 2017-08-28T07:00:00Z
5 2 2017-08-29T07:00:00Z
6 2 2017-08-30T07:00:00Z
使用此查询:
Select customer_id, max(sale_date) as max_date
from my_test_table
group by customer_id;
我得到这个结果:
customer_id max_date
1 2017-08-28T07:00:00Z
2 2017-08-30T07:00:00Z
还包括 where 语句:
Select customer_id, max(sale_date) as max_date
from my_test
where sale_date < '2017-08-28'
group by customer_id;
我遇到了同样的问题,通过这种方式我得以解决。如果你有一个很大的 table 可能需要一些时间。
基本上,将您的普通 table 与具有最大值记录的 table 相结合即可解决问题。
select c.* from
(select * from customer_sale WHERE sale_date < '2019-02-01') c
inner join
(SELECT customer_id, max(sale_date) as max_sale_date FROM customer_sale WHERE
sale_date < '2019-02-01' group by customer_id) max_c
on c.customer_id = max_c.customer_id and c.sale_date = max_c.sale_date
我认为可行的是:
SELECT *
FROM customer_sale
WHERE sale_date < '2019-02-01'
GROUP BY customer_id
HAVING sale_date = MAX(sale_date)
但是运行这会导致错误
HAVING clause expression references column
sale_date
which is neither grouped nor aggregated
在 Spanner 中还有其他方法可以实现这一点吗?更一般地说,为什么不允许以上内容?
编辑
customer_sale
table中的数据示例:
customer_id sale_date
-------------------------------
1 Jan 15
1 Jan 30
1 Feb 2
1 Feb 4
2 Jan 15
2 Feb 2
预期结果:
customer_id sale_date
-------------------------------
1 Jan 30
2 Jan 15
A HAVING clause in SQL specifies that an SQL SELECT statement should only return rows where aggregate values meet the specified conditions. It was added to the SQL language because the WHERE keyword could not be used with aggregate functions
这是测试table我正在使用:
index, customer_id, sale_date
1 1 2017-08-25T07:00:00Z
2 1 2017-08-26T07:00:00Z
3 1 2017-08-27T07:00:00Z
4 1 2017-08-28T07:00:00Z
5 2 2017-08-29T07:00:00Z
6 2 2017-08-30T07:00:00Z
使用此查询:
Select customer_id, max(sale_date) as max_date
from my_test_table
group by customer_id;
我得到这个结果:
customer_id max_date
1 2017-08-28T07:00:00Z
2 2017-08-30T07:00:00Z
还包括 where 语句:
Select customer_id, max(sale_date) as max_date
from my_test
where sale_date < '2017-08-28'
group by customer_id;
我遇到了同样的问题,通过这种方式我得以解决。如果你有一个很大的 table 可能需要一些时间。
基本上,将您的普通 table 与具有最大值记录的 table 相结合即可解决问题。
select c.* from
(select * from customer_sale WHERE sale_date < '2019-02-01') c
inner join
(SELECT customer_id, max(sale_date) as max_sale_date FROM customer_sale WHERE
sale_date < '2019-02-01' group by customer_id) max_c
on c.customer_id = max_c.customer_id and c.sale_date = max_c.sale_date