我想计算所有资产利润列中最大值的所有重复(它们重复,我想从每个唯一的重复中选择最佳结果(,最后根据策略列的值对结果进行分组。
我使用 DBD::CSV for Perl,它使用 SQL 语句,如下所示: http://search.cpan.org/~rehsack/SQL-Statement-1.412/lib/SQL/Statement/Syntax.pod
但这并不重要。我最关心的是改进我的语法,剩下的事情我应该处理。
SELECT COUNT(*) AS dupa
FROM (SELECT asset, strategy, MAX(profit)
FROM a1.csv
GROUP BY strategy);
输出:Bad table or column name: 'SELECT' is a SQL reserved word!
再比如:
SELECT * AS xxx FROM (SELECT strategy, profit FROM a1.csv GROUP BY strategy)
输出:Bad table or column name: 'SELECT' is a SQL reserved word!
这只是一个例子。我尝试了不同的组合,但总是有错误。我想问我的语法有什么问题?
您可以将来自"FROM"子句的查询存储在 CTE 中,如下所示:
with cte as(
SELECT asset, strategy, MAX(profit) FROM a1.csv GROUP BY strategy
)
Select count(*)
From cte
这是另一种方法,您能否尝试以下代码:
SELECT COUNT(t.*) AS dupa
FROM (SELECT asset, strategy, MAX(profit)
FROM a1.csv
GROUP BY strategy) t
好的,这是使用临时表的另一种方法,就像我之前说过的那样:
CREATE TEMP TABLE TempTable AS
SELECT asset, strategy, MAX(profit) FROM a1.csv GROUP BY asset, strategy
SELECT COUNT(*) FROM TempTable