title: How to resolve error: return columns doesn't match expected columns?
body: <p>I write function in PostgreSQL. I am trying to fetch data from db by passing parameters in function .But i am getting error" ERROR: structure of query does not match function result type".
  Here i created a function in which i am using union to get unique values.In first select stat. i amsetting value for account total that is used in below code.</p>
  
  <p><strong>Function:</strong></p>
  
  <pre><code>        CREATE OR REPLACE FUNCTION GetDeptListForViewModifyJointUsePercentages ( p_nInstID numeric,p_nDeptID numeric)  
      RETURNS Table(
      res_ndept_id numeric,
      res_salaryPercent character varying,
      res_nclient_cpc_mapping_id numeric, 
      res_CPCCODE character varying, 
      res_sdept_name character varying, 
      res_sclient_dept_id character varying, 
      res_sAlternateJointUsePercentage character varying
      )
      AS  $$  
      declare v_AccountTotal numeric(18,2);
      BEGIN
      RETURN QUERY 
      select SUM(CAST (coalesce(eam.npayroll_amt, null) AS numeric(18,2))) as AccountTotal
              FROM Account acct
                  INNER JOIN employeeaccountmapping eam 
                      ON eam.nacct_id = acct.naccount_id
                      AND acct.ninst_id =p_nInstID
                where acct.ndept_id =p_nDeptID ;
  
      SELECT  * 
      FROM 
          (select dep.ndept_id ,( CASE
                          WHEN v_AccountTotal =0 THEN 0
                          ELSE Round(SUM(CAST(coalesce(eam.npayroll_amt, null) AS numeric(18,2)) * cac.npercentage_assigned/100)  / v_AccountTotal *100,null)  
                          END     
                      ) as salaryPercent
          ,cac.nclient_cpc_mapping_id,client.sclient_cpc AS CPCCODE,
          dep.sdept_name,dep.sclient_dept_id,'NO' ASsAlternateJointUsePercentage
  
          FROM account 
          INNER JOIN employeeaccountmapping eam 
          ON eam.nacct_id = account .naccount_id AND account .ninst_id=p_nInstID 
          INNER JOIN accountcpcmapping acm 
          ON acm.naccount_cpc_mapping_id = account.naccount_cpc_mapping_id 
          INNER JOIN cpcaccountcpcmapping as cac
          ON cac.naccount_cpc_mapping_id=acm.naccount_cpc_mapping_id
          INNER JOIN clientcostpoolcodes as client
          ON client.nclient_cpc_mapping_id=cac.nclient_cpc_mapping_id
          INNER JOIN mastercostpoolcodes 
          ON mastercostpoolcodes .nmaster_cpc_id= client.nmaster_cpc_id
          INNER JOIN department as dep
          ON account.ndept_id=dep.ndept_id and dep.balternate_jointuse_percentage=FALSE
          where account.ndept_id =p_nDeptID 
          Group by dep.ndept_id,cac.nclient_cpc_mapping_id,client.sclient_cpc
          ,dep.sdept_name,dep.sclient_dept_id, dep.balternate_jointuse_percentage
          UNION
          SELECT     Department_1.ndept_id, a_1.SumByCPC, clientcostpoolcodes.nclient_cpc_mapping_id, clientcostpoolcodes .sclient_cpc, Department_1.sdept_name, Department_1.sclient_dept_id , 'YES' AS sAlternateJointUsePercentage 
  
          FROM   department AS Department_1 LEFT OUTER JOIN
                 mastercostpoolcodes RIGHT OUTER JOIN
                 clientcostpoolcodes RIGHT OUTER JOIN
                                (SELECT   JointUseStatistics_1.nccp_code, SUM(JointUseStatistics_1.npercent) /
                                                             (SELECT  SUM(jointusestatistics.npercent) AS SumByCPC
                                                             FROM          jointusestatistics INNER JOIN
                                                                                      roomdepartmentmapping ON jointusestatistics.nroom_allocation_id = roomdepartmentmapping .nroom_allocation_id
                                                               WHERE      (roomdepartmentmapping .ndept_id = Room_1.ndept_id)) * 100 AS SumByCPC, 
                                                            Room_1.ndept_id
                                    FROM jointusestatistics JointUseStatistics_1 INNER JOIN roomdepartmentmapping  AS Room_1 ON JointUseStatistics_1.nroom_allocation_id = Room_1.nroom_allocation_id
    GROUP BY JointUseStatistics_1.nccp_code, JointUseStatistics_1.npercent, Room_1.ndept_id) AS a_1 ON 
            clientcostpoolcodes .nclient_cpc_mapping_id = a_1.nccp_code ON mastercostpoolcodes .nmaster_cpc_id = clientcostpoolcodes .nmaster_cpc_id ON 
            Department_1.ndept_id = a_1.ndept_id                      
          WHERE     (Department_1.balternate_jointuse_percentage = 'TRUE')
                  AND (Department_1.ninst_id= p_nInstID)
      )AS My 
  
      where (ndept_id= p_nDeptID )
      ORDER BY My.sdept_name,My.CPCCODE ;
      END;    
      $$ LANGUAGE plpgsql;
  </code></pre>