I have to write a query to select a addressid based on the different criteria for each row. If the the addressid is at the Purchaseorder then use that. If it is not at the PO then use the one which is at the Quote. But remember one PO can be of multiple Quotes so the addressid of max Quote should go there. If there is no addressid at PO or Quote then use Customer's default addressid.
Can any one tell me how to implement this in SQL?