Aşağıdaki gibi bir SQL'im var, bu SQL iki tabloyu UNION ALL ile birleştiriyor ve sonucunu 2 satır olarak getiriyor. Bunun sebebi DESCRIPTION sütunu. Bu alan FORECASTDB tablosunda var ama BUDGETPROSPECTS tablosunda yok. Ben bu sorgunun sonucunda tek satır dönmesini istiyorum. DESCRIPTION bilgisi sadece FORECASTDB tablosundan gelsin, diğer tabloda zaten bu alan olmadığı için oradan bir şey gelmesine gerek yok.
SELECT SPECIALTY, DESCRIPTION, SUM(BUDGET) BUDGET, SUM(FORECAST) FORECAST FROM ( SELECT NEW_SP_CODE SPECIALTY, '' DESCRIPTION, SUM(DEC_VALUE) BUDGET, 0 FORECAST FROM MERCURY2011.BUDGETPROSPECTS WHERE BUDGET_YEAR LIKE '%2015%' AND DEC_VALUE <> 0 AND PROSPECT_CODE = 2528 GROUP BY NEW_SP_CODE UNION ALL SELECT NEW_SP_CODE SPECIALTY, DESCRIPTION DESCRIPTION, 0 BUDGET, SUM(AMOUNT) FORECAST FROM MERCURY2011.FORECASTDB WHERE REVENUE_DATE = '01/12/2015' AND GROUP_CODE = 2528 GROUP BY NEW_SP_CODE, DESCRIPTION) GROUP BY SPECIALTY, DESCRIPTION
SELECT SPECIALTY, MAX(DESCRIPTION) DESCRIPTION, SUM(BUDGET) BUDGET, SUM(FORECAST) FORECAST FROM ( SELECT NEW_SP_CODE SPECIALTY, '' DESCRIPTION, SUM(DEC_VALUE) BUDGET, 0 FORECAST FROM MERCURY2011.BUDGETPROSPECTS WHERE BUDGET_YEAR LIKE '%2015%' AND DEC_VALUE <> 0 AND PROSPECT_CODE = 2528 GROUP BY NEW_SP_CODE UNION ALL SELECT NEW_SP_CODE SPECIALTY, DESCRIPTION DESCRIPTION, 0 BUDGET, SUM(AMOUNT) FORECAST FROM MERCURY2011.FORECASTDB WHERE REVENUE_DATE = '01/12/2015' AND GROUP_CODE = 2528 GROUP BY NEW_SP_CODE, DESCRIPTION) GROUP BY SPECIALTY