sql - SUM conditional logic -


i have following 2 tables in sql server 2012:

|=================|   |id|salary|exclude|   | 1| 90000|   null|   | 1| 65000|   null|   | 1| 10000|      y|   | 2| 85000|   null|   | 2|  4500|      y|   | 2|  1000|      y|   | 3| 75000|   null|   |=================|  |===========|   |id|contract|   | 1|  155000|   | 2|   85000|   | 3|   75000|   |===========| 

i need write query returns following results:

|===========================|   |id|contract|excluded salary|   | 1|  155000|          10000|   | 2|   85000|           5500|   | 3|   75000|           null|   |===========================| 

in other words, need sum salary field in first table each id if exclude y. know how sum salary column don't understand how include exclude logic. can give me appreciated! thanks!

try this:

select      a.id,      b.contract,      sum(case when exclude = 'y' salary end) excluded_salary table_salary inner join table_contract b    on a.id = b.id group a.id, b.contract 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -