-8

This is the current query:

SELECT 
    cod.COM_DESC
   ,count(emp.OBE_Name) colValue
   ,'Total'[Desc] 
FROM OBP_EMP_MASTER emp
LEFT JOIN COMMONCODES cod 
  ON  emp.OBE_AGENT_DR = cod.COM_SLNO 
WHERE OBE_AGENT_DR IS NOT NULL 
GROUP BY cod.COM_DESC

UNION ALL

SELECT 
     cod1.COM_DESC
    ,count(rep.OBE_Name) colValue
    ,'Replaced'[Desc]  
FROM OBP_EMP_REPLACE_HISTORY rep 
LEFT JOIN COMMONCODES cod1 
  ON rep.OBE_AGENT_DR = cod1.COM_SLNO 
WHERE OBE_AGENT_DR IS NOT NULL
GROUP BY cod1.COM_DESC

RESULT

enter image description here

DESIRED RESULT

enter image description here

7
  • 1
    Search for conditional sum or conditional aggregation. Sep 10, 2015 at 6:37
  • 2
    You can also check for using PIVOT ;).
    – shA.t
    Sep 10, 2015 at 6:39
  • 6
    you have to show what have tried and why it failed. You cannot expect someone to post you entire solution and solve your problems Sep 10, 2015 at 6:40
  • With that attitude, just go hire someone.
    – lc.
    Sep 10, 2015 at 6:42
  • 1
    Add more sample data, it's not quite clear how to get the expected result.
    – jarlh
    Sep 10, 2015 at 6:57

1 Answer 1

0

Try:

select
    Agent,
    max(case when `desc` = 'Total' then ColValue else 0 end) Total,
    max(case when `desc` = 'Replaced' then ColValue else 0 end) Replaced
from tbl
group by Agent  

Demo sqlfiddle

1
  • 1
    I suppose SUM is what OP wants. (Same answer with sample data, I know, but if OP adds more sample data we will know.)
    – jarlh
    Sep 10, 2015 at 6:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.