[英]get row by criteria from PostgreSQL query
I have the following query select * from func1(ID)
我有以下查询,
select * from func1(ID)
If I do: select * from func1(10)
如果我这样做:
select * from func1(10)
I get: 我得到:
rownum date qty
1 1.1.10 -5
2 1.10.10 6
3 2.10.10 6
4 5.10.10 -2
5 6.10.10 -8
If I do select * from func1(7)
I get 如果我确实
select * from func1(7)
我会得到
rownum date qty
1 1.1.10 -7
2 1.10.10 -6
If I do select * from func1(6)
I get 如果我确实
select * from func1(6)
我会得到
rownum date qty
rownum
is the order of the column its being calculated in func1()
based on my needs (it's not random numbering) you assume that the numbering is correct. rownum
是根据我的需要在func1()
计算的列的顺序(这不是随机编号),您假定编号是正确的。
I want to write a query which returns me that find the first appears of qty>=0
in qty
(search from bottom up! from highest rownum
to lowest) and gives me the date
in the rownum+1
row. 我想写一个查询,返回给我,让我发现
qty>=0
第一次出现在qty
(从下往上搜索!从最高的rownum
到最低的),并给我date
为rownum+1
。 If no rows match for qty>=0
it will return the date of the 1st row. 如果没有行匹配
qty>=0
,它将返回第一行的日期。 If no rows at all return NULL
. 如果根本没有行,则返回
NULL
。
meaning that for: 意味着:
select * from func1(10)
the output should be 5.10.10
(as rownum=3 is the first qty>=0 from bottom up and 5.10.10 is the date in the rownum+1) select * from func1(10)
输出应为5.10.10
(因为rownum = 3是自下而上的第一个数量> = 0,而5.10.10是rownum + 1中的日期)
select * from func1(7)
the output should be 1.1.10
(as there are no qty>=0 so it gives the date of the rownum=1) select * from func1(7)
输出应为1.1.10
(因为没有qty> = 0,所以它给出了rownum = 1的日期)
select * from func1(7)
the output should be NULL
(as there are no rows) select * from func1(7)
输出应为NULL
(因为没有行)
How can i do that? 我怎样才能做到这一点?
first not negative from the bottom 首先不是自下而上的负面
t=# with a(i,q) as (values(1,-5),(2,3),(3,-9),(4,-1))
select * from a where i=1 or q>=0 order by case when i=1 then null else i end desc nulls last, q limit 1;
i | q
---+---
2 | 3
(1 row)
all negatives - first row: 所有底片-第一行:
t=# with a(i,q) as (values(1,-5),(2,-3),(3,-9),(4,-1))
select * from a where i=1 or q>=0 order by case when i=1 then null else i end desc nulls last, q limit 1;
i | q
---+----
1 | -5
(1 row)
empty set: 空集:
t=# with a(i,q) as (select 1,1 where false)
select * from a where i=1 or q>=0 order by case when i=1 then null else i end desc nulls last, q limit 1;
i | q
---+---
(0 rows)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.