优化一下我的sql语句:WITH t1 AS ( SELECT *, row_number() OVER (PARTITION BY user_id) rn FROM web_operation ) SELECT log_time,count(1) cnt FROM t1 WHERE rn =1 GROUP BY log_time;
时间: 2023-03-13 19:26:07 浏览: 104
我可以尝试优化你的SQL语句:SELECT log_time, COUNT(1) cnt FROM web_operation WHERE user_id IN (SELECT user_id FROM web_operation GROUP BY user_id) GROUP BY log_time;
相关问题
SQL 语句优化 SELECT t.* FROM (SELECT * ,ROW_NUMBER() OVER (PARTITION BY S_INFO_WINDCODE ORDER BY TRADE_DT DESC) AS rn FROM wind.CHINACLOSEDFUNDEODPRICE WHERE TRADE_DT > 20230101) t WHERE t.rn = 1; 提示[42000][923] ORA-00923: 未找到要求的 FROM 关键字
这是一个 Oracle 数据库的错误提示,提示中指出未找到要求的 FROM 关键字。可以检查一下 SQL 语句中的语法是否正确,比如是否缺少了必要的关键字或者符号。
针对这条 SQL 语句,可以尝试将子查询的结果集命名为一个表,然后在外层查询中使用该表。具体的 SQL 语句如下所示:
```
SELECT t.*
FROM
(SELECT *, ROW_NUMBER() OVER (PARTITION BY S_INFO_WINDCODE ORDER BY TRADE_DT DESC) AS rn
FROM wind.CHINACLOSEDFUNDEODPRICE
WHERE TRADE_DT > 20230101) AS t
WHERE t.rn = 1;
```
这样写可以让 SQL 语句更加清晰易懂,并且也可以避免一些语法错误。另外,如果数据量比较大,可以考虑对表添加索引来提高查询性能。
spark.sql( """ |select *, |if rn=1 |when gather_time=1 then 2 |end |from( |select * ,row_number() over(partition by name order by gather_time desc)rn |from gather) |""".stripMargin).show() 帮我改正确
以下是修改后的代码:
```
spark.sql("""
select *,
case when rn=1 and gather_time=1 then 2 else null end as new_col
from(
select *, row_number() over(partition by name order by gather_time desc) as rn
from gather
)
""").show()
```
修改说明:
1. 用`case when ... then ... else ... end`代替了原来的`if ... when ... then ... end`,因为Spark SQL中没有if语句。
2. 为`row_number()`函数的结果指定了别名`rn`。
3. 用`as`代替了原来的`|`,因为Spark SQL中不支持`|`作为换行符。
阅读全文