Home > Enterprise >  Could you help me to write this simple SQL (I haven't come up with results)
Could you help me to write this simple SQL (I haven't come up with results)

Time:07-15

Thank you for your help.

could you help me to create a SQL statement which shows if amount1 ($500 $300) >amount2 ($400 200) by 100, then result listing the two lines. I know I need to group column1 by 1001 and 1002, but couldn't come up with the rest :-( since for column 1 1002 is larger by only by 50 (100 - 50), so shouldn't show up in results.

Thanks in advance Jessie

column1 column2 amount1 amount2 customer
1001 1 $500 $400 ABC
1001 2 $300 $200 ABC
1002 1 $100 $50 CBX

Results should be:

column1 column2 amount1 amount2 customer
1001 1 $500 $400 ABC
1001 2 $300 $200 ABC

CodePudding user response:

Yo could use a correlated subquery with exists, such as:

select * 
from t
where exists (
  select * from t t2
  where t2.column1 = t.column1
  group by column1 
  having Sum(amount1) > (Sum(amount2)   100)
);
  • Related