Skip to content
Advertisement

TSQL to perform aggregation with Exists

My sample data source is following

I want SQL to check if there exists any row with cond=X or cond=Y by site and if yes, don’t perform any aggregation; if no, perform a sum of val.

I am currently achieving it by following

Method1

Method 2

I was wondering if there is any better way of achieving this.

Edit

Desired Result

Advertisement

Answer

fiddle

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