Skip to content
Advertisement

Complicated logic of group by and partition

I have the table as per the script below. The data that I want finally is shown in the screenshot. The logic that is to be implemented is : If SUM(FPR_QTY) > QPA, Use QPA without summing it up. Else, use FPR_QTY.

Eg explanation: For the first 4 rows, TOT_FPR > QPA, so I just need 1. While for rest 4 rows, TOT_FPR < QPA, i need to use TOT_FPR. So, ultimately, I want 21 against each record.

Please let me know if my explanation is not clear.

enter image description here

Advertisement

Answer

Here you go…I was able to write the query within two selects. I wish I could accept my own answer as it is most simplest and will run without failure.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement