select - SQL SUM restricted -
i have view pulling data number of tables. let's consider this:
transpk amount currency_id ------------------------------- 1 2000 0 1 -2000 0 2 3600 1 2 -7200 2 . . .
i want calculate sum(amount), per distinct transpk, currency_ids given transpk same. i.e. in data above there 1 result returned, since transpk=2 there 2 values currency_id.
i have no idea search on this! i've tried "sql sum restricted", hits weren't useful. i'm not first person want this. able point me in right direction?
try this:
select transpk, sum(amount) tablea group transpk having count(distinct currency_id) = 1;
check sql fiddle demo
output
| transpk | amount | |---------|--------| | 1 | 0 | | 3 | 3000 |
Comments
Post a Comment