0

sorry for bad english.

well, I have question how to comparing two query. example.

tabel transactions

|id| name | total| date      |

|01|rini  | 2    |2019/12/1  |

|02|rana  | 3    |2019/12/31 |

|03|tono  | 2    |2020/01/5  |

|04|tini  |10    |2020/28/5  |

if I want to know transactions on december 2019 and januari 2020 I can write

select sum(total) from transactions where date between '2019-12-01' and '2019-12-31'

select sum(total) from transactions where date between '2020-01-01' and '2020-01-31'

but How can I compare between that 2 query and have result like this

|desember_transaction|januari_transaction|

|          5         |         12        |

sorry again for my bad english. hope you all understand what I mean. :)

techlover
  • 31
  • 4

1 Answers1

1

use conditional aggregation

select 
sum(case when date between '2019-12-01' and '2019-12-31' then total else 0 end) desember_transaction,
sum(case when date between '2020-01-01' and '2020-01-31' then total else 0 end) januari_transaction
from transactions 
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
  • wow, thanks man. its help me. but I wonder, what is function "else 0"? coz I put it on or not the result is same. – techlover Jan 20 '20 at 07:13