sql - Using If/CASE statement in Where clause -


i have table need sum values when condition met.

this initial table querying from:

  worksheet   label   lob        data   -----------------------------------------    layers    aal    eq-ao-us    64726.02706    layers    aal    eq-ca       171683.6675    layers    aal    eq-int-m    858.9752352    layers    aal    eq-nm       9225.104696    layers    aal    eq-pnw      12554.17653    layers    aal    nws-int-m   63142.0387    layers    aal    nws-us      300230.9489    p&l      adjaal  eq-ao-us    32363.01353    p&l      adjaal  eq-ca       251180.82    p&l      adjaal  eq-nm       4612.552348    p&l      adjaal  eq-pnw      6277.088264    p&l      adjaal  nws-int-m   31571.01935    p&l      adjaal  nws-us      132993.08 

i need sum data of listed lob's in query , bring final sum. first need check if lob there adjaal, , if was, use one. otherwise, need use aal 'modeled' value particular lob

ms sql query

select fileid,               sum(cast(data float)) modeled          premium_data          label in ('adjaal','aal') , lob in ('nws-us',         'nws-int-m',         'eq-ca',         'eq-nm',         'eq-pnw',         'eq-ao-us',         'eq-int-m',         'stormsurge-us')         , fileid = 17719         group fileid  

the above query adds lob list , gives incorrect value 1081418.51206399. expect final answer 459856.5487. (i got number adding following numbers above table)

    adjaal        32363.01353     adjaal        251180.82     adjaal        4612.552348     adjaal        6277.088264     adjaal        31571.01935     adjaal        132993.08     aal           858.9752352 

any idea on how approach such cases of using if / case statement in clause or better approach retrieve final correct answer.

use row_number select label want.

i use demo in postgres same on mssql

select * -- sum("modeled") ( select *,               row_number() on (partition "lob" order "label" desc) rn        table1 t1      ) t t.rn =1  

enter image description here


Comments