简体   繁体   English

SQL LEFT JOIN with WHERE 子句语法错误(MS Access)

[英]SQL LEFT JOIN with WHERE Clause Syntax Error (MS Access)

I have a table of data showing sales of various product models over an 8 year period ( tblSales ) Each sale record lists transaction info including, product, transaction time stamp, and column called year_month based on the time stamp.我有一张数据表,显示 8 年期间各种产品型号的销售额( tblSales )每条销售记录都列出了交易信息,包括产品、交易时间戳和基于时间戳的名为 year_month 的列。

I need to create a query that returns a count sale for a product model for each year_month in the dataset (102 months total in total).我需要创建一个查询,返回数据集中每个 year_month 产品 model 的销售计数(总共 102 个月)。 The issue is that some models weren't purchased in a given month, so I need the query to return a 0 for that month.问题是某些型号在给定月份没有购买,因此我需要查询以返回该月份的 0。

Currently I am able to accomplish this by creating a second table and two separate queries.目前我可以通过创建第二个表和两个单独的查询来实现这一点。

I created a table ( tblYrMo ) with each of the year_months from the original table( tblSales ), then qry1 of month counts for productA (which leaves out months with no productA sales).我用原始表( tblSales tblYrMo ,然后产品 A 的月份计数为qry1 (这排除了没有 productA 销售的月份)。 Next, qry2 which is a Left Join of tblYrMo to qry1 using the NZ() function to produce a 0 for months with no product sales.接下来, qry2tblYrMoqry1的左连接,使用 NZ() function 在没有产品销售的月份产生 0。 This gives me the desired result of counts for productA for all 102 months.这为我提供了所有 102 个月的 productA 计数结果。

Using this method for several products creates dozens of queries, so I attempted to simplify my code and create only one query using a subquery.对多个产品使用这种方法会创建数十个查询,因此我尝试简化代码并使用子查询仅创建一个查询。 However, I'm getting a syntax error ("syntax error in join operation").但是,我收到语法错误(“连接操作中的语法错误”)。 I'm wondering if anyone here can help, or if this is even possible in MS Access SQL我想知道这里是否有人可以提供帮助,或者这在 MS Access SQL 中是否可行

Here is the code I wrote isn't running.这是我写的代码没有运行。 The error highlights the subquery FROM tblSales , so I've tried putting that in [], and I've tried putting all the tabels/columns in [], no luck.该错误突出显示了FROM tblSales的子查询,因此我尝试将其放入 [],并且尝试将所有表格/列放入 [],但没有运气。

SELECT tblYrMo.year_month
FROM tblYrMo
LEFT JOIN 
    (SELECT Count(NZ([tblSales].[year_month],0)) as Monthly_Count
     FROM  tblSales
     WHERE (tblSales.[product_model] Like "A"))
ON tblYrMo.year_month = tblSales.year_month;

For reference here are the two queries I'm using that currently get the job done作为参考,这里是我正在使用的两个查询,它们目前可以完成工作

qry1 qry1

SELECT tblSales.year_month, Count(tblSales.year_month) AS Monthly_Count
FROM tblSales
WHERE (((tblSale.[product_model]) Like "A"))
GROUP BY tblSales.year_month;

qry2 qry2

SELECT tblYrMo.year_month, Nz([qry1].[Monthly_Count],0) AS Monthly_Sale_Count
FROM tblYrMo 
LEFT JOIN qry1
ON tblYrMo.year_month = qry1.year_month;

You need a table alias:您需要一个表别名:

SELECT tblYrMo.year_month
FROM tblYrMo LEFT JOIN 
     (SELECT Count(NZ([tblSales].[year_month],0)) as Monthly_Count
      FROM  tblSales
      WHERE tblSales.[product_model] Like "A"
     ) as s
     ON tblYrMo.year_month = s.year_month;

I think the main issue is the query does not return what you want, I think you are close:我认为主要问题是查询没有返回你想要的,我认为你很接近:

SELECT tblYrMo.year_month, Nz([qry1].[Monthly_Count],0) AS Monthly_Sale_Count
FROM tblYrMo 
LEFT JOIN (
   SELECT tblSales.year_month, Count(tblSales.year_month) AS Monthly_Count
   FROM tblSales
   WHERE (((tblSale.[product_model]) Like "A"))
   GROUP BY tblSales.year_month
) as qry1
ON tblYrMo.year_month = qry1.year_month;

I think this is the kind of merged query that you were looking for.我认为这是您正在寻找的那种合并查询。 I might also add a group by of the sales product model and include that field in your results.我还可以添加销售产品 model 的分组,并将该字段包含在您的结果中。

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

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