Vertica SQL:基于两个条件,无视行
我需要一个考虑在整个表中关注整个表的条件:
如果为ID存在0(列中),则排除所有> 0的所有内容,如果0不存在,则存在d = a的行 在此之前排除所有内容。
例如(情况1),我想无视行1& 2,在示例2(情况2)中,我想无视行1,2,& 3。
载体我有:其中d< = 0或d = a
),但是在情况1中,这也返回了我不想要的行NR 2。
行Nr | Id | D | A |
---|---|---|---|
1 | 1 | 180 | 78 |
2 | 1 | 78 | 78 |
3 | 1 | 0 | 78 |
4 | 1 | -67 | 78 |
5 | 1 | -121 | 78 |
行Nr | ID | D | A |
---|---|---|---|
1 | 2 | 180 | 148 |
2 | 2 | 171 | 148 |
3 | 2 | 170 | 148 |
4 2 148 4 | 2 | 148 148 148 148 | 148 |
5 | 2 | -67 | 148 |
6 | 2 | -121 | 148 |
I need a where condition that considers to following for an entire table:
If a 0 exists for an ID (in column d) then exclude everything that is >0, if 0 does not exist, but exists a row where d = a then exclude everything before that..
In Example (Case 1) I want to disregard rows 1 & 2, in Example 2 (Case 2) I want to disregard rows 1,2,& 3.
Currenty I have: where d <= 0 or d = a
) but in Case 1 this also returns row nr 2, which I do not want.
row nr | ID | d | a |
---|---|---|---|
1 | 1 | 180 | 78 |
2 | 1 | 78 | 78 |
3 | 1 | 0 | 78 |
4 | 1 | -67 | 78 |
5 | 1 | -121 | 78 |
row nr | ID | d | a |
---|---|---|---|
1 | 2 | 180 | 148 |
2 | 2 | 171 | 148 |
3 | 2 | 170 | 148 |
4 | 2 | 148 | 148 |
5 | 2 | -67 | 148 |
6 | 2 | -121 | 148 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这比您预期的要复杂得多。您将必须涉及带有OLAP函数的嵌套查询,以检测一个分区中的每一行(由
ID
的值定义)属于该分区,其中至少一个行的值为0对于d
,然后在嵌套查询之外,过滤该事实,d
的值为0或更高。那是情况1。在另一种情况下,您使用相同的嵌套查询来确定仅使用 no lod 的行,在分区中为
d
的值为0,从那里开始,最简单的方法是使用Vertica的match()
子句来滤除由以下方式组成的行模式:d
等于>
;零,一个或多个以下行的发生,我在查询中描述了图案:(d_equal_a anyrow*)
。这里是:
This becomes a bit more complex to do than what you expected. You will have to involve a nested query with an OLAP function to detect that each row in a partition ( defined by the value of
id
) belongs to a partition of which at least one row has a value of 0 ford
, and then, outside of that nested query, filter for that fact, and the value ofd
being 0 or greater. That's case 1.In the other case, you use the same nested query to ascertain that you use only rows with no row with a value of 0 for
d
in the partition, and from there, the easiest way is to use Vertica'sMATCH()
clause to filter out the pattern of rows that consists of : a row with ad
equal toa
; zero, one or more occurrences of any row following, which I describe, in the query, with the pattern:(d_equal_a anyrow*)
.Here goes: