基于用户定义的 WHERE 子句
WHERE clause based on user-defined
我有一个变量,date_order
,我在 SELECT 子句中定义,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
.
我希望仅包含 date_order=1
的观察结果。
但是,如果我在下面的语句 and date_order=1
中将条件 date_order=1
包含到 WHERE 子句中,则会出现错误。我在这方面的问题是:
- 是否可以以 SELECT 子句中定义的
date_order
为条件?如果是,我该如何正确执行此操作?
- 如果不可能,你对我应该如何继续有什么建议吗?
我使用以下代码:
select to_char(a.from_date, 'dd-mm-yyyy') as fromdate, a.mother, a.mother_NAME,a.mother_GROUP, a.child_WEIGHT,
a.NODE_NO, a.NODE_NO_PARENT,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
,c.child_WEIGHT as PARENT_child_WEIGHT
from table_a a
left join table_b b
on a.mother_ref=b.mot_ref
left join table_c c
on a.NODE_NO_PARENT=c.NODE_NO and a.mother=c.mother
where 1=1
and a. NODE_TYPE in ('Model mother')
and a.child_WEIGHT <> 0
and c.child_WEIGHT <> 0
;
提前谢谢你。如果我使用 SQL 语法有误,请随时纠正我,但请记住,这只是我第三次使用 SQL.
此致,
将其放入某些 CTE(参见 https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html),然后您将能够针对 date_order
列进行过滤。
它可能看起来像这样:
WITH cte as
(
select
to_char(a.from_date, 'dd-mm-yyyy') as fromdate, a.mother, a.mother_NAME,a.mother_GROUP, a.child_WEIGHT,
a.NODE_NO, a.NODE_NO_PARENT,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
,c.child_WEIGHT as PARENT_child_WEIGHT
from table_a a
left join table_b b on a.mother_ref=b.mot_ref
left join table_c c on a.NODE_NO_PARENT=c.NODE_NO and a.mother=c.mother
where 1=1
and a. NODE_TYPE in ('Model mother')
and a.child_WEIGHT <> 0
and c.child_WEIGHT <> 0
)
select * from cte
where date_order = 1;
尝试使用 select 而不是 select。
select * from (
select to_char(a.from_date, 'dd-mm-yyyy') as fromdate, a.mother, a.mother_NAME,a.mother_GROUP, a.child_WEIGHT,
a.NODE_NO, a.NODE_NO_PARENT,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
,c.child_WEIGHT as PARENT_child_WEIGHT
from table_a a
left join table_b b
on a.mother_ref=b.mot_ref
left join table_c c
on a.NODE_NO_PARENT=c.NODE_NO and a.mother=c.mother
where 1=1
and a. NODE_TYPE in ('Model mother')
and a.child_WEIGHT <> 0
and c.child_WEIGHT <> 0
)t
where t.date_order = 1
据我所知,您不能在同一 SELECT 语句
的 WHERE 块中使用别名(在您的情况下 date_order)
我有一个变量,date_order
,我在 SELECT 子句中定义,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
.
我希望仅包含 date_order=1
的观察结果。
但是,如果我在下面的语句 and date_order=1
中将条件 date_order=1
包含到 WHERE 子句中,则会出现错误。我在这方面的问题是:
- 是否可以以 SELECT 子句中定义的
date_order
为条件?如果是,我该如何正确执行此操作? - 如果不可能,你对我应该如何继续有什么建议吗?
我使用以下代码:
select to_char(a.from_date, 'dd-mm-yyyy') as fromdate, a.mother, a.mother_NAME,a.mother_GROUP, a.child_WEIGHT,
a.NODE_NO, a.NODE_NO_PARENT,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
,c.child_WEIGHT as PARENT_child_WEIGHT
from table_a a
left join table_b b
on a.mother_ref=b.mot_ref
left join table_c c
on a.NODE_NO_PARENT=c.NODE_NO and a.mother=c.mother
where 1=1
and a. NODE_TYPE in ('Model mother')
and a.child_WEIGHT <> 0
and c.child_WEIGHT <> 0
;
提前谢谢你。如果我使用 SQL 语法有误,请随时纠正我,但请记住,这只是我第三次使用 SQL.
此致,
将其放入某些 CTE(参见 https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html),然后您将能够针对 date_order
列进行过滤。
它可能看起来像这样:
WITH cte as
(
select
to_char(a.from_date, 'dd-mm-yyyy') as fromdate, a.mother, a.mother_NAME,a.mother_GROUP, a.child_WEIGHT,
a.NODE_NO, a.NODE_NO_PARENT,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
,c.child_WEIGHT as PARENT_child_WEIGHT
from table_a a
left join table_b b on a.mother_ref=b.mot_ref
left join table_c c on a.NODE_NO_PARENT=c.NODE_NO and a.mother=c.mother
where 1=1
and a. NODE_TYPE in ('Model mother')
and a.child_WEIGHT <> 0
and c.child_WEIGHT <> 0
)
select * from cte
where date_order = 1;
尝试使用 select 而不是 select。
select * from (
select to_char(a.from_date, 'dd-mm-yyyy') as fromdate, a.mother, a.mother_NAME,a.mother_GROUP, a.child_WEIGHT,
a.NODE_NO, a.NODE_NO_PARENT,
rank() over (partition by a.mother order by to_char(a.from_date, 'dd-mm-yyyy') desc) as date_order
,c.child_WEIGHT as PARENT_child_WEIGHT
from table_a a
left join table_b b
on a.mother_ref=b.mot_ref
left join table_c c
on a.NODE_NO_PARENT=c.NODE_NO and a.mother=c.mother
where 1=1
and a. NODE_TYPE in ('Model mother')
and a.child_WEIGHT <> 0
and c.child_WEIGHT <> 0
)t
where t.date_order = 1
据我所知,您不能在同一 SELECT 语句
的 WHERE 块中使用别名(在您的情况下 date_order)