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.
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). 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.
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). 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. This gives me the desired result of counts for productA for all 102 months.
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
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.
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
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.