Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. CASE
  2. WHEN col1 > col2 THEN SUM(col3*col4)
  3. ELSE 0
  4. END AS some_product
  5.  
  6. select ...
  7. from (select ... , CASE WHEN col1 > col2 THEN SUM(col3*col4) ELSE 0 END AS some_product
  8. from ...
  9. group by col1, col2 ... ) T
  10. group by some_product ...
  11.  
  12. with T as (select ... , CASE WHEN col1 > col2 THEN SUM(col3*col4) ELSE 0 END AS some_product
  13. from ...
  14. group by col1, col2 ... )
  15. select ...
  16. from T
  17. group by some_product ...
  18.  
  19. Select Sum(Case When col1 > col2 Then col3*col4 Else 0 End) as SumSomeProduct
  20. From ...
  21.  
  22. Group By Case When col1 > col2 Then col3*col4 Else 0 End
  23.  
  24. Select SumSomeProduct, Count(*), <other aggregate functions>
  25. From (Select <other columns you are grouping By>,
  26. Sum(Case When col1 > col2
  27. Then col3*col4 Else 0 End) as SumSomeProduct
  28. From Table
  29. Group By <Other Columns> ) As Z
  30. Group by SumSomeProduct
Add Comment
Please, Sign In to add comment