0

I have this query .This query have union all

select cm.comp_name,e.Name_Employee,SUM(im.MSalary) MSalary,ID_invoice
    from   MMas im , employee e , company cm
    where              
    e.ID_Employee = im.ID_Employee
    and im.cid = cm.cid
    and im.ID_Employee=74
    group by cm.comp_name,e.Name_Employee , ID_invoice                                                  
    union all    
    select  cm.comp_name, e.Name_Employee,SUM(id.Amount) FBenefits,ID_invoice
    from   MMas im, MDil id,employee e, company cm
    where               
    im.MInd =id.MInd
    and e.ID_Employee=im.ID_Employee
    and cm.cid=im.cid
    and im.ID_Employee=74
     group by cm.comp_name,e.Name_Employee ,ID_invoice

and this shows result like this

     comp_name        Name_Employee  ID_Employee    MSalary   ID_invoice
ABC_Company            David                        74                1000        98950
ABC_Company          David                          74                  0             98950

where as i want result like this

comp_name                   Name_Employee  ID_Employee MSalary F_Benefits       ID_invoice
    ABC_Company       David                           74                       1000          0                        98950

how to do this ?

2 Answers 2

1

Please try this -

SELECT comp_name, Name_Employee,ID_Employee,SUM(MSalary) Salary,SUM(FBenefits)F_Benefits,ID_invoice
FROM 
(
    select cm.comp_name,e.Name_Employee,SUM(im.MSalary) MSalary, 0 FBenefits ,ID_invoice
    from   MMas im , employee e , company cm
    where              
    e.ID_Employee = im.ID_Employee
    and im.cid = cm.cid
    and im.ID_Employee=74
    group by cm.comp_name,e.Name_Employee , ID_invoice                                                  
    union all    
    select  cm.comp_name, e.Name_Employee,0 MSalary ,SUM(id.Amount) FBenefits,ID_invoice
    from   MMas im, MDil id,employee e, company cm
    where               
    im.MInd =id.MInd
    and e.ID_Employee=im.ID_Employee
    and cm.cid=im.cid
    and im.ID_Employee=74
    group by cm.comp_name,e.Name_Employee ,ID_invoice
)x 
GROUP BY comp_name, Name_Employee,ID_Employee,ID_invoice
Sign up to request clarification or add additional context in comments.

2 Comments

CTE is just a wrapper for the temporary result set of the SQL present inside. So once you put your sql inside the CTE you can use it in the next statement For more details pls read from learn.microsoft.com/en-us/sql/t-sql/queries/… technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx
if i dont want to use CTE then how i did this because this seems confusion in your query
0

you should be able to get the FBenefits amount using Outer Apply and take out the Union

select  cm.comp_name,
        e.Name_Employee,
        sum(im.MSalary) MSalary,
        id.FBenefits,
        ID_invoice
from    employee e
join    MMas im on e.ID_Employee = im.ID_Employee
join    company cm on im.cid = cm.cid
outer apply (
        select  sum(id.Amount) FBenefits
        from    MDil id
        where   im.MInd = id.MInd
)       id
where   e.ID_Employee = 74
group by cm.comp_name,
        e.Name_Employee,
        id.FBenefits,
        ID_invoice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.