HEX
Server: Apache/2.2.15 (CentOS)
System: Linux ip-10-0-2-146.eu-west-1.compute.internal 2.6.32-754.35.1.el6.centos.plus.x86_64 #1 SMP Sat Nov 7 11:33:42 UTC 2020 x86_64
User: root (0)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /www/exchange2/sql/custom/Leasabill-population.sql
/* Leasabill population report */
SELECT * FROM (
SELECT 
investor.ref_id as "Investor ID",
IF(investor.register_type=1,investor.organization_name, GROUP_CONCAT(DISTINCT CONCAT(contact_details.last_name,", ",contact_details.first_name)  SEPARATOR " & ")) as "Investor Name",
intermediary.ref_id as "Intermediary ID",
intermediary.organization_name as "Intermediary Name",
FORMAT(
	(CASE investor_investments.investment_type
            WHEN 1 THEN IF(investor_investments.currency_id!=1,investor_investments.amount*cr.currency_rate,investor_investments.amount)
            WHEN 3 THEN -1*IF(investor_investments.currency_id!=1,investor_investments.amount*cr.currency_rate,investor_investments.amount)
			WHEN 4 THEN -1*IF(investor_investments.currency_id!=1,investor_investments.change_principal*cr.currency_rate,investor_investments.change_principal)
            WHEN 8 THEN IF(investor_investments.currency_id!=1,investor_investments.amount*cr.currency_rate,investor_investments.amount)
            ELSE investor_investments.amount END),2) as "Principal Amount",
investor_investments.investor_id as "IID",
investor_investments.investment_date as "Transaction Date",
investor_investments.investment_id as "Investment ID",
investor_investments.investment_source as "Investment Type",
IF(investor_investments.investment_type = 1, "RPA Receipt",
    IF(investor_investments.investment_type = 2, "Coupon",
        IF(investor_investments.investment_type = 3, "Partial Redemption",
            IF(investor_investments.investment_type = 4, "Full Redemption",
                IF(investor_investments.investment_type = 5, "Compound",
                    IF(investor_investments.investment_type = 8, "Addition",
                        IF(investor_investments.investment_type = 9, "Renewal",
                           IF(investor_investments.investment_type = 11, "Partial Coupon","")))))))) as "Transaction Type",
investor_investments.investment_group as "Investment Group" 
FROM investor_investments
LEFT JOIN temp_registration as investor ON investor.user_id = investor_investments.investor_id 
LEFT JOIN currencies ON currencies.currency_id = investor_investments.currency_id 
LEFT JOIN currency_rates cr ON cr.currency_id = investor_investments.currency_id AND cr.currency_date = investor_investments.investment_date
LEFT JOIN transaction_types ON transaction_types.type_id=1 AND transaction_types.transaction_id = investor_investments.investment_type 
LEFT JOIN investor_relation ON investor_relation.investor_id = investor.temp_reg_id 
LEFT JOIN temp_registration as intermediary ON intermediary.user_id = investor_relation.intermediary_id 
LEFT JOIN contact_details ON contact_details.temp_reg_id = investor.temp_reg_id  AND (contact_details.contact_flag=3 OR (contact_details.contact_flag=0 AND contact_details.bank_id=0)) 
WHERE investor_investments.status IN (1,0) AND investor_investments.investment_type IN (1,3,4,8) AND investor_investments.investment_source="LB" 
GROUP BY investor_investments.investment_id ) as A

UNION 

SELECT * FROM (
SELECT 
investor.ref_id as "Investor ID",
IF(investor.register_type=1,investor.organization_name, GROUP_CONCAT(DISTINCT CONCAT(contact_details.last_name,", ",contact_details.first_name)  SEPARATOR " & ")) as "Investor Name",
intermediary.ref_id as "Intermediary ID",
intermediary.organization_name as "Intermediary Name",
FORMAT(
	(CASE investor_investments.investment_type
            WHEN 1 THEN IF(investor_investments.currency_id!=1,investor_investments.amount*cr.currency_rate,investor_investments.amount)
            WHEN 3 THEN -1*IF(investor_investments.currency_id!=1,investor_investments.amount*cr.currency_rate,investor_investments.amount)
			WHEN 4 THEN -1*IF(investor_investments.currency_id!=1,investor_investments.change_principal*cr.currency_rate,investor_investments.change_principal)
            WHEN 8 THEN IF(investor_investments.currency_id!=1,investor_investments.amount*cr.currency_rate,investor_investments.amount)
            ELSE investor_investments.amount END),2) as "Principal Amount",
investor_investments.investor_id as "IID",
investor_investments.investment_date as "Transaction Date",
investor_investments.investment_id as "Investment ID",
investor_investments.investment_source as "Investment Type",
IF(investor_investments.investment_type = 1, "RPA Receipt",
    IF(investor_investments.investment_type = 2, "Coupon",
        IF(investor_investments.investment_type = 3, "Partial Redemption",
            IF(investor_investments.investment_type = 4, "Full Redemption",
                IF(investor_investments.investment_type = 5, "Compound",
                    IF(investor_investments.investment_type = 8, "Addition",
                        IF(investor_investments.investment_type = 9, "Renewal",
                           IF(investor_investments.investment_type = 11, "Partial Coupon","")))))))) as "Transaction Type",
investor_investments.investment_group as "Investment Group" 
FROM investor_investments
LEFT JOIN temp_registration as investor ON investor.user_id = investor_investments.investor_id 
LEFT JOIN currencies ON currencies.currency_id = investor_investments.currency_id 
LEFT JOIN currency_rates cr ON cr.currency_id = investor_investments.currency_id AND cr.currency_date = investor_investments.investment_date
LEFT JOIN transaction_types ON transaction_types.type_id=1 AND transaction_types.transaction_id = investor_investments.investment_type 
LEFT JOIN investor_relation ON investor_relation.investor_id = investor.temp_reg_id 
LEFT JOIN temp_registration as intermediary ON intermediary.user_id = investor_relation.intermediary_id 
LEFT JOIN contact_details ON contact_details.temp_reg_id = investor.temp_reg_id  AND (contact_details.contact_flag=3 OR (contact_details.contact_flag=0 AND contact_details.bank_id=0)) 
WHERE investor_investments.status IN (1,0) AND investor_investments.investment_type IN (1,3,4,8) 
	AND investor_investments.investment_group IN (
		SELECT ALL investment_group FROM investor_investments 
		WHERE investor_investments.status IN (1,0) AND investor_investments.investment_type IN (1,8) AND investor_investments.investment_source="LB" 
		GROUP BY investor_investments.investment_id)
GROUP BY investor_investments.investment_id ) as B
ORDER BY "Investment Group" ASC