简体   繁体   中英

How to get a a matching records from 1st and 2nd table and only non matching records from 1st table in SQL Server having joined by 1 field

I wanted to get matching records from 2 tables and only non matching record from one table. I have two common fields between them known as Module_ID and one field in 2nd table as PK_ID. I want to return all records from 2nd table with Maximum value of PK_ID as it has duplicate records so i want to return the record having highest value of PK_ID but also i need records from TAble 1 having matching record with the Table 2 and non matching records from table 1.

When I use only three columns, it returns the right number of records but when I use more than that, it returns more than what i want

Here is the query with 3 fields and also i will give full query which i want proper count. Do i have to use temporary table?

Please help.

Select 
    IM.MODULE_ID, 
    Max(Pk_Id), Module_Name 
from 
    PACKAGE IP 
right join 
    RELEASE_MANAGEMENT_TRAN IM on IM.MODULE_ID = IP.MODULE_ID 
Group by 
    IM.MODULE_ID,MODULE_NAME

This returns the right no of records which I want. Right table is 1st table and left table is 2nd. Right table has more records which are all unique with no duplicates but LEft table has duplicates.

But if I add more columns to this query it returns more than the required records. Some columns are of Table 1 and some fields of Table 2.

This is the entire query all fields which I want:

Select  
    IM.MODULE_ID, max(ip.PK_ID), MODULE_NAME, 
    COMPLEXITY, RECEIVED_DATE, 
    datename(month, RECEIVED_DATE) as Month_Received,
    CASE 
       WHEN STATUS = 'C' THEN 'DELIVERED' 
       WHEN STATUS = 'I' THEN 'IN PROGRESS' 
       WHEN STATUS = 'H' THEN 'ON HOLD' 
       WHEN STATUS = 'R' THEN 'REJECTED' 
       WHEN STATUS = 'D' THEN 'CANCELLED' 
    END AS STATUS,
    Delivered_Date, DATENAME(month, Delivered_Date) as Month_Delivered,
    SLA,
    CASE 
       WHEN IP.Failed_Module = '1' THEN 'YES' 
       WHEN ip.Failed_Module = '2' THEN 'NO' 
    END Failed_Module,
    First_Time_Platform_Test, im.TARGET_DATE, 
    ip.COMMENTS, ip.BUILD_NAME, PACKAGING_EFFORTS, SERVICE_AREA,
    CASE 
       WHEN LOCAL_SOLUTION = '1' THEN 'Y'  
       WHEN LOCAL_SOLUTION = '2' THEN 'N' 
    END LOCAL_SOLUTION,
    SOURCE_TYPE, Deliverable_Type,
    CASE 
       WHEN PLATFORM = 'C' THEN 'Client' 
       WHEN PLATFORM = 'S' THEN 'Server' 
       WHEN PLATFORM = 'TS' THEN 'Terminal Server' 
       WHEN PLATFORM = 'CS' THEN 'Client, Server' 
       WHEN PLATFORM = 'CTS' THEN 'Client, Terminal Server'  
       WHEN PLATFORM = 'TSS' THEN 'Terminal Server, Server' 
       WHEN PLATFORM = 'CTSS' THEN 'Client, Terminal Server, Server' 
    END AS PLATFORM
FROM 
    PACKAGE IP 
RIGHT JOIN 
    RELEASE_MANAGEMENT_TRAN IM ON IP.MODULE_ID = IM.MODULE_ID 
GROUP BY
    IM.MODULE_ID, MODULE_NAME, COMPLEXITY, RECEIVED_DATE,
    ip.STATUS, Delivered_Date, SLA, ip.Failed_Module,
    First_Time_Platform_Test, im.TARGET_DATE, ip.COMMENTS, ip.BUILD_NAME,
    PACKAGING_EFFORTS, SERVICE_AREA, LOCAL_SOLUTION, SOURCE_TYPE,
    Deliverable_Type, PLATFORM 

Below is structure and sample data

Table No 2:- All unique records Release_Management_Tran table has Module_ID Module_Name Request_id Complexity Priority Recieved_Date Local_Soln 1 xyz 1 S Normal 12/6/2020 Yes

Failed_Module Failed_Resource AssignedTo Platform TargetDate YN tom server 15/6/2020

Table no 1 (Multiple instance of the Primary key field of Table no 2)

PK_Id Module_ID (Foreign Key) Tran_Id Status Efforts Deliverable_Type... 1 1 1 I 2 abcde 2 1 1 C 2 abcde

as you can see i have more than one instance of the module_id of table 2 but i just want that From table 1 we should pickup the module having higher value of PK_ID. And if there is no record of the same module_id in Table 1 then just that from Table 2 instance of module id.

I hope i could explain properly. If want further clarification please ask me.

I have got the solution for this posting below so that it might be useful to someone. I used CTE to get the initial records by using aggregate Max with the unique id of the table in it and then right join it with the other table. Below is the complete query.

with CTEIkeaReport(MODULE_ID,PK_ID,MODULE_NAME,COMPLEXITY,PRIORITY,RECIEVED_DATE,LOCAL_SOLUTION,USER_FNAME,SERVICE_AREA,PLATFORM)

AS (

Select im.MODULE_ID,max(ip.PK_ID) as PK_ID,MODULE_NAME,COMPLEXITY,PRIORITY,RECIEVED_DATE,LOCAL_SOLUTION,user_fname,SERVICE_AREA,PLATFORM From tblPACKAGE IP right join RELEASE_MANAGEMENT im on im.MODULE_ID= ip.MODULE_ID inner join user_master um on um.id = im.ASSIGNED_TO group by im.MODULE_ID,MODULE_NAME,COMPLEXITY,PRIORITY,RECIEVED_DATE,LOCAL_SOLUTION,USER_FNAME,SERVICE_AREA,PLATFORM )

Select MODULE_NAME, COMPLEXITY,RECIEVED_DATE,datename(month,RECIEVED_DATE) as Month_Recieved,CASE WHEN STATUS = 'C' THEN 'DELIVERED' WHEN STATUS = 'I' THEN 'IN PROGRESS' WHEN STATUS = 'H' THEN 'ON HOLD' when STATUS = 'R' THEN 'REJECTED' WHEN STATUS = 'D' THEN 'CANCELLED' END AS STATUS,Delivered_Date,DATENAME(month,Delivered_Date) as Month_Delivered,SLA,case when IP.Failed_Module = '1' then 'YES' when ip.Failed_Module = '2' then 'NO' END Failed_Module,First_Time_Platform_Test,ip.COMMENTS,ip.BUILD_NAME,PACKAGING_EFFORTS,SERVICE_AREA,case when LOCAL_SOLUTION='1' then 'Y' when LOCAL_SOLUTION='2' then 'N' end LOCAL_SOLUTION,SOURCE_TYPE,Deliverable_Type,CASE WHEN PLATFORM = 'C' THEN 'Client' when PLATFORM = 'S' THEN 'Server' WHEN PLATFORM = 'TS' then 'Terminal Server' when PLATFORM = 'CS' THEN 'Client, Server' when PLATFORM = 'CTS' THEN ' Client, Terminal Server' when PLATFORM = 'TSS' THEN 'Terminal Server, Server' when PLATFORM = 'CTSS' THEN 'Client, Terminal Server, Server' end As PLATFORM FROM PACKAGE IP right JOIN CTEReport CT on CT.PK_ID = ip.PK_ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM