Artificial Intelligence
Please fill the required field.

modify the below code which should give sum of Current_Stock_Value when SalesOrganization=1100 SELECT "Projection 4_0"."FRCST_MONTH" AS "Projection 4-FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED" AS "Projection 4-FRCST_VALUE_COST_AED", "


To modify your SQL query for calculating the sum of `Current_Stock_Value` specifically for `SalesOrganization = '1100'`, you can use a `WHERE` clause in the outermost `SELECT` statement and also modify the final `SELECT` statement accordingly. Here’s how you can do it: ```sql SELECT "Projection 4_0"."FRCST_MONTH" AS "Projection 4-FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED" AS "Projection 4-FRCST_VALUE_COST_AED", "Aggregation 5_1"."YEAR_MONTH_OPN_PO" AS "Aggregation 5-YEAR_MONTH_OPN_PO", SUM("Projection 4_0"."Current_Stock_Value") AS "Total_Current_Stock_Value", -- Changed to SUM "Aggregation 5_1"."YEAR_MONTH_PO" AS "Join_Mergedsources-YEAR_MONTH_PO", "Projection 4_0"."CUR_YEAR_MONTH" AS "Projection 4-CUR_YEAR_MONTH", "Projection 4_0"."Agency" AS "Projection 4-Agency", "Projection 4_0"."AgencyGroup" AS "Projection 4-AgencyGroup", "Projection 4_0"."AgencyBySales" AS "Projection 4-AgencyBySales", "Aggregation 5_1"."YEAR_DATE_OPEN_PO" AS "Aggregation 5-YEAR_DATE_OPEN_PO", "Aggregation 5_1"."PENDING_ARRIVALS" AS "Aggregation 5-PENDING_ARRIVALS", "Projection 4_0"."SalesOrganization" AS "Projection 4-SalesOrganization", "Projection 4_0"."YEAR_DATE" AS "1-YEAR_DATE", "Projection 4_0"."Year" AS "Projection 4-Year", "Projection 4_0"."Month" AS "Projection 4-Month", "Aggregation 5_1"."Agncy" AS "Aggregation 5-Agncy", "Aggregation 5_1"."Agency_Group" AS "Aggregation 5-Agency_Group", "Aggregation 5_1"."Agency_By_Sales" AS "Aggregation 5-Agency_By_Sales", "Aggregation 5_1"."Sales_Organization" AS "Aggregation 5-Sales_Organization", "Aggregation 5_1"."Year_PO" AS "Aggregation 5-Year_PO", "Aggregation 5_1"."Month_PO" AS "Aggregation 5-Month_PO" FROM ( SELECT "Aggregation 4_3"."FRCST_MONTH" AS "FRCST_MONTH", "Aggregation 4_3"."FRCST_VALUE_COST_AED" AS "FRCST_VALUE_COST_AED", "Projection 5_2"."Current_Stock_Value" AS "Current_Stock_Value", CASE WHEN "Projection 5_2"."Agncy_Tblstk" IS NULL THEN "Aggregation 4_3"."Agncy_Frcst" ELSE "Projection 5_2"."Agncy_Tblstk" END AS "Agency", "Projection 5_2"."CUR_YEAR_MONTH" AS "CUR_YEAR_MONTH", CASE WHEN "Projection 5_2"."AgencyGroup_Tblstk" IS NULL THEN "Aggregation 4_3"."AgencyGroup_Frcst" ELSE "Projection 5_2"."AgencyGroup_Tblstk" END AS "AgencyGroup", CASE WHEN "Projection 5_2"."AgencyBySales_Tblstk" IS NULL THEN "Aggregation 4_3"."AgencyBySales_Frcst" ELSE "Projection 5_2"."AgencyBySales_Tblstk" END AS "AgencyBySales", CASE WHEN "Projection 5_2"."MNTH_END_STK_SALES_ORG" IS NULL THEN "Aggregation 4_3"."VKORG" ELSE "Projection 5_2"."MNTH_END_STK_SALES_ORG" END AS "SalesOrganization", CASE WHEN "Projection 5_2"."MNTH_END_STK_FSCL_YR" IS NULL THEN "Aggregation 4_3"."FRCST_YEAR" ELSE "Projection 5_2"."MNTH_END_STK_FSCL_YR" END AS "Year", CASE WHEN "Projection 5_2"."MNTH_END_STK_FSCL_MNTH" IS NULL THEN "Aggregation 4_3"."FRCST_MONTH" ELSE "Projection 5_2"."MNTH_END_STK_FSCL_MNTH" END AS "Month", "Aggregation 4_3"."YEAR_DATE" AS "YEAR_DATE" FROM ( SELECT "Aggregation 6_4"."ARTI_AGNCY_CD" AS "Agncy_Tblstk", "Aggregation 6_4"."AGENCYBYSALES" AS "AgencyBySales_Tblstk", "Aggregation 6_4"."AGENCYGROUP" AS "AgencyGroup_Tblstk", "Aggregation 6_4"."MNTH_END_STK_FSCL_YR" AS "MNTH_END_STK_FSCL_YR", "Aggregation 6_4"."CUR_YEAR_MONTH" AS "CUR_YEAR_MONTH", "Aggregation 6_4"."MNTH_END_STK_FSCL_MNTH" AS "MNTH_END_STK_FSCL_MNTH", "Aggregation 6_4"."MNTH_END_STK_SALES_ORG" AS "MNTH_END_STK_SALES_ORG", "Aggregation 6_4"."Current_Stock_Value" AS "Current_Stock_Value" FROM ( SELECT "ZMM_HL_VR_TBLMNTH_STK_9"."SALESORGNAME" AS "SALESORGNAME", "ZMM_HL_VR_TBLMNTH_STK_9"."ARTI_AGNCY_CD" AS "ARTI_AGNCY_CD", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYBYSALES" AS "AGENCYBYSALES", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYGROUP" AS "AGENCYGROUP", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR" AS "MNTH_END_STK_FSCL_YR", TO_DATE("ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR" || "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH" || '01') AS "CUR_YEAR_MONTH", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH" AS "MNTH_END_STK_FSCL_MNTH", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_SALES_ORG" AS "MNTH_END_STK_SALES_ORG", SUM("ZMM_HL_VR_TBLMNTH_STK_9"."Current_Stock_Value") AS "Current_Stock_Value" FROM "ZMM_HL_VR_TBLMNTH_STK" AS "ZMM_HL_VR_TBLMNTH_STK_9" GROUP BY "ZMM_HL_VR_TBLMNTH_STK_9"."SALESORGNAME", "ZMM_HL_VR_TBLMNTH_STK_9"."ARTI_AGNCY_CD", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYBYSALES", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYGROUP", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR", TO_DATE("ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR" || "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH" || '01'), "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_SALES_ORG" ) AS "Aggregation 6_4" ) AS "Projection 5_2" FULL JOIN ( SELECT YEAR("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE") AS "FRCST_YEAR", MONTH("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE") AS "FRCST_MONTH", SUM("ZSD_HL_VR_CURR_COST_FORECAST_10"."FRCST_VALUE_COST_AED") AS "FRCST_VALUE_COST_AED", "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYGROUP" AS "AgencyGroup_Frcst", "ZSD_HL_VR_CURR_COST_FORECAST_10"."VKORG" AS "VKORG", "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYBYSALES" AS "AgencyBySales_Frcst", "ZSD_HL_VR_CURR_COST_FORECAST_10"."Agncy" AS "Agncy_Frcst", "ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE" AS "YEAR_DATE" FROM "ZSD_HL_VR_CURR_COST_FORECAST" AS "ZSD_HL_VR_CURR_COST_FORECAST_10" WHERE "ZSD_HL_VR_CURR_COST_FORECAST_10"."FRCST_YR" >= YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_FORECAST_10"."FRCST_MNTH" >= MONTH(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE" <= ADD_MONTHS(CURRENT_DATE, 3) GROUP BY YEAR("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE"), MONTH("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE"), "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYGROUP", "ZSD_HL_VR_CURR_COST_FORECAST_10"."VKORG", "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYBYSALES", "ZSD_HL_VR_CURR_COST_FORECAST_10"."Agncy", "ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE" ) AS "Aggregation 4_3" ON "Projection 5_2"."MNTH_END_STK_FSCL_YR" = "Aggregation 4_3"."FRCST_YEAR" AND "Projection 5_2"."MNTH_END_STK_FSCL_MNTH" = "Aggregation 4_3"."FRCST_MONTH" AND "Projection 5_2"."Agncy_Tblstk" = "Aggregation 4_3"."Agncy_Frcst" AND "Projection 5_2"."AgencyBySales_Tblstk" = "Aggregation 4_3"."AgencyBySales_Frcst" AND "Projection 5_2"."AgencyGroup_Tblstk" = "Aggregation 4_3"."AgencyGroup_Frcst" AND "Projection 5_2"."MNTH_END_STK_SALES_ORG" = "Aggregation 4_3"."VKORG" ) AS "Projection 4_0" FULL JOIN ( SELECT "Calculated Columns 6_5"."YEAR_MONTH_PO" AS "YEAR_MONTH_PO", SUM(COALESCE("Calculated Columns 6_5"."OPN_OPN_PO_VALUE_COST_AED", 0) + COALESCE("Calculated Columns 11_6"."PO_RCPT_NET_VAL_COST_AED", 0)) AS "PENDING_ARRIVALS", "Calculated Columns 11_6"."YEAR_DATE_OPEN_PO" AS "YEAR_DATE_OPEN_PO", "Calculated Columns 6_5"."YEAR_MONTH_OPN_PO" AS "YEAR_MONTH_OPN_PO", CASE WHEN "Calculated Columns 6_5"."Agncy_Opnpo" IS NULL THEN "Calculated Columns 11_6"."Agncy_Po" ELSE "Calculated Columns 6_5"."Agncy_Opnpo" END AS "Agncy", CASE WHEN "Calculated Columns 6_5"."AgencyGroup_Opnpo" IS NULL THEN "Calculated Columns 11_6"."AgencyGroup_Po" ELSE "Calculated Columns 6_5"."AgencyGroup_Opnpo" END AS "Agency_Group", CASE WHEN "Calculated Columns 6_5"."AgencyBySales_Opnpo" IS NULL THEN "Calculated Columns 11_6"."AgencyBySales_Po" ELSE "Calculated Columns 6_5"."AgencyBySales_Opnpo" END AS "Agency_By_Sales", CASE WHEN "Calculated Columns 6_5"."SalesOrganization_Opnpo" IS NULL THEN "Calculated Columns 11_6"."SalesOrganization_Po" ELSE "Calculated Columns 6_5"."SalesOrganization_Opnpo" END AS "Sales_Organization", CASE WHEN "Calculated Columns 6_5"."YEAR" IS NULL THEN "Calculated Columns 11_6"."Year_Po" ELSE "Calculated Columns 6_5"."YEAR" END AS "Year_PO", CASE WHEN "Calculated Columns 6_5"."MONTH" IS NULL THEN "Calculated Columns 11_6"."Month_Po" ELSE "Calculated Columns 6_5"."MONTH" END AS "Month_PO" FROM ( SELECT "Aggregation 3_7"."Agncy" AS "Agncy_Opnpo", "Aggregation 3_7"."AGENCYGROUP" AS "AgencyGroup_Opnpo", "Aggregation 3_7"."AGENCYBYSALES" AS "AgencyBySales_Opnpo", left("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 4) AS "YEAR", right("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 2) AS "MONTH", "Aggregation 3_7"."YEAR_MONTH_OPN_PO" AS "YEAR_MONTH_OPN_PO", "Aggregation 3_7"."SALESORG" AS "SalesOrganization_Opnpo", TO_DATE(left("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 4) || right("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 2) || '01') AS "YEAR_MONTH_PO", "Aggregation 3_7"."OPN_OPN_PO_VALUE_COST_AED" AS "OPN_OPN_PO_VALUE_COST_AED" FROM ( SELECT "ZSD_HL_VR_CURR_COST_OPN_PO_11"."Agncy" AS "Agncy", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYGROUP" AS "AGENCYGROUP", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYBYSALES" AS "AGENCYBYSALES", TO_VARCHAR(CASE WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" < YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= 12 THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" <= YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= MONTH(CURRENT_DATE) THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) ELSE "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" || "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" END) AS "YEAR_MONTH_OPN_PO", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."SALESORG" AS "SALESORG", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" AS "YEAR", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" AS "MONTH", SUM("ZSD_HL_VR_CURR_COST_OPN_PO_11"."OPN_OPN_PO_VALUE_COST_AED") AS "OPN_OPN_PO_VALUE_COST_AED" FROM "ZSD_HL_VR_CURR_COST_OPN_PO" AS "ZSD_HL_VR_CURR_COST_OPN_PO_11" GROUP BY "ZSD_HL_VR_CURR_COST_OPN_PO_11"."Agncy", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYGROUP", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYBYSALES", TO_VARCHAR(CASE WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" < YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= 12 THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" <= YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= MONTH(CURRENT_DATE) THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) ELSE "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" || "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" END), "ZSD_HL_VR_CURR_COST_OPN_PO_11"."SALESORG", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" ) AS "Aggregation 3_7" ) AS "Calculated Columns 6_5" FULL JOIN ( SELECT "Aggregation 2_8"."PO_RCPT_NET_VAL_COST_AED" AS "PO_RCPT_NET_VAL_COST_AED", "Aggregation 2_8"."SALESORGCODE" AS "SalesOrganization_Po", "Aggregation 2_8"."AGENCYGROUP" AS "AgencyGroup_Po", "Aggregation 2_8"."AGENCYBYSALES" AS "AgencyBySales_Po", "Aggregation 2_8"."Agncy" AS "Agncy_Po", TO_DATE("Aggregation 2_8"."YEAR" || "Aggregation 2_8"."MONTH" || '01') AS "YEAR_DATE_OPEN_PO", "Aggregation 2_8"."YEAR" AS "Year_Po", "Aggregation 2_8"."MONTH" AS "Month_Po" FROM ( SELECT SUM("ZSD_HL_VR_CURR_COST_PO_RCPT_12"."PO_RCPT_NET_VAL_COST_AED") AS "PO_RCPT_NET_VAL_COST_AED", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."SALESORGCODE" AS "SALESORGCODE", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYGROUP" AS "AGENCYGROUP", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYBYSALES" AS "AGENCYBYSALES", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."Agncy" AS "Agncy", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."YEAR" AS "YEAR", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."MONTH" AS "MONTH" FROM "ZSD_HL_VR_CURR_COST_PO_RCPT" AS "ZSD_HL_VR_CURR_COST_PO_RCPT_12" GROUP BY "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."SALESORGCODE", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYGROUP", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYBYSALES", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."Agncy", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."YEAR", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."MONTH" ) AS "Aggregation 2_8" ) AS "Calculated Columns 11_6" ON "Calculated Columns 6_5"."Agncy_Opnpo" = "Calculated Columns 11_6"."Agncy_Po" AND "Calculated Columns 6_5"."AgencyGroup_Opnpo" = "Calculated Columns 11_6"."AgencyGroup_Po" AND "Calculated Columns 6_5"."SalesOrganization_Opnpo" = "Calculated Columns 11_6"."SalesOrganization_Po" AND "Calculated Columns 6_5"."AgencyBySales_Opnpo" = "Calculated Columns 11_6"."AgencyBySales_Po" GROUP BY "Calculated Columns 6_5"."YEAR_MONTH_PO", "Calculated Columns 11_6"."YEAR_DATE_OPEN_PO", "Calculated Columns 6_5"."YEAR_MONTH_OPN_PO", CASE WHEN "Calculated Columns 6_5"."Agncy_Opnpo" IS NULL THEN "Calculated Columns 11_6"."Agncy_Po" ELSE "Calculated Columns 6_5"."Agncy_Opnpo" END, CASE WHEN "Calculated Columns 6_5"."AgencyGroup_Opnpo" IS NULL THEN "Calculated Columns 11_6"."AgencyGroup_Po" ELSE "Calculated Columns 6_5"."AgencyGroup_Opnpo" END, CASE WHEN "Calculated Columns 6_5"."AgencyBySales_Opnpo" IS NULL THEN "Calculated Columns 11_6"."AgencyBySales_Po" ELSE "Calculated Columns 6_5"."AgencyBySales_Opnpo" END, CASE WHEN "Calculated Columns 6_5"."SalesOrganization_Opnpo" IS NULL THEN "Calculated Columns 11_6"."SalesOrganization_Po" ELSE "Calculated Columns 6_5"."SalesOrganization_Opnpo" END, CASE WHEN "Calculated Columns 6_5"."YEAR" IS NULL THEN "Calculated Columns 11_6"."Year_Po" ELSE "Calculated Columns 6_5"."YEAR" END, CASE WHEN "Calculated Columns 6_5"."MONTH" IS NULL THEN "Calculated Columns 11_6"."Month_Po" ELSE "Calculated Columns 6_5"."MONTH" END ) AS "Aggregation 5_1" ON "Projection 4_0"."Agency" = "Aggregation 5_1"."Agncy" AND "Projection 4_0"."AgencyGroup" = "Aggregation 5_1"."Agency_Group" AND "Projection 4_0"."AgencyBySales" = "Aggregation 5_1"."Agency_By_Sales" AND "Projection 4_0"."SalesOrganization" = "Aggregation 5_1"."Sales_Organization" AND "Projection 4_0"."Year" = "Aggregation 5_1"."Year_PO" AND "Projection 4_0"."Month" = "Aggregation 5_1"."Month_PO" WHERE "Projection 4_0"."SalesOrganization" = '1100' -- Filtering on SalesOrganization GROUP BY "Projection 4_0"."FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED", "Aggregation 5_1"."YEAR_MONTH_OPN_PO", "Aggregation 5_1"."YEAR_DATE_OPEN_PO", "Aggregation 5_1"."PENDING_ARRIVALS", "Projection 4_0"."CUR_YEAR_MONTH", "Projection 4_0"."Agency", "Projection 4_0"."AgencyGroup", "Projection 4_0"."AgencyBySales", "Projection 4_0"."SalesOrganization", "Projection 4_0"."YEAR_DATE", "Projection 4_0"."Year", "Projection 4_0"."Month", "Aggregation 5_1"."Agncy", "Aggregation 5_1"."Agency_Group", "Aggregation 5_1"."Agency_By_Sales", "Aggregation 5_1"."Sales_Organization", "Aggregation 5_1"."Year_PO", "Aggregation 5_1"."Month_PO"; ``` ### Key Changes Made: 1. Changed the `SELECT` clause in the main part of the query to sum `Current_Stock_Value` specifically for the `SalesOrganization` that equals '1100'. 2. Added a `WHERE` clause to filter for `SalesOrganization = '1100'` at the end of the main query. 3. Retained the necessary grouping for the `SUM` function so that the SQL remains valid. Make sure to run the query in your SQL environment to confirm correctness and performance. Update (2024-12-22): To modify the SQL query so that it calculates the sum of `Current_Stock_Value` when `SalesOrganization = 1100`, we need to adjust the main SELECT statement to group by the necessary fields and add a conditional SUM clause for `Current_Stock_Value`. Here's the modified code: ```sql SELECT "Projection 4_0"."FRCST_MONTH" AS "Projection 4-FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED" AS "Projection 4-FRCST_VALUE_COST_AED", "Aggregation 5_1"."YEAR_MONTH_OPN_PO" AS "Aggregation 5-YEAR_MONTH_OPN_PO", SUM(CASE WHEN "Projection 4_0"."SalesOrganization" = 1100 THEN "Projection 4_0"."Current_Stock_Value" ELSE 0 END) AS "Total_Current_Stock_Value", -- Change made here "Aggregation 5_1"."YEAR_MONTH_PO" AS "Join_Mergedsources-YEAR_MONTH_PO", "Projection 4_0"."CUR_YEAR_MONTH" AS "Projection 4-CUR_YEAR_MONTH", "Projection 4_0"."Agency" AS "Projection 4-Agency", "Projection 4_0"."AgencyGroup" AS "Projection 4-AgencyGroup", "Projection 4_0"."AgencyBySales" AS "Projection 4-AgencyBySales", "Aggregation 5_1"."YEAR_DATE_OPEN_PO" AS "Aggregation 5-YEAR_DATE_OPEN_PO", "Aggregation 5_1"."PENDING_ARRIVALS" AS "Aggregation 5-PENDING_ARRIVALS", "Projection 4_0"."SalesOrganization" AS "Projection 4-SalesOrganization", "Projection 4_0"."YEAR_DATE" AS "1-YEAR_DATE", "Projection 4_0"."Year" AS "Projection 4-Year", "Projection 4_0"."Month" AS "Projection 4-Month", "Aggregation 5_1"."Agncy" AS "Aggregation 5-Agncy", "Aggregation 5_1"."Agency_Group" AS "Aggregation 5-Agency_Group", "Aggregation 5_1"."Agency_By_Sales" AS "Aggregation 5-Agency_By_Sales", "Aggregation 5_1"."Sales_Organization" AS "Aggregation 5-Sales_Organization", "Aggregation 5_1"."Year_PO" AS "Aggregation 5-Year_PO", "Aggregation 5_1"."Month_PO" AS "Aggregation 5-Month_PO" FROM ( -- Sub-query remains the same as in your original query (SELECT -- Your existing fields here ... ) ) AS "Projection 4_0" FULL JOIN ( -- Sub-query for Aggregation 5_1 remains the same (SELECT -- Your existing fields here ... ) ) AS "Aggregation 5_1" ON "Projection 4_0"."Agency" = "Aggregation 5_1"."Agncy" AND "Projection 4_0"."AgencyGroup" = "Aggregation 5_1"."Agency_Group" AND "Projection 4_0"."AgencyBySales" = "Aggregation 5_1"."Agency_By_Sales" AND "Projection 4_0"."SalesOrganization" = "Aggregation 5_1"."Sales_Organization" AND "Projection 4_0"."Year" = "Aggregation 5_1"."Year_PO" AND "Projection 4_0"."Month" = "Aggregation 5_1"."Month_PO" GROUP BY "Projection 4_0"."FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED", "Aggregation 5_1"."YEAR_MONTH_OPN_PO", "Aggregation 5_1"."YEAR_MONTH_PO", "Projection 4_0"."CUR_YEAR_MONTH", "Projection 4_0"."Agency", "Projection 4_0"."AgencyGroup", "Projection 4_0"."AgencyBySales", "Aggregation 5_1"."YEAR_DATE_OPEN_PO", "Aggregation 5_1"."PENDING_ARRIVALS", "Projection 4_0"."SalesOrganization", "Projection 4_0"."YEAR_DATE", "Projection 4_0"."Year", "Projection 4_0"."Month", "Aggregation 5_1"."Agncy", "Aggregation 5_1"."Agency_Group", "Aggregation 5_1"."Agency_By_Sales", "Aggregation 5_1"."Sales_Organization", "Aggregation 5_1"."Year_PO", "Aggregation 5_1"."Month_PO"; ``` ### Key Changes Made: 1. A new column `Total_Current_Stock_Value` has been added to calculate the sum of `Current_Stock_Value` where `SalesOrganization` equals `1100`. This is done using a conditional `SUM` with a `CASE` statement. 2. The query has been wrapped in a `GROUP BY` clause to ensure proper aggregation while selecting the other columns. Ensure that the subqueries containing the necessary data and the aggregations are consistent with your requirements and that they yield the correct results. Update (2024-12-22): To modify the provided SQL code so that it sums the `Current_Stock_Value` specifically when the `SalesOrganization` equals `1100`, you need to incorporate a `SUM` function combined with a `CASE` statement or a `WHERE` clause to filter the data accordingly. Below is a modified version of the SQL code that achieves this. Please note that I have added a new column to the result set called `Sum_Current_Stock_Value`, which reflects the sum of the `Current_Stock_Value` where `SalesOrganization = 1100`. Additionally, wherever there is aggregation or summation, you should ensure the grouping is handled accordingly as well. ```sql SELECT "Projection 4_0"."FRCST_MONTH" AS "Projection 4-FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED" AS "Projection 4-FRCST_VALUE_COST_AED", "Aggregation 5_1"."YEAR_MONTH_OPN_PO" AS "Aggregation 5-YEAR_MONTH_OPN_PO", SUM(CASE WHEN "Projection 4_0"."SalesOrganization" = 1100 THEN "Projection 4_0"."Current_Stock_Value" ELSE 0 END) AS "Sum_Current_Stock_Value", "Aggregation 5_1"."YEAR_MONTH_PO" AS "Join_Mergedsources-YEAR_MONTH_PO", "Projection 4_0"."CUR_YEAR_MONTH" AS "Projection 4-CUR_YEAR_MONTH", "Projection 4_0"."Agency" AS "Projection 4-Agency", "Projection 4_0"."AgencyGroup" AS "Projection 4-AgencyGroup", "Projection 4_0"."AgencyBySales" AS "Projection 4-AgencyBySales", "Aggregation 5_1"."YEAR_DATE_OPEN_PO" AS "Aggregation 5-YEAR_DATE_OPEN_PO", "Aggregation 5_1"."PENDING_ARRIVALS" AS "Aggregation 5-PENDING_ARRIVALS", "Projection 4_0"."SalesOrganization" AS "Projection 4-SalesOrganization", "Projection 4_0"."YEAR_DATE" AS "1-YEAR_DATE", "Projection 4_0"."Year" AS "Projection 4-Year", "Projection 4_0"."Month" AS "Projection 4-Month", "Aggregation 5_1"."Agncy" AS "Aggregation 5-Agncy", "Aggregation 5_1"."Agency_Group" AS "Aggregation 5-Agency_Group", "Aggregation 5_1"."Agency_By_Sales" AS "Aggregation 5-Agency_By_Sales", "Aggregation 5_1"."Sales_Organization" AS "Aggregation 5-Sales_Organization", "Aggregation 5_1"."Year_PO" AS "Aggregation 5-Year_PO", "Aggregation 5_1"."Month_PO" AS "Aggregation 5-Month_PO" FROM ( SELECT "Aggregation 4_3"."FRCST_MONTH" AS "FRCST_MONTH", "Aggregation 4_3"."FRCST_VALUE_COST_AED" AS "FRCST_VALUE_COST_AED", "Projection 5_2"."Current_Stock_Value" AS "Current_Stock_Value", CASE WHEN "Projection 5_2"."Agncy_Tblstk" IS NULL THEN "Aggregation 4_3"."Agncy_Frcst" ELSE "Projection 5_2"."Agncy_Tblstk" END AS "Agency", "Projection 5_2"."CUR_YEAR_MONTH" AS "CUR_YEAR_MONTH", CASE WHEN "Projection 5_2"."AgencyGroup_Tblstk" IS NULL THEN "Aggregation 4_3"."AgencyGroup_Frcst" ELSE "Projection 5_2"."AgencyGroup_Tblstk" END AS "AgencyGroup", CASE WHEN "Projection 5_2"."AgencyBySales_Tblstk" IS NULL THEN "Aggregation 4_3"."AgencyBySales_Frcst" ELSE "Projection 5_2"."AgencyBySales_Tblstk" END AS "AgencyBySales", CASE WHEN "Projection 5_2"."MNTH_END_STK_SALES_ORG" IS NULL THEN "Aggregation 4_3"."VKORG" ELSE "Projection 5_2"."MNTH_END_STK_SALES_ORG" END AS "SalesOrganization", CASE WHEN "Projection 5_2"."MNTH_END_STK_FSCL_YR" IS NULL THEN "Aggregation 4_3"."FRCST_YEAR" ELSE "Projection 5_2"."MNTH_END_STK_FSCL_YR" END AS "Year", CASE WHEN "Projection 5_2"."MNTH_END_STK_FSCL_MNTH" IS NULL THEN "Aggregation 4_3"."FRCST_MONTH" ELSE "Projection 5_2"."MNTH_END_STK_FSCL_MNTH" END AS "Month", "Aggregation 4_3"."YEAR_DATE" AS "YEAR_DATE" FROM ( SELECT "Aggregation 6_4"."ARTI_AGNCY_CD" AS "Agncy_Tblstk", "Aggregation 6_4"."AGENCYBYSALES" AS "AgencyBySales_Tblstk", "Aggregation 6_4"."AGENCYGROUP" AS "AgencyGroup_Tblstk", "Aggregation 6_4"."MNTH_END_STK_FSCL_YR" AS "MNTH_END_STK_FSCL_YR", "Aggregation 6_4"."CUR_YEAR_MONTH" AS "CUR_YEAR_MONTH", "Aggregation 6_4"."MNTH_END_STK_FSCL_MNTH" AS "MNTH_END_STK_FSCL_MNTH", "Aggregation 6_4"."MNTH_END_STK_SALES_ORG" AS "MNTH_END_STK_SALES_ORG", SUM("Aggregation 6_4"."Current_Stock_Value") AS "Current_Stock_Value" FROM ( SELECT "ZMM_HL_VR_TBLMNTH_STK_9"."SALESORGNAME" AS "SALESORGNAME", "ZMM_HL_VR_TBLMNTH_STK_9"."ARTI_AGNCY_CD" AS "ARTI_AGNCY_CD", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYBYSALES" AS "AGENCYBYSALES", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYGROUP" AS "AGENCYGROUP", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR" AS "MNTH_END_STK_FSCL_YR", TO_DATE("ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR" || "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH" || '01') AS "CUR_YEAR_MONTH", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH" AS "MNTH_END_STK_FSCL_MNTH", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_SALES_ORG" AS "MNTH_END_STK_SALES_ORG", SUM("ZMM_HL_VR_TBLMNTH_STK_9"."Current_Stock_Value") AS "Current_Stock_Value" FROM "ZMM_HL_VR_TBLMNTH_STK" AS "ZMM_HL_VR_TBLMNTH_STK_9" GROUP BY "ZMM_HL_VR_TBLMNTH_STK_9"."SALESORGNAME", "ZMM_HL_VR_TBLMNTH_STK_9"."ARTI_AGNCY_CD", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYBYSALES", "ZMM_HL_VR_TBLMNTH_STK_9"."AGENCYGROUP", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR", TO_DATE("ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_YR" || "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH" || '01'), "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_FSCL_MNTH", "ZMM_HL_VR_TBLMNTH_STK_9"."MNTH_END_STK_SALES_ORG" ) AS "Aggregation 6_4" GROUP BY "Aggregation 6_4"."Agncy_Tblstk", "Aggregation 6_4"."AgencyBySales_Tblstk", "Aggregation 6_4"."AgencyGroup_Tblstk", "Aggregation 6_4"."MNTH_END_STK_FSCL_YR", "Aggregation 6_4"."CUR_YEAR_MONTH", "Aggregation 6_4"."MNTH_END_STK_FSCL_MNTH", "Aggregation 6_4"."MNTH_END_STK_SALES_ORG" ) AS "Projection 5_2" FULL JOIN ( SELECT YEAR("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE") AS "FRCST_YEAR", MONTH("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE") AS "FRCST_MONTH", SUM("ZSD_HL_VR_CURR_COST_FORECAST_10"."FRCST_VALUE_COST_AED") AS "FRCST_VALUE_COST_AED", "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYGROUP" AS "AgencyGroup_Frcst", "ZSD_HL_VR_CURR_COST_FORECAST_10"."VKORG" AS "VKORG", "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYBYSALES" AS "AgencyBySales_Frcst", "ZSD_HL_VR_CURR_COST_FORECAST_10"."Agncy" AS "Agncy_Frcst", "ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE" AS "YEAR_DATE" FROM "ZSD_HL_VR_CURR_COST_FORECAST" AS "ZSD_HL_VR_CURR_COST_FORECAST_10" WHERE "ZSD_HL_VR_CURR_COST_FORECAST_10"."FRCST_YR" >= YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_FORECAST_10"."FRCST_MNTH" >= MONTH(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE" <= ADD_MONTHS(CURRENT_DATE, 3) GROUP BY YEAR("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE"), MONTH("ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE"), "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYGROUP", "ZSD_HL_VR_CURR_COST_FORECAST_10"."VKORG", "ZSD_HL_VR_CURR_COST_FORECAST_10"."AGENCYBYSALES", "ZSD_HL_VR_CURR_COST_FORECAST_10"."Agncy", "ZSD_HL_VR_CURR_COST_FORECAST_10"."YEAR_DATE" ) AS "Aggregation 4_3" ON "Projection 5_2"."MNTH_END_STK_FSCL_YR" = "Aggregation 4_3"."FRCST_YEAR" AND "Projection 5_2"."MNTH_END_STK_FSCL_MNTH" = "Aggregation 4_3"."FRCST_MONTH" AND "Projection 5_2"."Agncy_Tblstk" = "Aggregation 4_3"."Agncy_Frcst" AND "Projection 5_2"."AgencyBySales_Tblstk" = "Aggregation 4_3"."AgencyBySales_Frcst" AND "Projection 5_2"."AgencyGroup_Tblstk" = "Aggregation 4_3"."AgencyGroup_Frcst" AND "Projection 5_2"."MNTH_END_STK_SALES_ORG" = "Aggregation 4_3"."VKORG" ) AS "Projection 4_0" FULL JOIN ( SELECT "Calculated Columns 6_5"."YEAR_MONTH_PO" AS "YEAR_MONTH_PO", SUM(COALESCE("Calculated Columns 6_5"."OPN_OPN_PO_VALUE_COST_AED", 0) + COALESCE("Calculated Columns 11_6"."PO_RCPT_NET_VAL_COST_AED", 0)) AS "PENDING_ARRIVALS", "Calculated Columns 11_6"."YEAR_DATE_OPEN_PO" AS "YEAR_DATE_OPEN_PO", "Calculated Columns 6_5"."YEAR_MONTH_OPN_PO" AS "YEAR_MONTH_OPN_PO", CASE WHEN "Calculated Columns 6_5"."Agncy_Opnpo" IS NULL THEN "Calculated Columns 11_6"."Agncy_Po" ELSE "Calculated Columns 6_5"."Agncy_Opnpo" END AS "Agncy", CASE WHEN "Calculated Columns 6_5"."AgencyGroup_Opnpo" IS NULL THEN "Calculated Columns 11_6"."AgencyGroup_Po" ELSE "Calculated Columns 6_5"."AgencyGroup_Opnpo" END AS "Agency_Group", CASE WHEN "Calculated Columns 6_5"."AgencyBySales_Opnpo" IS NULL THEN "Calculated Columns 11_6"."AgencyBySales_Po" ELSE "Calculated Columns 6_5"."AgencyBySales_Opnpo" END AS "Agency_By_Sales", CASE WHEN "Calculated Columns 6_5"."SalesOrganization_Opnpo" IS NULL THEN "Calculated Columns 11_6"."SalesOrganization_Po" ELSE "Calculated Columns 6_5"."SalesOrganization_Opnpo" END AS "Sales_Organization", CASE WHEN "Calculated Columns 6_5"."YEAR" IS NULL THEN "Calculated Columns 11_6"."Year_Po" ELSE "Calculated Columns 6_5"."YEAR" END AS "Year_PO", CASE WHEN "Calculated Columns 6_5"."MONTH" IS NULL THEN "Calculated Columns 11_6"."Month_Po" ELSE "Calculated Columns 6_5"."MONTH" END AS "Month_PO" FROM ( SELECT "Aggregation 3_7"."Agncy" AS "Agncy_Opnpo", "Aggregation 3_7"."AGENCYGROUP" AS "AgencyGroup_Opnpo", "Aggregation 3_7"."AGENCYBYSALES" AS "AgencyBySales_Opnpo", LEFT("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 4) AS "YEAR", RIGHT("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 2) AS "MONTH", "Aggregation 3_7"."YEAR_MONTH_OPN_PO" AS "YEAR_MONTH_OPN_PO", "Aggregation 3_7"."SALESORG" AS "SalesOrganization_Opnpo", TO_DATE(LEFT("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 4) || RIGHT("Aggregation 3_7"."YEAR_MONTH_OPN_PO", 2) || '01') AS "YEAR_MONTH_PO", "Aggregation 3_7"."OPN_OPN_PO_VALUE_COST_AED" AS "OPN_OPN_PO_VALUE_COST_AED" FROM ( SELECT "ZSD_HL_VR_CURR_COST_OPN_PO_11"."Agncy" AS "Agncy", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYGROUP" AS "AGENCYGROUP", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYBYSALES" AS "AGENCYBYSALES", TO_VARCHAR(CASE WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" < YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= 12 THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" <= YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= MONTH(CURRENT_DATE) THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) ELSE "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" || "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" END) AS "YEAR_MONTH_OPN_PO", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."SALESORG" AS "SALESORG", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" AS "YEAR", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" AS "MONTH", SUM("ZSD_HL_VR_CURR_COST_OPN_PO_11"."OPN_OPN_PO_VALUE_COST_AED") AS "OPN_OPN_PO_VALUE_COST_AED" FROM "ZSD_HL_VR_CURR_COST_OPN_PO" AS "ZSD_HL_VR_CURR_COST_OPN_PO_11" GROUP BY "ZSD_HL_VR_CURR_COST_OPN_PO_11"."Agncy", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYGROUP", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."AGENCYBYSALES", TO_VARCHAR(CASE WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" < YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= 12 THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) WHEN "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" <= YEAR(CURRENT_DATE) AND "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" <= MONTH(CURRENT_DATE) THEN TO_VARCHAR(YEAR(CURRENT_DATE)) || TO_VARCHAR(MONTH(CURRENT_DATE)) ELSE "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR" || "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH END), "ZSD_HL_VR_CURR_COST_OPN_PO_11"."SALESORG", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."YEAR", "ZSD_HL_VR_CURR_COST_OPN_PO_11"."MONTH" ) AS "Aggregation 3_7" ) AS "Calculated Columns 6_5" FULL JOIN ( SELECT "Aggregation 2_8"."PO_RCPT_NET_VAL_COST_AED" AS "PO_RCPT_NET_VAL_COST_AED", "Aggregation 2_8"."SALESORGCODE" AS "SalesOrganization_Po", "Aggregation 2_8"."AGENCYGROUP" AS "AgencyGroup_Po", "Aggregation 2_8"."AGENCYBYSALES" AS "AgencyBySales_Po", "Aggregation 2_8"."Agncy" AS "Agncy_Po", TO_DATE("Aggregation 2_8"."YEAR" || "Aggregation 2_8"."MONTH" || '01') AS "YEAR_DATE_OPEN_PO", "Aggregation 2_8"."YEAR" AS "Year_Po", "Aggregation 2_8"."MONTH" AS "Month_Po" FROM ( SELECT SUM("ZSD_HL_VR_CURR_COST_PO_RCPT_12"."PO_RCPT_NET_VAL_COST_AED") AS "PO_RCPT_NET_VAL_COST_AED", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."SALESORGCODE" AS "SALESORGCODE", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYGROUP" AS "AGENCYGROUP", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYBYSALES" AS "AGENCYBYSALES", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."Agncy" AS "Agncy", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."YEAR" AS "YEAR", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."MONTH" AS "MONTH" FROM "ZSD_HL_VR_CURR_COST_PO_RCPT" AS "ZSD_HL_VR_CURR_COST_PO_RCPT_12" GROUP BY "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."SALESORGCODE", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYGROUP", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."AGENCYBYSALES", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."Agncy", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."YEAR", "ZSD_HL_VR_CURR_COST_PO_RCPT_12"."MONTH" ) AS "Aggregation 2_8" ) AS "Calculated Columns 11_6" ON "Calculated Columns 6_5"."Agncy_Opnpo" = "Calculated Columns 11_6"."Agncy_Po" AND "Calculated Columns 6_5"."AgencyGroup_Opnpo" = "Calculated Columns 11_6"."AgencyGroup_Po" AND "Calculated Columns 6_5"."SalesOrganization_Opnpo" = "Calculated Columns 11_6"."SalesOrganization_Po" AND "Calculated Columns 6_5"."AgencyBySales_Opnpo" = "Calculated Columns 11_6"."AgencyBySales_Po" ) GROUP BY "Projection 4_0"."FRCST_MONTH", "Projection 4_0"."FRCST_VALUE_COST_AED", "Aggregation 5_1"."YEAR_MONTH_OPN_PO", "Aggregation 5_1"."YEAR_MONTH_PO", "Projection 4_0"."CUR_YEAR_MONTH", "Projection 4_0"."Agency", "Projection 4_0"."AgencyGroup", "Projection 4_0"."AgencyBySales", "Aggregation 5_1"."YEAR_DATE_OPEN_PO", "Aggregation 5_1"."PENDING_ARRIVALS", "Projection 4_0"."SalesOrganization", "Projection 4_0"."YEAR_DATE", "Projection 4_0"."Year", "Projection 4_0"."Month", "Aggregation 5_1"."Agncy", "Aggregation 5_1"."Agency_Group", "Aggregation 5_1"."Agency_By_Sales", "Aggregation 5_1"."Sales_Organization", "Aggregation 5_1"."Year_PO", "Aggregation 5_1"."Month_PO"; ``` ### Key Changes: 1. **SUM with CASE**: The sum of `Current_Stock_Value` is calculated only for rows where `SalesOrganization = 1100`. 2. **GROUP BY Clause**: Ensured that the results remain grouped correctly to avoid errors related to aggregation. 3. **New Column**: The new column named `Sum_Current_Stock_Value` is added to display the computed sum. This query will now return the sum of `Current_Stock_Value` for the specified `SalesOrganization`, along with the other requested fields.