select ky as productcode
from invoice
cross join lateral jsonb_object_keys(contents) as t(ky)
where orgcode=48 and invoice.inoutflag=15
group by ky
order by count(*) desc
limit(5);
现在我得到前五名产品代码,但是其他表名中有一个标志为“ product”,如果flag = 7,我想获得前五名产品代码。
这是产品表
productcode | gscode | gsflag | productdesc |
----------------+----------+----------+------- ----------+
50 | 444 | 7 | car |
答案 0 :(得分:0)
这是您想要的吗?
select t.ky as productcode
from invoice i cross join lateral
jsonb_object_keys(i.contents) as t(ky) join
product p
on p.productcode = t.ky
where i.orgcode = 48 and i.inoutflag = 15 and
p.gsflag = 7
group by t.ky
order by count(*) desc
limit 5;