我正在寻找查询的方向以获取 ID,对于每笔交易,返回 TransDd,其中所有子项(可以说产品、数量、价格)都有一对/重复值。这里的例子:
TransID Product QTY Price
1 a 2 1.0
1 a 2 1.0
1 b 3 2.5
2 a 1 1.0
2 a 1 1.0
2 b 2 2.0
2 b 2 2.0
3 a 5 2.0
3 a 4 3.0
4 a 1 2.0
4 a 1 2.0
4 b 2 2.0
4 b 2 2.0
4 c 1 1.0
在此示例中,将仅返回 transID 2。
到目前为止,我被困在了
select transid, product, qty, price
, row_number() over (partition by transid, product, qty, price order by transID desc) rk
from x
但我认为我在那里走错了路。欣赏任何方向。
您可以使用count()
代替row_number()
:
select transid
from (select x.*,
count(*) over (partition by transid, product, qty, price) as cnt
from x
) x
group by transid
having min(cnt) > 1;
但是,这有点矫枉过正,您也可以group by
在子查询中使用:
select transid
from (select transid, product, qty, price, count(*) as cnt
from x
group by transid, product, qty, price
) x
group by transid
having min(cnt) > 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句