subquery must return onl one column in postgresql I want to get the XML response after the stored procedure call in PostgreSQL. In the below query I was trying to get the output, but it throws an Error: subquery must return only one column. Can someone help me with this?
SELECT (
        SELECT xmlforest('2021_PPP_Parasiticide_Program' AS Name, CASE 
                    WHEN &quot;temptable&quot;.&quot;C_2021_PPP_PARA&quot; IS NOT NULL
                        THEN 'True'
                    ELSE 'False'
                    END AS requirementsMe, coalesce(&quot;temptable&quot;.&quot;YTD_2021_Qualifying_Carton_Purchased&quot;, 0) AS qualifyingPurchaseAmount, 'Dollar' AS qualifyingPurchaseAmountType, CASE 
                    WHEN &quot;temptable&quot;.&quot;C_2021_PPP_PARA&quot; IS NOT NULL
                        THEN 'Active'
                    ELSE 'In-Active'
                    END AS componentStatus)
            ,(
                SELECT (
                        SELECT xmlforest('q1_parasiticide_carton_rebate' AS rewardName, CASE 
                                    WHEN q1_parasiticide_carton_rebate IS NULL
                                        THEN CAST(0 AS VARCHAR)
                                    ELSE cast(coalesce(q1_parasiticide_carton_rebate, 0) AS VARCHAR(10))
                                    END AS rewardAmount)
                        FROM temptable
                        )
                    ,(
                        SELECT xmlforest('q2_parasiticide_carton_rebate' AS rewardName, CASE 
                                    WHEN q2_parasiticide_carton_rebate IS NULL
                                        THEN CAST(0 AS VARCHAR)
                                    ELSE cast(coalesce(q2_parasiticide_carton_rebate, 0) AS VARCHAR(10))
                                    END AS rewardAmount)
                        FROM temptable
                        WHERE temptable.rus_id = temptable.rus_id
                        )
                FROM temptable
                )
        FROM temptable
        );

I want response in the below format -
&lt;CustomerProgramStatus_response&gt;
   &lt;programComponents&gt;
      &lt;name&gt;2020_PPP_Parasiticide_Program&lt;/name&gt;
      &lt;requirementsMet&gt;FALSE&lt;/requirementsMet&gt;
      &lt;qualifyingPurchaseAmount&gt;0&lt;/qualifyingPurchaseAmount&gt;
      &lt;qualifyingPurchaseAmountType&gt;Dollar&lt;/qualifyingPurchaseAmountType&gt;
      &lt;componentStatus&gt;In-Active&lt;/componentStatus&gt;
      &lt;componentRewards&gt;
         &lt;rewardName&gt;Q1_Parasiticide_Carton_Rebate&lt;/rewardName&gt;
         &lt;rewardAmount&gt;0&lt;/rewardAmount&gt;
      &lt;/componentRewards&gt;
      &lt;componentRewards&gt;
         &lt;rewardName&gt;Q2_Parasiticide_Carton_Rebate&lt;/rewardName&gt;
         &lt;rewardAmount&gt;0&lt;/rewardAmount&gt;
      &lt;/componentRewards&gt;
   &lt;/programComponents&gt;
&lt;/CustomerProgramStatus_response&gt;
If temptable has one record, then you probably dont need subqueries at all. It it has more the one, then you need aggregates. Probably the best is not to provide more info, but study some examples like here: https://www.w3resource.com/PostgreSQL/postgresql-xml-functions.php and build your xml from inside moving outward step by step like
1:
SELECT xmlforest('q1_parasiticide_carton_rebate' AS &quot;rewardName&quot;,
    CASE WHEN q1_parasiticide_carton_rebate IS NULL
    THEN CAST(0 AS VARCHAR)
    ELSE cast(coalesce(q1_parasiticide_carton_rebate, 0) AS VARCHAR(10))
    END AS rewardAmount)
FROM temptable;

2:
SELECT xmlforest(xmlforest('q1_parasiticide_carton_rebate' AS &quot;rewardName&quot;,
    CASE WHEN q1_parasiticide_carton_rebate IS NULL
    THEN CAST(0 AS VARCHAR)
    ELSE cast(coalesce(q1_parasiticide_carton_rebate, 0) AS VARCHAR(10))
    END AS rewardAmount) as &quot;componentRewards&quot;,
xmlforest('q2_parasiticide_carton_rebate' AS rewardName,
    CASE WHEN q2_parasiticide_carton_rebate IS NULL
    THEN CAST(0 AS VARCHAR)
    ELSE cast(coalesce(q2_parasiticide_carton_rebate, 0) AS VARCHAR(10))
    END AS rewardAmount) as &quot;componentRewards&quot;)
FROM temptable;