在Microsoft Access中创建包含CROSS JOIN后跟LEFT JOIN的查询时出现问题。

huangapple go评论65阅读模式
英文:

Problem in creating the query containing a CROSS JOIN followed by a LEFT JOIN in Microsoft Access

问题

我试图将以下的SQLite查询转换成MS-ACCESS(Microsoft Access 2016)查询,但没有成功:

SQLite中的查询:

SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c CROSS JOIN LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;

我尝试了以下MS-ACCESS中的查询,但出现了“JOIN操作中的语法错误”:

MS-ACCESS中的查询:

SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c, LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;

如何在MS-ACCESS中重新编写这个查询?

英文:

I'm trying to convert the following SQLite query to a MS-ACCESS (Microsoft Access 2016) query without any success:

query in SQLite:

SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c CROSS JOIN LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;

I've tried the following query in MS-ACCESS which leads to the 'Syntax error in JOIN operation' error:

my query in MS-ACCESS:

SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c, LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;

How could I rewrite the query in MS-ACCESS?

答案1

得分: 1

以下是翻译好的部分:

MS-Access中的语法非常不同。

不支持显式的CROSS连接,必须使用旧的逗号语法。

此外,连接的ON子句不能包含像LoanDate = '19990125'这样的表达式。

查询可以这样编写:

SELECT t.CategoryName, t.SubCategoryName, Nz(l.Count, 0) AS Count
FROM (
  SELECT c.CategoryId, c.CategoryName, 
         s.CategoryId AS SubCategoryId, s.CategoryName AS SubCategoryName
  FROM LoanCategories AS c, LoanSubCategories AS s
) AS t
LEFT JOIN (
  SELECT LoanCatId, LoanSubCatId, COUNT(*) AS Count
  FROM Loans
  WHERE LoanDate = '19990125'
  GROUP BY LoanCatId, LoanSubCatId
) AS l ON l.LoanCatId = t.CategoryId AND l.LoanSubCatId = t.SubCategoryId;
英文:

The syntax in MS-Access is quite different.<br/>

The explicit CROSS join is not supported and you must use the old comma-syntax.<br/>

Also, the ON clause of a join can't contain expressions like LoanDate = &#39;19990125&#39;.<br/>

The query could be written like this:

SELECT t.CategoryName, t.SubCategoryName, Nz(l.Count, 0) AS Count
FROM (
  SELECT c.CategoryId, c.CategoryName, 
         s.CategoryId AS SubCategoryId, s.CategoryName AS SubCategoryName
  FROM LoanCategories AS c, LoanSubCategories AS s
) AS t
LEFT JOIN (
  SELECT LoanCatId, LoanSubCatId, COUNT(*) AS Count
  FROM Loans
  WHERE LoanDate = &#39;19990125&#39;
  GROUP BY LoanCatId, LoanSubCatId
) AS l ON l.LoanCatId = t.CategoryId AND l.LoanSubCatId = t.SubCategoryId;

huangapple
  • 本文由 发表于 2023年5月7日 22:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定