SQL query to combine two user tables, remove duplcates and find the latest update entry for each user

StackOverflow https://stackoverflow.com/questions/22310719

Domanda

I have two SQL tables. TableA has a list of active users, TableB has a list of all help tickets the users submitted with emails and submit dates.

I need to output a list of users that exist in Table A & Table B (excluding users only in TableB) along with the date of the last help ticket they submitted. This is because several users were removed from the active users table, but their help tickets remain in the second table.

Table Fields:

eUser
eUserName, eEmailAddress

Work_Orders
txtContactEmail, SubmitDate


Desired output:
eUserName, eEmailAddress, SubmitDate

My Attempt

SELECT   eUser.eUserName
       , eUser.eEMailAddress
       , Work_Orders.txtContactEmail
       , max(Work_Orders.dtRequest) lastupdate 
FROM eUser inner join Work_Orders 
on eUser.eEMailAddress = Work_Orders.txtContactEmail 
group by eUser.eUserName
       , eUser.eEMailAddress
       , Work_Orders.txtContactEmail
È stato utile?

Soluzione

SELECT  A.eUserName,
        A.eEmailAddress,
        B.MaxSubmitDate
FROM TableA AS A
INNER JOIN (SELECT txtContactEmail, MAX(SubmitDate) MaxSubmitDate
            FROM TableB
            GROUP BY txtContactEmail) AS B
    ON A.eEmailAddress = B.txtContactEmail
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top