I have the following data and I need help creating a case expression with a condition, When more than one PPaymentID exists per PaymentID then M is Y else false.
PaymentID ProductID PPaymentID
1456789 1398 4587934
3445738 1398 8754418
3445738 1399 8754419
I would like to see something like
PaymentID ProductID PPaymentID M
1456789 1398 4587934 N
3445738 1398 8754418 Y
3445738 1399 8754419 Y
You can use
SELECT *, CASE WHEN COUNT(1) OVER(PARTITION BY PaymentID) >= 2 THEN 'Y' ELSE 'N' END AS M
FROM YourTable
Since you only care whether there is another row with the same PaymentId
and a different PPaymentId
there is no need to obtain a precise count
. It is more efficient to just check if such a row exists
:
select PaymentId, ProductId, PPaymentId,
case when exists ( select 42 from YourTableOData as iYTOD
where iYTOD.PaymentId = YTOD.PaymentId and iYTOD.PPaymentId != YTOD.PPaymentId )
then 'Y'
else 'N' end as M
from YourTableOData as YTOD;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.