简体   繁体   中英

SQL Combining two queries

The below code selects records from the two tables where both the email and dob match another record (all duplicates..)

SELECT 
    AccountName, 
    EmailAddress, 
    DateOfBirth
FROM 
(
    SELECT 
        a.AccountName, 
        a.EmailAddress, 
        u.DateOfBirth, 
        COUNT(*) over (partition by a.EmailAddress, u.DateOfBirth) AS cnt
    FROM Account AS a 
    JOIN [User] AS u ON a.AccountID = u.AccountID              
) ua                
WHERE cnt > 1
AND EmailAddress IS NOT null
AND DateOfBirth IS NOT null     
ORDER BY EmailAddress, DateOfBirth 

I also want to add to this table, a field within another table called 'Audit'. We can join them using the LoginID, however the LoginID has a one to many relationship in the Audit table. ie a LoginID can have many Audits.

I want to add the Audit StartDate column. The following query allows me to identify the latest Audit by date.

         SELECT  a.LoginID as AuditLoginID,
         MAX(StartDate) as StartDate
         FROM    Audit as a
         GROUP BY  a.LoginID
         ORDER BY a.StartDate

Would anyone be able to suggest how I can combine these two queries, so that my original query has a join to the Audit table, displaying a 'StartDate' column of the latest audit start date?

You should consider using a correlated subquery . That will avoid building another database object to support this query, and it's a relatively standard SQL construct.

Example:

SELECT 
    AccountName, 
    EmailAddress, 
    DateOfBirth
FROM 
(
    SELECT 
        a.AccountName, 
        a.EmailAddress, 
        u.DateOfBirth, 
        a.LoginID,
        COUNT(*) over (partition by a.EmailAddress, u.DateOfBirth) AS cnt
    FROM Account AS a 
    JOIN [User] AS u ON a.AccountID = u.AccountID              
) ua           
join Audit as a 
  on a.LoginID = au.LoginID     
WHERE cnt > 1
AND EmailAddress IS NOT null
AND DateOfBirth IS NOT null     
AND a.startdate = (SELECT MAX(StartDate) as StartDate
                   FROM    Audit as b
                   WHERE b.LoginID = a.LoginID)
ORDER BY EmailAddress, DateOfBirth 

Here's an expansion on my comment:

CREATE VIEW MostRecentLogins AS
  (
     SELECT a.LoginID as AuditLoginID,
            MAX(StartDate) as StartDate
       FROM Audit as a
      GROUP BY a.LoginID
  )

Then, you can join the MostRecentLogins view into your other query. It's not clear from your post which column would be the counterpart to LoginId (from the Audit table) but the query would then look something like this:

SELECT a.AccountName, 
       a.EmailAddress, 
       u.DateOfBirth,
       MRL.StartDate
  FROM 
     (
       SELECT a.AccountName, 
              a.EmailAddress, 
              u.DateOfBirth, 
              COUNT(*) over (partition by a.EmailAddress, u.DateOfBirth) AS cnt
         FROM Account AS a 
              JOIN [User] AS u 
                       ON a.AccountID = u.AccountID              
     ) ua
     INNER JOIN MostRecentLogins MRL
         ON MRL.LoginID = a.LoginID    -- not sure what column should be on the RHS of this..
  WHERE cnt > 1
    AND EmailAddress IS NOT null
    AND DateOfBirth IS NOT null     
  ORDER BY EmailAddress, DateOfBirth 

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