简体   繁体   English

SQL结合两个查询

[英]SQL Combining two queries

The below code selects records from the two tables where both the email and dob match another record (all duplicates..) 下面的代码从两个表中选择记录,其中电子邮件和dob匹配另一个记录(所有重复。)

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'. 我还想在此表中添加另一个名为“ Audit”的表中的字段。 We can join them using the LoginID, however the LoginID has a one to many relationship in the Audit table. 我们可以使用LoginID加入他们,但是LoginID在Audit表中具有一对多的关系。 ie a LoginID can have many Audits. 即,一个LoginID可以具有许多审核。

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? 谁能建议我如何组合这两个查询,以便我的原始查询具有到“审计”表的联接,并显示最新审计开始日期的“ StartDate”列?

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. 这样可以避免构建另一个数据库对象来支持此查询,并且这是一个相对标准的SQL构造。

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. 然后,您可以将MostRecentLogins视图加入其他查询中。 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: 从您的帖子中尚不清楚哪一列与LoginId对应(从Audit表中),但是查询看起来像这样:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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