sql - 如何在没有'with'的情况下将多个子查询合并为一个?
问题描述
我有一个由多个子查询组成的查询。我使用'join',因为我不允许使用'with'。子查询具有“来自”子句,这会产生问题。
我必须显示两列,每列包含要显示的某些逻辑。为了打印这两列,我需要使用需要“from”子句的子查询。我不确定如何编写“from”子句以适应整个查询并使其可运行。我检查了各个查询,它们都工作正常。
select lead(dt) over
(partition by t1.id_user order by f.topup_date desc rows between 0
preceding and unbounded following )
from
(select *,
(max(case when f.topup_value >= 20 then f.topup_date end) over (partition
by f.id_user order by f.topup_date desc rows between 0 preceding and
unbounded following )) as dt
from topups f) as f, //(<-I think this is incorrect)
CAST(f.topup_value as float)/CAST(t1.topup_value as float) from
(SELECT t1.seq,t1.id_user,t1.topup_value,row_number()
over (partition by t1.id_user order by t1.topup_date )
as rowrank from topups t1) as t1
inner join topups f on f.id_user=t1.id_user
inner join topups t2 on t1.seq=t2.seq
解决方案
真的很难阅读该查询。您标记为可能不正确的内容是错误的,因为您试图SELECT
在原始FROM
子句之后添加看起来像另一个的内容。那是不正确的语法。将您的FROM
子查询视为临时表。你不能说这样的话:
SELECT some_column
FROM a_table, some_other_column
那是交叉连接语法。some_other_column
需要成为一个表格才能使其有效。
考虑添加 aCREATE TABLE
和样本数据,以便我们进行测试。
您可能正在寻找类似以下内容的内容:
SELECT LEAD(temp.dt) OVER(PARTITION BY temp.id_user ORDER BY temp.topup_date DESC ROWS BETWEEN 0 PRECEDING AND UNBOUNDED FOLLOWING)
, temp.division
FROM
(
SELECT (max(CASE WHEN f.topup_value >= 20 THEN f.topup_date END) OVER(PARTITION BY f.id_user ORDER BY f.topup_date DESC ROWS BETWEEN 0 PRECEDING AND UNBOUNDED FOLLOWING )) AS dt
, f.topup_value::float / t1.topup_value::float AS division
, t1.id_user
, f.topup_date
FROM topups t1
JOIN topups f USING (id_user)
) temp
;
只是一种意见,但使用::
运算符来转换变量的噪音较小。而不是CAST(f.topup_value as float)
仅仅使用f.topup_value::float
推荐阅读
- c# - 在 c#.net 中添加服务引用时出错
- node.js - 在 Node 中存储敏感凭证的最佳方式是什么?
- c# - Unity:Shooter 2D-Character 动画只运行一次
- sql - SQL 在列之间交换值
- spring-mvc - 当发现不需要的基本身份验证标头时,Spring 安全性不起作用
- matrix - 如何根据层次结构的不同级别进行计算 - Power BI
- c# - “兄弟”视图之间基于事件的通信
- bash - Shell 命令不起作用,但回显命令并手动运行它有效吗?
- python - Python 3 如何格式化为 yyyy-mm-ddThh:mm:ssZ
- python - CMake FindPython3 无法在 Windows 上找到解释器