如何在sql中嵌套多个左联接和子查询

rjjhvcjd  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(448)

如何同时嵌套两个子查询和两个左连接?我目前有我的查询(query1)如下,这是完美的作品。。但它需要连接两个外部查询,它们必须按数据日期分别更新。
我想通过应用子查询(query2)来扩展query1以供实践,并在一个脚本中更新所有内容。
注意:查询是用ms-access编写的。
非常感谢您的帮助!

SELECT DISTINCT 
Acct.AccountNumber
, Acct.ABCat AS Cat
, Acct.fName AS FirstName
, Acct.lName AS LastName
, Acct.Age AS Age
, o.Type, o.OFirstLoginDate
, o.OLastLoginDate
, m.Type
, m.LstDepDate AS LastDepDate
FROM 
table1 AS Acct 
LEFT JOIN 
        OB_table AS o 
        ON Acct.AccountNumber = o.AccountNumber

LEFT JOIN 
        MB_table AS m 
        ON Acct.AccountNumber = m.AccountNumber
WHERE 
Acct.Cat IN (9,12,16,17)
AND Acct.DataDate = 20200430 
AND Acct.Status = 'Open';

查询2使用子查询和左连接我得到错误

Syntax error (missing operator) in query expression 'Acct.AccountNumber = o.AccountNumber    
LEFT JOIN 
(SELECT DISTINCT 
ParentAccount AS AccountNumber
,TYPE
,LstDepDate AS LastDepDate
FROM MBtable
WHERE ProcessDate = 20200430
SELECT DISTINCT 
Acct.AccountNumber
, Acct.ABCat AS Cat
, Acct.fName AS FirstName
, Acct.lName AS LastName
, Acct.Age AS Age
, o.Type, o.OFirstLoginDate
, o.OLastLoginDate
, m.Type
, m.LstDepDate AS LastDepDate
FROM 
table1 AS Acct 
LEFT JOIN 
(
SELECT DISTINCT 
Online.Account AS AccountNumber
, Online.TYPE
, Online.OBFLDate AS OFirstLoginDate
, Online.OBLLDate AS OLastLoginDate
FROM OBtable AS Online
WHERE Online.DataDate=20200430 
And Online.OBFLDate Is Not Null
)
AS o 
ON Acct.AccountNumber = o.AccountNumber
LEFT JOIN
(
SELECT DISTINCT 
ParentAccount AS AccountNumber
, TYPE
, LstDepDate AS LastDepDate
FROM MBtable
WHERE ProcessDate = 20200430 
AND FDate IS NOT NULL
ORDER BY LDate DESC
) 
AS m 
ON Acct.AccountNumber = m.AccountNumber
WHERE Acct.Cat IN (9,12,16,17)
 AND Acct.DataDate=20200430 
AND Acct.Status = 'Open';
6yt4nkrj

6yt4nkrj1#

你没有详细说明“不起作用”的含义。我仍然认为,如果您使用mssqlserver,那么您的案例可以很好地作为使用“outerapply”的候选者。实际示例,何时在sql中使用outer/cross-apply

相关问题