Sql 基于其他两个表筛选记录
我需要在MS Access数据库中生成活动客户的列表。所有过去和现在的客户都存储在客户表中。然而,确定活动状态的标准需要从另外两个表中得出:入口和出口。如果客户的入职日期后没有退出日期,则视为活跃客户。然而,令人困惑的是,有退出日期的前客户可以通过获得新的进入日期再次成为客户 以下是存储此信息的三个表结构的相关部分:Sql 基于其他两个表筛选记录,sql,ms-access,join,subquery,ms-access-2010,Sql,Ms Access,Join,Subquery,Ms Access 2010,我需要在MS Access数据库中生成活动客户的列表。所有过去和现在的客户都存储在客户表中。然而,确定活动状态的标准需要从另外两个表中得出:入口和出口。如果客户的入职日期后没有退出日期,则视为活跃客户。然而,令人困惑的是,有退出日期的前客户可以通过获得新的进入日期再次成为客户 以下是存储此信息的三个表结构的相关部分: customers table customerID name intake table intakeID customerI
customers table
customerID
name
intake table
intakeID
customerID
intakeDate
exit date
exitID
customerID
exitDate
一个客户可以有多个入口记录和多个出口记录。因此,SQL语句的伪代码需要如下所示:
SELECT customerID, name FROM customers
WHERE ((most recent intakeDate)>(most recent exitDate(if any)))
在真正的SQL中应该是什么样子?用于MS Access 2010数据库。显然,连接是必要的。但是什么类型的连接呢?它需要看起来怎么样?我喜欢@sqlgrl的方法,即只查看每个客户最近的入职和离职情况,但我已经将其调整为使用MS Access特定的语法,并且我认为,连接逻辑稍微收紧了一点:
SELECT ActiveCustomers.*, tblAddress.* FROM
(
SELECT customers.name, customers.customerID,
(
SELECT COUNT(intakeDate)
FROM intake
WHERE customers.customerID = intake.customerID AND Len(intakeDate & '') > 0
) AS IntakeCount,
(
SELECT COUNT(exitDate)
FROM exit
WHERE customers.customerID = exit.customerID AND Len(exitDate & '') > 0
) AS ExitCount
FROM customers
) AS ActiveCustomers
INNER JOIN tblAddress ON ActiveCustomers.customerID = tblAddress.customerID
WHERE IntakeCount > ExitCount
AND tblAddress.CurrentAddress = True
select c.*
from ([customers table] as c
inner join (
select customerID, max(exitDate) as lastOut
from [exit date]
group by customerID
) as [out]
on c.customerID = [out].customerID)
inner join (
select customerID, max(intakeDate) as lastIn
from [intake table]
group by customerID
) as [in]
on c.customerID = [in].customerID
where [in].lastIn > [out].lastOut
以上基本上是这样说的:
- 建立每个客户最近退出日期的列表
- 建立每个客户最近入职日期的列表
- 将这两个列表与customers表连接起来
- 如果客户最近的入职日期在其最近的离职日期之后,则将该客户包括在最终输出中
select c.*
from ([customers table] as c
inner join (
select customerID, max(exitDate) as lastOut
from [exit date]
group by customerID
) as [out]
on c.customerID = [out].customerID)
inner join (
select customerID, max(intakeDate) as lastIn
from [intake table]
group by customerID
) as [in]
on c.customerID = [in].customerID
where [in].lastIn > [out].lastOut
以上基本上是这样说的:
- 建立每个客户最近退出日期的列表
- 建立每个客户最近入职日期的列表
- 将这两个列表与customers表连接起来
- 如果客户最近的入职日期在其最近的离职日期之后,则将该客户包括在最终输出中
ActiveCustomers
。例如,来自ActiveCustomers内部的在ActiveCustomers.CustomerID=OtherTable.CustomerID
上加入OtherTable。然后在我有的地方选择*
您将指定您想要的所有字段。再次感谢您。令人困惑的是我应该如何捆绑ActiveCustomers代码。假设我还想添加一个字段,其中包含每个活动客户当前地址的电话号码(如果在地址表中的某个客户地址记录中指定了当前地址),或者如果没有将任何地址选中为当前地址,则将电话号码保留为空。我应该将以下内容放在哪里:“ActiveCustomers.customerID=tblAddress.customerID中的内部连接tblAddress.CurrentAddress=-1”?我希望在看到你的回答后,我能自己把其他部分放在一起。在我的代码中,“SELECT customers.name,customers.customerID”实际上是“SELECT customers.customerID,customers.firstName,customers.lastName”。但无论我如何排列这三个字段,Access都只能看到前两个字段,并且会出现一个对话框,询问列表中第三个字段的值。目前,它在生成的数据表中将lastName保留为空。知道如何访问这三个字段吗?我不知道上面的SQL是如何工作的。第3-4行肯定会出错?谢谢+一个快速周到的回应。您建议计算入口和出口,而不是明确返回比任何出口日期都要晚的入口日期?此外,我还将在MS Access中创建一个报告,该报告从其他三个表中提取数据,以填写此查询确定的活动客户的信息。你对我将怎样做有什么一般性的评论吗