简体   繁体   English

如何在没有 NULL 的情况下显示 mysql 查询?

[英]How show mysql query without NULL?

How show mysql query without NULL?如何在没有 NULL 的情况下显示 mysql 查询?

I want to show my query where "LAG(title) OVER(PARTITION BY emp_no)" IS NOT NULL, I need only titles.emp_no = "LAG(title) OVER(PARTITION BY emp_no)"我想显示我的查询“LAG(title)OVER(PARTITION BY emp_no)”不是NULL,我只需要titles.emp_no =“LAG(title)OVER(PARTITION BY emp_no)”

My query:我的查询:

SELECT titles.emp_no, LAG(title) OVER(PARTITION BY emp_no)
FROM titles;

Output: Output:

在此处输入图像描述

Subquery your current query and filter based on the lag value:子查询您当前的查询并根据滞后值进行过滤:

WITH cte AS (
    SELECT emp_no, LAG(title) OVER (PARTITION BY emp_no ORDER BY <col>) lag_title
    FROM titles
)

SELECT emp_no, lag_title
FROM cte
WHERE lag_title IS NOT NULL;

Notice that I added an ORDER BY clause to LAG , without which is does not make much sense.请注意,我在LAG中添加了ORDER BY子句,没有它就没有多大意义。

you could simply use:你可以简单地使用:

select emp_no, max(title) as title
from titles
group by emp_no

Results would include some title:NULL if there is no row with that emp_no with title:NOT NULL.如果没有标题为 emp_no 且标题为:NOT NULL 的行,则结果将包括一些标题:NULL。 If you want to eliminate those, as well (and show only those where there is at least one nn-Null title:如果您还想消除那些(并且只显示至少有一个 nn-Null 标题的那些:

select emp_no, max(title) as title
from titles
where title is not null
group by emp_no

Any specific reason for using LAG?使用 LAG 的任何具体原因? it would make sense if you wanted to get a previous (ordered in a particular way) title (and that title was not-null);如果您想获得以前的(以特定方式排序)标题(并且该标题不为空),那将是有意义的; but you didn't specify an order.但您没有指定订单。

@Vladyslav Nazarov If you want you can also use max(title)over(partition by emo_no order by ---only if you want or need----) in this case you increase performance and you don t use group by @Vladyslav Nazarov 如果你愿意,你也可以使用 max(title)over(partition by emo_no order by ---仅当你想要或需要时----) 在这种情况下你可以提高性能并且你不使用 group by

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM