sql server - How do I add a conditional column to a union query in MS SQL? -


in ms sql there way can add conditional column union query 1 below based on value of first column?

basically im trying add bin value fourth column if part value ('%xx') or have display 0, far have been able add column mirrors max(part_bins.bin) column if code doesn't meet like.

select part_bins.part part,        sum(part_bins.free_stock) free_stock,        max(part_bins.bin) bin part_bins left outer join part on part_bins.part = part.part (part_bins.free_stock > 0) group part_bins.part union select part.master_part part,        sum(part_bins.free_stock) free_stock,        max(part_bins.bin) bin part_bins left outer join part on part_bins.part = part.part (part_bins.free_stock > 0) group part.master_part 

you can use case statement , have add in both queries add fourth column like;

--4th column both queries, if bin_value numeric type case when part_value '%xx' bin_value         else 0 end fourth_column  --4th column both queries, if bin_value string type case when part_value '%xx' bin_value         else '0' end fourth_column 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -