Click here to Skip to main content
15,884,933 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
table 1
ID  Name   Address  Basic_Salary  Basic_Allowance
1   Moses  MTM      1000          500
2   GEO    NGL      3000          1500
Another table

table 2

ID  Name   Address  Salary        Allowance      Month
1   Moses  MTM      800           220            Nov

I Need the result

ID  Name   Address   Salary      Allowance   Month       Status
1   Moses  MTM       800         220         Nov         Paid
2   GEO    NGL       3000        1500        Nov         Not Paid
Can Any one help me how to solve it
Posted
Updated 8-Nov-14 3:15am
v2
Comments
[no name] 8-Nov-14 9:18am    
Think about something like this:
INSERT INTO DST .... WHERE NOT EXISTS (SELECT DST.ID ....)
Tomas Takac 8-Nov-14 9:34am    
It is not clear where do you get the values of Month and Status. Yes, there is Month in table 2 but where did the other values come form? Do you want to insert new records like Bruno suggested or do you need to update changed records as well?
[no name] 8-Nov-14 9:46am    
Uuups, you are completely right. I read the Q in a very lazy way. I focused only on "merge"...which does not really means always "insert", maybe OP needs simply to join the two tables. But that OP has to make clear.

1 solution

Your question is not clear. But I am assuming that your first table is the Salary table and the second one which has been paid out.

Based on the vagueness of your question, I guess this is what you want:

SQL
SELECT
    Salary.ID,
    Salary.Name,
    Salary.Address,
    CASE WHEN Paid_Salary.Salary IS NULL THEN Salary.Basic_Salary ELSE Paid_Salary.Salary END AS Salary,
    CASE WHEN Paid_Salary.Allowance IS NULL THEN Salary.Basic_Allowance ELSE Paid_Salary.Allowance END AS Allowance,
    Paid_Salary.Month,
    CASE WHEN Paid_Salary.Salary IS NULL THEN 'Not Paid' ELSE 'Paid' END AS Status
FROM
    Salary LEFT JOIN Paid_Salary ON
        Salary.Name = Paid_Salary.Name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900