sql - Select case comparing two columns -
i have query uses greater of 2 values/columns if other value record true.
i'm trying report account holdings. unfortunately db stores value of cash in column called holdingqty, while every other type of holding (stocks, bonds, mutual funds) stores in column called qty.
the problem value of cash stored in qty only, , in both qty , holdingqty. stored in holdingqty mentioned above.
basically want select statement "if security cash, @ both qty , holding qty , give me value of whatever greater. otherwise, if security isn't cash give me qty".
how write in t-sql? here effort:
select h.account_name, h.security_name, h.security_type, h.price, (case: when security_type = 'cash' (when h.qty > h.holdingqty h.qty else h.holdingqty) else qty) quantity, h.total_value holdings h ...........
your query correct need few syntax arrangement, try below code
select h.account_name, h.security_name, h.security_type, h.price, case when security_type = 'cash' case when h.qty > h.holdingqty h.qty else h.holdingqty end else qty end 'yourcolumnname' ) quantity, h.total_value holdings h ...........
Comments
Post a Comment